Fix some daylength issues, possible division by zero in main menu.
[openttd-joker.git] / src / debug.cpp
blob832d6bd2fbd2ae1154e44c2397b06ad67c45a20a
1 /* $Id: debug.cpp 26058 2013-11-23 13:15:07Z rubidium $ */
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 */
10 /** @file debug.cpp Handling of printing debug messages. */
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"
20 #include <time.h>
22 #if defined(ENABLE_NETWORK)
23 #include "network/network_admin.h"
24 SOCKET _debug_socket = INVALID_SOCKET;
25 #endif /* ENABLE_NETWORK */
27 #include "safeguards.h"
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 int _debug_sound_level;
45 #ifdef RANDOM_DEBUG
46 int _debug_random_level;
47 #endif
49 uint32 _realtime_tick = 0;
51 struct DebugLevel {
52 const char *name;
53 int *level;
56 #define DEBUG_LEVEL(x) { #x, &_debug_##x##_level }
57 static const DebugLevel debug_level[] = {
58 DEBUG_LEVEL(driver),
59 DEBUG_LEVEL(grf),
60 DEBUG_LEVEL(map),
61 DEBUG_LEVEL(misc),
62 DEBUG_LEVEL(net),
63 DEBUG_LEVEL(sprite),
64 DEBUG_LEVEL(oldloader),
65 DEBUG_LEVEL(npf),
66 DEBUG_LEVEL(yapf),
67 DEBUG_LEVEL(freetype),
68 DEBUG_LEVEL(script),
69 DEBUG_LEVEL(sl),
70 DEBUG_LEVEL(gamelog),
71 DEBUG_LEVEL(desync),
72 DEBUG_LEVEL(console),
73 DEBUG_LEVEL(sound),
74 #ifdef RANDOM_DEBUG
75 DEBUG_LEVEL(random),
76 #endif
78 #undef DEBUG_LEVEL
80 /**
81 * Dump the available debug facility names in the help text.
82 * @param buf Start address for storing the output.
83 * @param last Last valid address for storing the output.
84 * @return Next free position in the output.
86 char *DumpDebugFacilityNames(char *buf, char *last)
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;
96 buf = strecpy(buf, i->name, last);
97 length += strlen(i->name);
99 if (length > 0) {
100 buf = strecpy(buf, "\n\n", last);
102 return buf;
105 #if !defined(NO_DEBUG_MESSAGES)
108 * Internal function for outputting the debug line.
109 * @param dbg Debug category.
110 * @param buf Text line to output.
112 static void debug_print(const char *dbg, const char *buf)
114 #if defined(ENABLE_NETWORK)
115 if (_debug_socket != INVALID_SOCKET) {
116 char buf2[1024 + 32];
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;
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;
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;
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(WINCE)
143 NKDbgPrintfW(OTTD2FS(buffer));
144 #elif defined(WIN32) || defined(WIN64)
145 _fputts(OTTD2FS(buffer, true), 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);
157 * Output a debug line.
158 * @note Do not call directly, use the #DEBUG macro instead.
159 * @param dbg Debug category.
160 * @param format Text string a la printf, with optional arguments.
162 void CDECL debug(const char *dbg, const char *format, ...)
164 char buf[1024];
166 va_list va;
167 va_start(va, format);
168 vseprintf(buf, lastof(buf), format, va);
169 va_end(va);
171 debug_print(dbg, buf);
173 #endif /* NO_DEBUG_MESSAGES */
176 * Set debugging levels by parsing the text in \a s.
177 * For setting individual levels a string like \c "net=3,grf=6" should be used.
178 * If the string starts with a number, the number is used as global debugging level.
179 * @param s Text describing the wanted debugging levels.
181 void SetDebugString(const char *s)
183 int v;
184 char *end;
185 const char *t;
187 /* global debugging level? */
188 if (*s >= '0' && *s <= '9') {
189 const DebugLevel *i;
191 v = strtoul(s, &end, 0);
192 s = end;
194 for (i = debug_level; i != endof(debug_level); ++i) *i->level = v;
197 /* individual levels */
198 for (;;) {
199 const DebugLevel *i;
200 int *p;
202 /* skip delimiters */
203 while (*s == ' ' || *s == ',' || *s == '\t') s++;
204 if (*s == '\0') break;
206 t = s;
207 while (*s >= 'a' && *s <= 'z') s++;
209 /* check debugging levels */
210 p = NULL;
211 for (i = debug_level; i != endof(debug_level); ++i) {
212 if (s == t + strlen(i->name) && strncmp(t, i->name, s - t) == 0) {
213 p = i->level;
214 break;
218 if (*s == '=') s++;
219 v = strtoul(s, &end, 0);
220 s = end;
221 if (p != NULL) {
222 *p = v;
223 } else {
224 ShowInfoF("Unknown debug level '%.*s'", (int)(s - t), t);
225 return;
231 * Print out the current debug-level.
232 * Just return a string with the values of all the debug categories.
233 * @return string with debug-levels
235 const char *GetDebugString()
237 const DebugLevel *i;
238 static char dbgstr[150];
239 char dbgval[20];
241 memset(dbgstr, 0, sizeof(dbgstr));
242 i = debug_level;
243 seprintf(dbgstr, lastof(dbgstr), "%s=%d", i->name, *i->level);
245 for (i++; i != endof(debug_level); i++) {
246 seprintf(dbgval, lastof(dbgval), ", %s=%d", i->name, *i->level);
247 strecat(dbgstr, dbgval, lastof(dbgstr));
250 return dbgstr;
254 * Get the prefix for logs; if show_date_in_logs is enabled it returns
255 * the date, otherwise it returns nothing.
256 * @return the prefix for logs (do not free), never NULL
258 const char *GetLogPrefix()
260 static char _log_prefix[24];
261 if (_settings_client.gui.show_date_in_logs) {
262 time_t cur_time = time(NULL);
263 strftime(_log_prefix, sizeof(_log_prefix), "[%Y-%m-%d %H:%M:%S] ", localtime(&cur_time));
264 } else {
265 *_log_prefix = '\0';
267 return _log_prefix;