Crossfire Client, Trunk  R20693
misc.c
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 1999-2013 Mark Wedel and the Crossfire Development Team
5  * Copyright (c) 1992 Frank Tore Johansen
6  *
7  * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8  * welcome to redistribute it under certain conditions. For details, please
9  * see COPYING and LICENSE.
10  *
11  * The authors can be reached via e-mail at <crossfire@metalforge.org>.
12  */
13 
20 #include "client.h"
21 
22 #include <errno.h>
23 
24 #ifndef WIN32
25 #include <sys/wait.h>
26 #else
27 #include <direct.h>
28 #include <io.h>
29 #endif
30 
33 
50 void replace_chars_with_string(char* buffer,
51  const guint16 buffer_size,
52  const char find,
53  const char* replace )
54 {
55 
56  guint16 buffer_len, expand, i, replace_len, replace_limit, template_len;
57  char* template;
58 
59  replace_limit = buffer_size - 1;
60  replace_len = strlen(replace);
61  template_len = strlen(buffer);
62  template = g_strdup(buffer);
63  buffer[0] = '\0';
64 
65  buffer_len = 0;
66  for (i = 0; i <= template_len; i++) {
67  expand = buffer_len + replace_len < replace_limit ? replace_len : 1;
68  if (expand == 1 && buffer_len == replace_limit) {
69  break;
70  }
71  if ((template[i] != find) || ((expand == 1) && (replace_len > 1))) {
72  buffer[buffer_len++] = template[i];
73  buffer[buffer_len] = '\0';
74  } else {
75  strcat(buffer, replace);
76  buffer_len += replace_len;
77  }
78  }
79  free(template);
80 }
81 
85 int make_path_to_file(char *filename) {
86  gchar *dirname = g_path_get_dirname(filename);
87  int result = g_mkdir_with_parents(dirname, 0755);
88  g_free(dirname);
89  return result;
90 }
91 
92 static const char *getLogLevelText(LogLevel level) {
93  const char *LogLevelTexts[] = {
94  "\x1b[34;1m" "DD" "\x1b[0m",
95  "\x1b[32;1m" "II" "\x1b[0m",
96  "\x1b[35;1m" "WW" "\x1b[0m",
97  "\x1b[31;1m" "EE" "\x1b[0m",
98  "\x1b[31;1m" "!!" "\x1b[0m",
99  "\x1b[30;1m" "??" "\x1b[0m",
100  };
101 
102  return LogLevelTexts[level > LOG_CRITICAL ? LOG_CRITICAL + 1 : level];
103 }
104 
109 void LOG(LogLevel level, const char *origin, const char *format, ...) {
110  va_list ap;
111 
112  /* This buffer needs to be very big - larger than any other buffer. */
113  char buf[20480];
114 
115  /* Don't log messages that the user doesn't want. */
116  if (level < MINLOG) {
117  return;
118  }
119 
120  va_start(ap, format);
121  vsnprintf(buf, sizeof(buf), format, ap);
122 
123  if (strlen(buf) > 0) {
124  fprintf(stderr, "[%s] (%s) %s\n", getLogLevelText(level), origin, buf);
125  }
126 
127  va_end(ap);
128 }
Fatal crash-worthy error.
Definition: client.h:445
int make_path_to_file(char *filename)
Definition: misc.c:85
LogLevel
Definition: client.h:440
void replace_chars_with_string(char *buffer, const guint16 buffer_size, const char find, const char *replace)
Definition: misc.c:50
void LOG(LogLevel level, const char *origin, const char *format,...)
Definition: misc.c:109
LogLevel MINLOG
Definition: misc.c:32
static const char * getLogLevelText(LogLevel level)
Definition: misc.c:92
Minor, non-harmful issues.
Definition: client.h:442