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 #if defined(ENABLE_NETWORK)
27 #include "network/network_admin.h"
28 SOCKET _debug_socket = INVALID_SOCKET;
29 #endif /* ENABLE_NETWORK */
30 
31 #include "safeguards.h"
32 
33 int _debug_driver_level;
34 int _debug_grf_level;
35 int _debug_map_level;
36 int _debug_misc_level;
37 int _debug_net_level;
38 int _debug_sprite_level;
39 int _debug_oldloader_level;
40 int _debug_npf_level;
41 int _debug_yapf_level;
42 int _debug_freetype_level;
43 int _debug_script_level;
44 int _debug_sl_level;
45 int _debug_gamelog_level;
46 int _debug_desync_level;
47 int _debug_console_level;
48 #ifdef RANDOM_DEBUG
49 int _debug_random_level;
50 #endif
51 
52 uint32 _realtime_tick = 0;
53 
54 struct DebugLevel {
55  const char *name;
56  int *level;
57 };
58 
59 #define DEBUG_LEVEL(x) { #x, &_debug_##x##_level }
60  static const DebugLevel debug_level[] = {
61  DEBUG_LEVEL(driver),
62  DEBUG_LEVEL(grf),
63  DEBUG_LEVEL(map),
64  DEBUG_LEVEL(misc),
65  DEBUG_LEVEL(net),
66  DEBUG_LEVEL(sprite),
67  DEBUG_LEVEL(oldloader),
68  DEBUG_LEVEL(npf),
69  DEBUG_LEVEL(yapf),
70  DEBUG_LEVEL(freetype),
71  DEBUG_LEVEL(script),
72  DEBUG_LEVEL(sl),
73  DEBUG_LEVEL(gamelog),
74  DEBUG_LEVEL(desync),
75  DEBUG_LEVEL(console),
76 #ifdef RANDOM_DEBUG
77  DEBUG_LEVEL(random),
78 #endif
79  };
80 #undef DEBUG_LEVEL
81 
88 char *DumpDebugFacilityNames(char *buf, char *last)
89 {
90  size_t length = 0;
91  for (const DebugLevel *i = debug_level; i != endof(debug_level); ++i) {
92  if (length == 0) {
93  buf = strecpy(buf, "List of debug facility names:\n", last);
94  } else {
95  buf = strecpy(buf, ", ", last);
96  length += 2;
97  }
98  buf = strecpy(buf, i->name, last);
99  length += strlen(i->name);
100  }
101  if (length > 0) {
102  buf = strecpy(buf, "\n\n", last);
103  }
104  return buf;
105 }
106 
112 static void debug_print(const char *dbg, const char *buf)
113 {
114 #if defined(ENABLE_NETWORK)
115  if (_debug_socket != INVALID_SOCKET) {
116  char buf2[1024 + 32];
117 
118  seprintf(buf2, lastof(buf2), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
119  /* Sending out an error when this fails would be nice, however... the error
120  * would have to be send over this failing socket which won't work. */
121  send(_debug_socket, buf2, (int)strlen(buf2), 0);
122  return;
123  }
124 #endif /* ENABLE_NETWORK */
125  if (strcmp(dbg, "desync") == 0) {
126  static FILE *f = FioFOpenFile("commands-out.log", "wb", AUTOSAVE_DIR);
127  if (f == NULL) return;
128 
129  fprintf(f, "%s%s\n", GetLogPrefix(), buf);
130  fflush(f);
131 #ifdef RANDOM_DEBUG
132  } else if (strcmp(dbg, "random") == 0) {
133  static FILE *f = FioFOpenFile("random-out.log", "wb", AUTOSAVE_DIR);
134  if (f == NULL) return;
135 
136  fprintf(f, "%s\n", buf);
137  fflush(f);
138 #endif
139  } else {
140  char buffer[512];
141  seprintf(buffer, lastof(buffer), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
142 #if defined(_WIN32)
143  TCHAR system_buf[512];
144  convert_to_fs(buffer, system_buf, lengthof(system_buf), true);
145  _fputts(system_buf, stderr);
146 #else
147  fputs(buffer, stderr);
148 #endif
149 #ifdef ENABLE_NETWORK
150  NetworkAdminConsole(dbg, buf);
151 #endif /* ENABLE_NETWORK */
152  IConsoleDebug(dbg, buf);
153  }
154 }
155 
162 void CDECL debug(const char *dbg, const char *format, ...)
163 {
164  char buf[1024];
165 
166  va_list va;
167  va_start(va, format);
168  vseprintf(buf, lastof(buf), format, va);
169  va_end(va);
170 
171  debug_print(dbg, buf);
172 }
173 
180 void SetDebugString(const char *s)
181 {
182  int v;
183  char *end;
184  const char *t;
185 
186  /* global debugging level? */
187  if (*s >= '0' && *s <= '9') {
188  const DebugLevel *i;
189 
190  v = strtoul(s, &end, 0);
191  s = end;
192 
193  for (i = debug_level; i != endof(debug_level); ++i) *i->level = v;
194  }
195 
196  /* individual levels */
197  for (;;) {
198  const DebugLevel *i;
199  int *p;
200 
201  /* skip delimiters */
202  while (*s == ' ' || *s == ',' || *s == '\t') s++;
203  if (*s == '\0') break;
204 
205  t = s;
206  while (*s >= 'a' && *s <= 'z') s++;
207 
208  /* check debugging levels */
209  p = NULL;
210  for (i = debug_level; i != endof(debug_level); ++i) {
211  if (s == t + strlen(i->name) && strncmp(t, i->name, s - t) == 0) {
212  p = i->level;
213  break;
214  }
215  }
216 
217  if (*s == '=') s++;
218  v = strtoul(s, &end, 0);
219  s = end;
220  if (p != NULL) {
221  *p = v;
222  } else {
223  ShowInfoF("Unknown debug level '%.*s'", (int)(s - t), t);
224  return;
225  }
226  }
227 }
228 
234 const char *GetDebugString()
235 {
236  const DebugLevel *i;
237  static char dbgstr[150];
238  char dbgval[20];
239 
240  memset(dbgstr, 0, sizeof(dbgstr));
241  i = debug_level;
242  seprintf(dbgstr, lastof(dbgstr), "%s=%d", i->name, *i->level);
243 
244  for (i++; i != endof(debug_level); i++) {
245  seprintf(dbgval, lastof(dbgval), ", %s=%d", i->name, *i->level);
246  strecat(dbgstr, dbgval, lastof(dbgstr));
247  }
248 
249  return dbgstr;
250 }
251 
257 const char *GetLogPrefix()
258 {
259  static char _log_prefix[24];
261  time_t cur_time = time(NULL);
262  strftime(_log_prefix, sizeof(_log_prefix), "[%Y-%m-%d %H:%M:%S] ", localtime(&cur_time));
263  } else {
264  *_log_prefix = '\0';
265  }
266  return _log_prefix;
267 }
268 
static void debug_print(const char *dbg, const char *buf)
Internal function for outputting the debug line.
Definition: debug.cpp:112
uint32 _realtime_tick
The real time in the game.
Definition: debug.cpp:52
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:632
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:162
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:154
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:257
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:76
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:88
const char * GetDebugString()
Print out the current debug-level.
Definition: debug.cpp:234
#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:412
void SetDebugString(const char *s)
Set debugging levels by parsing the text in s.
Definition: debug.cpp:180
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.