Crossfire Client, Trunk
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 
31 GTimer* global_time;
32 
35 
52 void replace_chars_with_string(char* buffer,
53  const guint16 buffer_size,
54  const char find,
55  const char* replace )
56 {
57 
58  guint16 buffer_len, expand, i, replace_len, replace_limit, template_len;
59  char* template;
60 
61  replace_limit = buffer_size - 1;
62  replace_len = strlen(replace);
63  template_len = strlen(buffer);
64  template = g_strdup(buffer);
65  buffer[0] = '\0';
66 
67  buffer_len = 0;
68  for (i = 0; i <= template_len; i++) {
69  expand = buffer_len + replace_len < replace_limit ? replace_len : 1;
70  if (expand == 1 && buffer_len == replace_limit) {
71  break;
72  }
73  if ((template[i] != find) || ((expand == 1) && (replace_len > 1))) {
74  buffer[buffer_len++] = template[i];
75  buffer[buffer_len] = '\0';
76  } else {
77  strcat(buffer, replace);
78  buffer_len += replace_len;
79  }
80  }
81  free(template);
82 }
83 
87 int make_path_to_file(char *filename) {
88  gchar *dirname = g_path_get_dirname(filename);
89  int result = g_mkdir_with_parents(dirname, 0755);
90  g_free(dirname);
91  return result;
92 }
93 
94 static const char *getLogLevelText(LogLevel level) {
95  const char *LogLevelTexts[] = {
96  "\x1b[34;1m" "DD" "\x1b[0m",
97  "\x1b[32;1m" "II" "\x1b[0m",
98  "\x1b[35;1m" "WW" "\x1b[0m",
99  "\x1b[31;1m" "EE" "\x1b[0m",
100  "\x1b[31;1m" "!!" "\x1b[0m",
101  "\x1b[30;1m" "??" "\x1b[0m",
102  };
103 
104  return LogLevelTexts[level > LOG_CRITICAL ? LOG_CRITICAL + 1 : level];
105 }
106 
111 void LOG(LogLevel level, const char *origin, const char *format, ...) {
112  va_list ap;
113 
114  /* This buffer needs to be very big - larger than any other buffer. */
115  char buf[20480];
116 
117  /* Don't log messages that the user doesn't want. */
118  if (level < MINLOG) {
119  return;
120  }
121 
122  va_start(ap, format);
123  vsnprintf(buf, sizeof(buf), format, ap);
124 
125  if (strlen(buf) > 0) {
126  fprintf(stderr, "[%s] %lf (%s) %s\n", getLogLevelText(level), g_timer_elapsed(global_time, NULL), origin, buf);
127  }
128 
129  va_end(ap);
130 }
LOG_INFO
@ LOG_INFO
Minor, non-harmful issues.
Definition: client.h:434
MINLOG
LogLevel MINLOG
Definition: misc.c:34
global_time
GTimer * global_time
Definition: misc.c:31
replace_chars_with_string
void replace_chars_with_string(char *buffer, const guint16 buffer_size, const char find, const char *replace)
Definition: misc.c:52
LogLevel
LogLevel
Definition: client.h:432
LOG_CRITICAL
@ LOG_CRITICAL
Fatal crash-worthy error.
Definition: client.h:437
LOG
void LOG(LogLevel level, const char *origin, const char *format,...)
Definition: misc.c:111
getLogLevelText
static const char * getLogLevelText(LogLevel level)
Definition: misc.c:94
client.h
make_path_to_file
int make_path_to_file(char *filename)
Definition: misc.c:87