OpenTTD
debug.cpp
Go to the documentation of this file.
1 /* $Id: debug.cpp 26506 2014-04-24 19:51:45Z rubidium $ */
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 #include <time.h>
21 
22 #if defined(ENABLE_NETWORK)
23 #include "network/network_admin.h"
24 SOCKET _debug_socket = INVALID_SOCKET;
25 #endif /* ENABLE_NETWORK */
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 
103 #if !defined(NO_DEBUG_MESSAGES)
104 
110 static void debug_print(const char *dbg, const char *buf)
111 {
112 #if defined(ENABLE_NETWORK)
113  if (_debug_socket != INVALID_SOCKET) {
114  char buf2[1024 + 32];
115 
116  seprintf(buf2, lastof(buf2), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
117  /* Sending out an error when this fails would be nice, however... the error
118  * would have to be send over this failing socket which won't work. */
119  send(_debug_socket, buf2, (int)strlen(buf2), 0);
120  return;
121  }
122 #endif /* ENABLE_NETWORK */
123  if (strcmp(dbg, "desync") == 0) {
124  static FILE *f = FioFOpenFile("commands-out.log", "wb", AUTOSAVE_DIR);
125  if (f == NULL) return;
126 
127  fprintf(f, "%s%s\n", GetLogPrefix(), buf);
128  fflush(f);
129 #ifdef RANDOM_DEBUG
130  } else if (strcmp(dbg, "random") == 0) {
131  static FILE *f = FioFOpenFile("random-out.log", "wb", AUTOSAVE_DIR);
132  if (f == NULL) return;
133 
134  fprintf(f, "%s\n", buf);
135  fflush(f);
136 #endif
137  } else {
138  char buffer[512];
139  seprintf(buffer, lastof(buffer), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
140 #if defined(WINCE)
141  NKDbgPrintfW(OTTD2FS(buffer));
142 #elif defined(WIN32) || defined(WIN64)
143  _fputts(OTTD2FS(buffer, true), stderr);
144 #else
145  fputs(buffer, stderr);
146 #endif
147 #ifdef ENABLE_NETWORK
148  NetworkAdminConsole(dbg, buf);
149 #endif /* ENABLE_NETWORK */
150  IConsoleDebug(dbg, buf);
151  }
152 }
153 
160 void CDECL debug(const char *dbg, const char *format, ...)
161 {
162  char buf[1024];
163 
164  va_list va;
165  va_start(va, format);
166  vseprintf(buf, lastof(buf), format, va);
167  va_end(va);
168 
169  debug_print(dbg, buf);
170 }
171 #endif /* NO_DEBUG_MESSAGES */
172 
179 void SetDebugString(const char *s)
180 {
181  int v;
182  char *end;
183  const char *t;
184 
185  /* global debugging level? */
186  if (*s >= '0' && *s <= '9') {
187  const DebugLevel *i;
188 
189  v = strtoul(s, &end, 0);
190  s = end;
191 
192  for (i = debug_level; i != endof(debug_level); ++i) *i->level = v;
193  }
194 
195  /* individual levels */
196  for (;;) {
197  const DebugLevel *i;
198  int *p;
199 
200  /* skip delimiters */
201  while (*s == ' ' || *s == ',' || *s == '\t') s++;
202  if (*s == '\0') break;
203 
204  t = s;
205  while (*s >= 'a' && *s <= 'z') s++;
206 
207  /* check debugging levels */
208  p = NULL;
209  for (i = debug_level; i != endof(debug_level); ++i) {
210  if (s == t + strlen(i->name) && strncmp(t, i->name, s - t) == 0) {
211  p = i->level;
212  break;
213  }
214  }
215 
216  if (*s == '=') s++;
217  v = strtoul(s, &end, 0);
218  s = end;
219  if (p != NULL) {
220  *p = v;
221  } else {
222  ShowInfoF("Unknown debug level '%.*s'", (int)(s - t), t);
223  return;
224  }
225  }
226 }
227 
233 const char *GetDebugString()
234 {
235  const DebugLevel *i;
236  static char dbgstr[150];
237  char dbgval[20];
238 
239  memset(dbgstr, 0, sizeof(dbgstr));
240  i = debug_level;
241  seprintf(dbgstr, lastof(dbgstr), "%s=%d", i->name, *i->level);
242 
243  for (i++; i != endof(debug_level); i++) {
244  seprintf(dbgval, lastof(dbgval), ", %s=%d", i->name, *i->level);
245  strecat(dbgstr, dbgval, lastof(dbgstr));
246  }
247 
248  return dbgstr;
249 }
250 
256 const char *GetLogPrefix()
257 {
258  static char _log_prefix[24];
260  time_t cur_time = time(NULL);
261  strftime(_log_prefix, sizeof(_log_prefix), "[%Y-%m-%d %H:%M:%S] ", localtime(&cur_time));
262  } else {
263  *_log_prefix = '\0';
264  }
265  return _log_prefix;
266 }
267 
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:48
static char * strecat(char *dst, const char *src, const char *last)
Appends characters from one string to another.
Definition: depend.cpp:99
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:398
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:50
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:160
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:130
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:256
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:474
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:84
const char * GetDebugString()
Print out the current debug-level.
Definition: debug.cpp:233
const TCHAR * OTTD2FS(const char *name, bool console_cp)
Convert from OpenTTD&#39;s encoding to that of the local environment.
Definition: win32.cpp:631
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:427
void SetDebugString(const char *s)
Set debugging levels by parsing the text in s.
Definition: debug.cpp:179
bool show_date_in_logs
whether to show dates in console logs
Server part of the admin network protocol.