OpenTTD
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 uint32 _realtime_tick = 0;
49 
50 struct DebugLevel {
51  const char *name;
52  int *level;
53 };
54 
55 #define DEBUG_LEVEL(x) { #x, &_debug_##x##_level }
56  static const DebugLevel debug_level[] = {
57  DEBUG_LEVEL(driver),
58  DEBUG_LEVEL(grf),
59  DEBUG_LEVEL(map),
60  DEBUG_LEVEL(misc),
61  DEBUG_LEVEL(net),
62  DEBUG_LEVEL(sprite),
63  DEBUG_LEVEL(oldloader),
64  DEBUG_LEVEL(npf),
65  DEBUG_LEVEL(yapf),
66  DEBUG_LEVEL(freetype),
67  DEBUG_LEVEL(script),
68  DEBUG_LEVEL(sl),
69  DEBUG_LEVEL(gamelog),
70  DEBUG_LEVEL(desync),
71  DEBUG_LEVEL(console),
72 #ifdef RANDOM_DEBUG
73  DEBUG_LEVEL(random),
74 #endif
75  };
76 #undef DEBUG_LEVEL
77 
84 char *DumpDebugFacilityNames(char *buf, char *last)
85 {
86  size_t length = 0;
87  for (const DebugLevel *i = debug_level; i != endof(debug_level); ++i) {
88  if (length == 0) {
89  buf = strecpy(buf, "List of debug facility names:\n", last);
90  } else {
91  buf = strecpy(buf, ", ", last);
92  length += 2;
93  }
94  buf = strecpy(buf, i->name, last);
95  length += strlen(i->name);
96  }
97  if (length > 0) {
98  buf = strecpy(buf, "\n\n", last);
99  }
100  return buf;
101 }
102 
108 static void debug_print(const char *dbg, const char *buf)
109 {
110  if (_debug_socket != INVALID_SOCKET) {
111  char buf2[1024 + 32];
112 
113  seprintf(buf2, lastof(buf2), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
114  /* Sending out an error when this fails would be nice, however... the error
115  * would have to be send over this failing socket which won't work. */
116  send(_debug_socket, buf2, (int)strlen(buf2), 0);
117  return;
118  }
119  if (strcmp(dbg, "desync") == 0) {
120  static FILE *f = FioFOpenFile("commands-out.log", "wb", AUTOSAVE_DIR);
121  if (f == nullptr) return;
122 
123  fprintf(f, "%s%s\n", GetLogPrefix(), buf);
124  fflush(f);
125 #ifdef RANDOM_DEBUG
126  } else if (strcmp(dbg, "random") == 0) {
127  static FILE *f = FioFOpenFile("random-out.log", "wb", AUTOSAVE_DIR);
128  if (f == nullptr) return;
129 
130  fprintf(f, "%s\n", buf);
131  fflush(f);
132 #endif
133  } else {
134  char buffer[512];
135  seprintf(buffer, lastof(buffer), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
136 #if defined(_WIN32)
137  TCHAR system_buf[512];
138  convert_to_fs(buffer, system_buf, lengthof(system_buf), true);
139  _fputts(system_buf, stderr);
140 #else
141  fputs(buffer, stderr);
142 #endif
143  NetworkAdminConsole(dbg, buf);
144  IConsoleDebug(dbg, buf);
145  }
146 }
147 
154 void CDECL debug(const char *dbg, const char *format, ...)
155 {
156  char buf[1024];
157 
158  va_list va;
159  va_start(va, format);
160  vseprintf(buf, lastof(buf), format, va);
161  va_end(va);
162 
163  debug_print(dbg, buf);
164 }
165 
172 void SetDebugString(const char *s)
173 {
174  int v;
175  char *end;
176  const char *t;
177 
178  /* global debugging level? */
179  if (*s >= '0' && *s <= '9') {
180  const DebugLevel *i;
181 
182  v = strtoul(s, &end, 0);
183  s = end;
184 
185  for (i = debug_level; i != endof(debug_level); ++i) *i->level = v;
186  }
187 
188  /* individual levels */
189  for (;;) {
190  const DebugLevel *i;
191  int *p;
192 
193  /* skip delimiters */
194  while (*s == ' ' || *s == ',' || *s == '\t') s++;
195  if (*s == '\0') break;
196 
197  t = s;
198  while (*s >= 'a' && *s <= 'z') s++;
199 
200  /* check debugging levels */
201  p = nullptr;
202  for (i = debug_level; i != endof(debug_level); ++i) {
203  if (s == t + strlen(i->name) && strncmp(t, i->name, s - t) == 0) {
204  p = i->level;
205  break;
206  }
207  }
208 
209  if (*s == '=') s++;
210  v = strtoul(s, &end, 0);
211  s = end;
212  if (p != nullptr) {
213  *p = v;
214  } else {
215  ShowInfoF("Unknown debug level '%.*s'", (int)(s - t), t);
216  return;
217  }
218  }
219 }
220 
226 const char *GetDebugString()
227 {
228  const DebugLevel *i;
229  static char dbgstr[150];
230  char dbgval[20];
231 
232  memset(dbgstr, 0, sizeof(dbgstr));
233  i = debug_level;
234  seprintf(dbgstr, lastof(dbgstr), "%s=%d", i->name, *i->level);
235 
236  for (i++; i != endof(debug_level); i++) {
237  seprintf(dbgval, lastof(dbgval), ", %s=%d", i->name, *i->level);
238  strecat(dbgstr, dbgval, lastof(dbgstr));
239  }
240 
241  return dbgstr;
242 }
243 
249 const char *GetLogPrefix()
250 {
251  static char _log_prefix[24];
253  time_t cur_time = time(nullptr);
254  strftime(_log_prefix, sizeof(_log_prefix), "[%Y-%m-%d %H:%M:%S] ", localtime(&cur_time));
255  } else {
256  *_log_prefix = '\0';
257  }
258  return _log_prefix;
259 }
260 
static void debug_print(const char *dbg, const char *buf)
Internal function for outputting the debug line.
Definition: debug.cpp:108
uint32 _realtime_tick
The real time in the game.
Definition: debug.cpp:48
static char * strecat(char *dst, const char *src, const char *last)
Appends characters from one string to another.
Definition: depend.cpp:97
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:625
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:407
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:60
Functions for Standard In/Out file operations.
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:48
void CDECL debug(const char *dbg, const char *format,...)
Output a debug line.
Definition: debug.cpp:154
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:146
void CDECL ShowInfoF(const char *str,...)
Shows some information on the console/a popup box depending on the OS.
Definition: openttd.cpp:134
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:249
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:78
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:463
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:111
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:84
const char * GetDebugString()
Print out the current debug-level.
Definition: debug.cpp:226
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:40
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:66
#define endof(x)
Get the end element of an fixed size array.
Definition: stdafx.h:384
void SetDebugString(const char *s)
Set debugging levels by parsing the text in s.
Definition: debug.cpp:172
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.