OpenTTD Source  1.11.0-beta2
debug.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
10 #include "stdafx.h"
11 #include <stdarg.h>
12 #include "console_func.h"
13 #include "debug.h"
14 #include "string_func.h"
15 #include "fileio_func.h"
16 #include "settings_type.h"
17 
18 #if defined(_WIN32)
19 #include "os/windows/win32.h"
20 #endif
21 
22 #include <time.h>
23 
24 #include "network/network_admin.h"
25 SOCKET _debug_socket = INVALID_SOCKET;
26 
27 #include "safeguards.h"
28 
29 int _debug_driver_level;
30 int _debug_grf_level;
31 int _debug_map_level;
32 int _debug_misc_level;
33 int _debug_net_level;
34 int _debug_sprite_level;
35 int _debug_oldloader_level;
36 int _debug_npf_level;
37 int _debug_yapf_level;
38 int _debug_freetype_level;
39 int _debug_script_level;
40 int _debug_sl_level;
41 int _debug_gamelog_level;
42 int _debug_desync_level;
43 int _debug_console_level;
44 #ifdef RANDOM_DEBUG
45 int _debug_random_level;
46 #endif
47 
48 struct DebugLevel {
49  const char *name;
50  int *level;
51 };
52 
53 #define DEBUG_LEVEL(x) { #x, &_debug_##x##_level }
54  static const DebugLevel debug_level[] = {
55  DEBUG_LEVEL(driver),
56  DEBUG_LEVEL(grf),
57  DEBUG_LEVEL(map),
58  DEBUG_LEVEL(misc),
59  DEBUG_LEVEL(net),
60  DEBUG_LEVEL(sprite),
61  DEBUG_LEVEL(oldloader),
62  DEBUG_LEVEL(npf),
63  DEBUG_LEVEL(yapf),
64  DEBUG_LEVEL(freetype),
65  DEBUG_LEVEL(script),
66  DEBUG_LEVEL(sl),
67  DEBUG_LEVEL(gamelog),
68  DEBUG_LEVEL(desync),
69  DEBUG_LEVEL(console),
70 #ifdef RANDOM_DEBUG
71  DEBUG_LEVEL(random),
72 #endif
73  };
74 #undef DEBUG_LEVEL
75 
82 char *DumpDebugFacilityNames(char *buf, char *last)
83 {
84  size_t length = 0;
85  for (const DebugLevel *i = debug_level; i != endof(debug_level); ++i) {
86  if (length == 0) {
87  buf = strecpy(buf, "List of debug facility names:\n", last);
88  } else {
89  buf = strecpy(buf, ", ", last);
90  length += 2;
91  }
92  buf = strecpy(buf, i->name, last);
93  length += strlen(i->name);
94  }
95  if (length > 0) {
96  buf = strecpy(buf, "\n\n", last);
97  }
98  return buf;
99 }
100 
106 static void debug_print(const char *dbg, const char *buf)
107 {
108  if (_debug_socket != INVALID_SOCKET) {
109  char buf2[1024 + 32];
110 
111  seprintf(buf2, lastof(buf2), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
112  /* Sending out an error when this fails would be nice, however... the error
113  * would have to be send over this failing socket which won't work. */
114  send(_debug_socket, buf2, (int)strlen(buf2), 0);
115  return;
116  }
117  if (strcmp(dbg, "desync") == 0) {
118  static FILE *f = FioFOpenFile("commands-out.log", "wb", AUTOSAVE_DIR);
119  if (f == nullptr) return;
120 
121  fprintf(f, "%s%s\n", GetLogPrefix(), buf);
122  fflush(f);
123 #ifdef RANDOM_DEBUG
124  } else if (strcmp(dbg, "random") == 0) {
125  static FILE *f = FioFOpenFile("random-out.log", "wb", AUTOSAVE_DIR);
126  if (f == nullptr) return;
127 
128  fprintf(f, "%s\n", buf);
129  fflush(f);
130 #endif
131  } else {
132  char buffer[512];
133  seprintf(buffer, lastof(buffer), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
134 #if defined(_WIN32)
135  wchar_t system_buf[512];
136  convert_to_fs(buffer, system_buf, lengthof(system_buf), true);
137  _fputts(system_buf, stderr);
138 #else
139  fputs(buffer, stderr);
140 #endif
141  NetworkAdminConsole(dbg, buf);
142  IConsoleDebug(dbg, buf);
143  }
144 }
145 
152 void CDECL debug(const char *dbg, const char *format, ...)
153 {
154  char buf[1024];
155 
156  va_list va;
157  va_start(va, format);
158  vseprintf(buf, lastof(buf), format, va);
159  va_end(va);
160 
161  debug_print(dbg, buf);
162 }
163 
170 void SetDebugString(const char *s)
171 {
172  int v;
173  char *end;
174  const char *t;
175 
176  /* global debugging level? */
177  if (*s >= '0' && *s <= '9') {
178  const DebugLevel *i;
179 
180  v = strtoul(s, &end, 0);
181  s = end;
182 
183  for (i = debug_level; i != endof(debug_level); ++i) *i->level = v;
184  }
185 
186  /* individual levels */
187  for (;;) {
188  const DebugLevel *i;
189  int *p;
190 
191  /* skip delimiters */
192  while (*s == ' ' || *s == ',' || *s == '\t') s++;
193  if (*s == '\0') break;
194 
195  t = s;
196  while (*s >= 'a' && *s <= 'z') s++;
197 
198  /* check debugging levels */
199  p = nullptr;
200  for (i = debug_level; i != endof(debug_level); ++i) {
201  if (s == t + strlen(i->name) && strncmp(t, i->name, s - t) == 0) {
202  p = i->level;
203  break;
204  }
205  }
206 
207  if (*s == '=') s++;
208  v = strtoul(s, &end, 0);
209  s = end;
210  if (p != nullptr) {
211  *p = v;
212  } else {
213  ShowInfoF("Unknown debug level '%.*s'", (int)(s - t), t);
214  return;
215  }
216  }
217 }
218 
224 const char *GetDebugString()
225 {
226  const DebugLevel *i;
227  static char dbgstr[150];
228  char dbgval[20];
229 
230  memset(dbgstr, 0, sizeof(dbgstr));
231  i = debug_level;
232  seprintf(dbgstr, lastof(dbgstr), "%s=%d", i->name, *i->level);
233 
234  for (i++; i != endof(debug_level); i++) {
235  seprintf(dbgval, lastof(dbgval), ", %s=%d", i->name, *i->level);
236  strecat(dbgstr, dbgval, lastof(dbgstr));
237  }
238 
239  return dbgstr;
240 }
241 
247 const char *GetLogPrefix()
248 {
249  static char _log_prefix[24];
251  time_t cur_time = time(nullptr);
252  strftime(_log_prefix, sizeof(_log_prefix), "[%Y-%m-%d %H:%M:%S] ", localtime(&cur_time));
253  } else {
254  *_log_prefix = '\0';
255  }
256  return _log_prefix;
257 }
258 
SetDebugString
void SetDebugString(const char *s)
Set debugging levels by parsing the text in s.
Definition: debug.cpp:170
win32.h
fileio_func.h
AUTOSAVE_DIR
@ AUTOSAVE_DIR
Subdirectory of save for autosaves.
Definition: fileio_type.h:111
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:79
NetworkAdminConsole
void NetworkAdminConsole(const char *origin, const char *string)
Send console to the admin network (if they did opt in for the respective update).
Definition: network_admin.cpp:948
GUISettings::show_date_in_logs
bool show_date_in_logs
whether to show dates in console logs
Definition: settings_type.h:164
DumpDebugFacilityNames
char * DumpDebugFacilityNames(char *buf, char *last)
Dump the available debug facility names in the help text.
Definition: debug.cpp:82
GetLogPrefix
const char * GetLogPrefix()
Get the prefix for logs; if show_date_in_logs is enabled it returns the date, otherwise it returns no...
Definition: debug.cpp:247
debug
void CDECL debug(const char *dbg, const char *format,...)
Output a debug line.
Definition: debug.cpp:152
FioFOpenFile
FILE * FioFOpenFile(const std::string &filename, const char *mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
Definition: fileio.cpp:406
convert_to_fs
wchar_t * convert_to_fs(const char *name, wchar_t *system_buf, size_t buflen, bool console_cp)
Convert from OpenTTD's encoding to that of the environment in UNICODE.
Definition: win32.cpp:617
safeguards.h
settings_type.h
vseprintf
int CDECL vseprintf(char *str, const char *last, const char *format, va_list ap)
Safer implementation of vsnprintf; same as vsnprintf except:
Definition: string.cpp:61
stdafx.h
string_func.h
endof
#define endof(x)
Get the end element of an fixed size array.
Definition: stdafx.h:375
GetDebugString
const char * GetDebugString()
Print out the current debug-level.
Definition: debug.cpp:224
ShowInfoF
void CDECL ShowInfoF(const char *str,...)
Shows some information on the console/a popup box depending on the OS.
Definition: openttd.cpp:151
seprintf
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:442
IConsoleDebug
void IConsoleDebug(const char *dbg, const char *string)
It is possible to print debugging information to the console, which is achieved by using this functio...
Definition: console.cpp:147
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:367
DebugLevel
Definition: debug.cpp:48
network_admin.h
console_func.h
strecpy
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: string.cpp:112
strecat
char * strecat(char *dst, const char *src, const char *last)
Appends characters from one string to another.
Definition: string.cpp:84
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:383
ClientSettings::gui
GUISettings gui
settings related to the GUI
Definition: settings_type.h:567
debug.h
debug_print
static void debug_print(const char *dbg, const char *buf)
Internal function for outputting the debug line.
Definition: debug.cpp:106