OpenTTD
debug.cpp
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #include "stdafx.h"
13 #include <stdarg.h>
14 #include "console_func.h"
15 #include "debug.h"
16 #include "string_func.h"
17 #include "fileio_func.h"
18 #include "settings_type.h"
19 
20 #if defined(_WIN32)
21 #include "os/windows/win32.h"
22 #endif
23 
24 #include <time.h>
25 
26 #include "network/network_admin.h"
27 SOCKET _debug_socket = INVALID_SOCKET;
28 
29 #include "safeguards.h"
30 
31 int _debug_driver_level;
32 int _debug_grf_level;
33 int _debug_map_level;
34 int _debug_misc_level;
35 int _debug_net_level;
36 int _debug_sprite_level;
37 int _debug_oldloader_level;
38 int _debug_npf_level;
39 int _debug_yapf_level;
40 int _debug_freetype_level;
41 int _debug_script_level;
42 int _debug_sl_level;
43 int _debug_gamelog_level;
44 int _debug_desync_level;
45 int _debug_console_level;
46 #ifdef RANDOM_DEBUG
47 int _debug_random_level;
48 #endif
49 
50 uint32 _realtime_tick = 0;
51 
52 struct DebugLevel {
53  const char *name;
54  int *level;
55 };
56 
57 #define DEBUG_LEVEL(x) { #x, &_debug_##x##_level }
58  static const DebugLevel debug_level[] = {
59  DEBUG_LEVEL(driver),
60  DEBUG_LEVEL(grf),
61  DEBUG_LEVEL(map),
62  DEBUG_LEVEL(misc),
63  DEBUG_LEVEL(net),
64  DEBUG_LEVEL(sprite),
65  DEBUG_LEVEL(oldloader),
66  DEBUG_LEVEL(npf),
67  DEBUG_LEVEL(yapf),
68  DEBUG_LEVEL(freetype),
69  DEBUG_LEVEL(script),
70  DEBUG_LEVEL(sl),
71  DEBUG_LEVEL(gamelog),
72  DEBUG_LEVEL(desync),
73  DEBUG_LEVEL(console),
74 #ifdef RANDOM_DEBUG
75  DEBUG_LEVEL(random),
76 #endif
77  };
78 #undef DEBUG_LEVEL
79 
86 char *DumpDebugFacilityNames(char *buf, char *last)
87 {
88  size_t length = 0;
89  for (const DebugLevel *i = debug_level; i != endof(debug_level); ++i) {
90  if (length == 0) {
91  buf = strecpy(buf, "List of debug facility names:\n", last);
92  } else {
93  buf = strecpy(buf, ", ", last);
94  length += 2;
95  }
96  buf = strecpy(buf, i->name, last);
97  length += strlen(i->name);
98  }
99  if (length > 0) {
100  buf = strecpy(buf, "\n\n", last);
101  }
102  return buf;
103 }
104 
110 static void debug_print(const char *dbg, const char *buf)
111 {
112  if (_debug_socket != INVALID_SOCKET) {
113  char buf2[1024 + 32];
114 
115  seprintf(buf2, lastof(buf2), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
116  /* Sending out an error when this fails would be nice, however... the error
117  * would have to be send over this failing socket which won't work. */
118  send(_debug_socket, buf2, (int)strlen(buf2), 0);
119  return;
120  }
121  if (strcmp(dbg, "desync") == 0) {
122  static FILE *f = FioFOpenFile("commands-out.log", "wb", AUTOSAVE_DIR);
123  if (f == nullptr) return;
124 
125  fprintf(f, "%s%s\n", GetLogPrefix(), buf);
126  fflush(f);
127 #ifdef RANDOM_DEBUG
128  } else if (strcmp(dbg, "random") == 0) {
129  static FILE *f = FioFOpenFile("random-out.log", "wb", AUTOSAVE_DIR);
130  if (f == nullptr) return;
131 
132  fprintf(f, "%s\n", buf);
133  fflush(f);
134 #endif
135  } else {
136  char buffer[512];
137  seprintf(buffer, lastof(buffer), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
138 #if defined(_WIN32)
139  TCHAR system_buf[512];
140  convert_to_fs(buffer, system_buf, lengthof(system_buf), true);
141  _fputts(system_buf, stderr);
142 #else
143  fputs(buffer, stderr);
144 #endif
145  NetworkAdminConsole(dbg, buf);
146  IConsoleDebug(dbg, buf);
147  }
148 }
149 
156 void CDECL debug(const char *dbg, const char *format, ...)
157 {
158  char buf[1024];
159 
160  va_list va;
161  va_start(va, format);
162  vseprintf(buf, lastof(buf), format, va);
163  va_end(va);
164 
165  debug_print(dbg, buf);
166 }
167 
174 void SetDebugString(const char *s)
175 {
176  int v;
177  char *end;
178  const char *t;
179 
180  /* global debugging level? */
181  if (*s >= '0' && *s <= '9') {
182  const DebugLevel *i;
183 
184  v = strtoul(s, &end, 0);
185  s = end;
186 
187  for (i = debug_level; i != endof(debug_level); ++i) *i->level = v;
188  }
189 
190  /* individual levels */
191  for (;;) {
192  const DebugLevel *i;
193  int *p;
194 
195  /* skip delimiters */
196  while (*s == ' ' || *s == ',' || *s == '\t') s++;
197  if (*s == '\0') break;
198 
199  t = s;
200  while (*s >= 'a' && *s <= 'z') s++;
201 
202  /* check debugging levels */
203  p = nullptr;
204  for (i = debug_level; i != endof(debug_level); ++i) {
205  if (s == t + strlen(i->name) && strncmp(t, i->name, s - t) == 0) {
206  p = i->level;
207  break;
208  }
209  }
210 
211  if (*s == '=') s++;
212  v = strtoul(s, &end, 0);
213  s = end;
214  if (p != nullptr) {
215  *p = v;
216  } else {
217  ShowInfoF("Unknown debug level '%.*s'", (int)(s - t), t);
218  return;
219  }
220  }
221 }
222 
228 const char *GetDebugString()
229 {
230  const DebugLevel *i;
231  static char dbgstr[150];
232  char dbgval[20];
233 
234  memset(dbgstr, 0, sizeof(dbgstr));
235  i = debug_level;
236  seprintf(dbgstr, lastof(dbgstr), "%s=%d", i->name, *i->level);
237 
238  for (i++; i != endof(debug_level); i++) {
239  seprintf(dbgval, lastof(dbgval), ", %s=%d", i->name, *i->level);
240  strecat(dbgstr, dbgval, lastof(dbgstr));
241  }
242 
243  return dbgstr;
244 }
245 
251 const char *GetLogPrefix()
252 {
253  static char _log_prefix[24];
255  time_t cur_time = time(nullptr);
256  strftime(_log_prefix, sizeof(_log_prefix), "[%Y-%m-%d %H:%M:%S] ", localtime(&cur_time));
257  } else {
258  *_log_prefix = '\0';
259  }
260  return _log_prefix;
261 }
262 
static void debug_print(const char *dbg, const char *buf)
Internal function for outputting the debug line.
Definition: debug.cpp:110
uint32 _realtime_tick
The real time in the game.
Definition: debug.cpp:50
static char * strecat(char *dst, const char *src, const char *last)
Appends characters from one string to another.
Definition: depend.cpp:99
TCHAR * convert_to_fs(const char *name, TCHAR *system_buf, size_t buflen, bool console_cp)
Convert from OpenTTD&#39;s encoding to that of the environment in UNICODE.
Definition: win32.cpp:627
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:409
Functions related to debugging.
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:62
Functions for Standard In/Out file operations.
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:50
void CDECL debug(const char *dbg, const char *format,...)
Output a debug line.
Definition: debug.cpp:156
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:148
void CDECL ShowInfoF(const char *str,...)
Shows some information on the console/a popup box depending on the OS.
Definition: openttd.cpp:136
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:251
void NetworkAdminConsole(const char *origin, const char *string)
Send console to the admin network (if they did opt in for the respective update). ...
Functions related to low-level strings.
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:80
Types related to global configuration settings.
FILE * FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
Definition: fileio.cpp:465
Definition of base types and functions in a cross-platform compatible way.
A number of safeguards to prevent using unsafe methods.
Subdirectory of save for autosaves.
Definition: fileio_type.h:113
Console functions used outside of the console code.
char * DumpDebugFacilityNames(char *buf, char *last)
Dump the available debug facility names in the help text.
Definition: debug.cpp:86
const char * GetDebugString()
Print out the current debug-level.
Definition: debug.cpp:228
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:42
GUISettings gui
settings related to the GUI
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: depend.cpp:68
#define endof(x)
Get the end element of an fixed size array.
Definition: stdafx.h:386
void SetDebugString(const char *s)
Set debugging levels by parsing the text in s.
Definition: debug.cpp:174
bool show_date_in_logs
whether to show dates in console logs
declarations of functions for MS windows systems
Server part of the admin network protocol.