Let HandleWindowDragging return a boolean status
[openttd/fttd.git] / src / debug.cpp
bloba74d5b0c9c869d6cdf41fc3321cfc2abe22e337e
1 /* $Id$ */
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.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 int _debug_driver_level;
28 int _debug_grf_level;
29 int _debug_map_level;
30 int _debug_misc_level;
31 int _debug_net_level;
32 int _debug_sprite_level;
33 int _debug_oldloader_level;
34 int _debug_yapf_level;
35 int _debug_freetype_level;
36 int _debug_script_level;
37 int _debug_sl_level;
38 int _debug_gamelog_level;
39 int _debug_desync_level;
40 int _debug_console_level;
41 #ifdef RANDOM_DEBUG
42 int _debug_random_level;
43 #endif
45 uint32 _realtime_tick = 0;
47 struct DebugLevel {
48 const char *name;
49 int *level;
52 #define DEBUG_LEVEL(x) { #x, &_debug_##x##_level }
53 static const DebugLevel debug_level[] = {
54 DEBUG_LEVEL(driver),
55 DEBUG_LEVEL(grf),
56 DEBUG_LEVEL(map),
57 DEBUG_LEVEL(misc),
58 DEBUG_LEVEL(net),
59 DEBUG_LEVEL(sprite),
60 DEBUG_LEVEL(oldloader),
61 DEBUG_LEVEL(yapf),
62 DEBUG_LEVEL(freetype),
63 DEBUG_LEVEL(script),
64 DEBUG_LEVEL(sl),
65 DEBUG_LEVEL(gamelog),
66 DEBUG_LEVEL(desync),
67 DEBUG_LEVEL(console),
68 #ifdef RANDOM_DEBUG
69 DEBUG_LEVEL(random),
70 #endif
72 #undef DEBUG_LEVEL
74 /**
75 * Dump the available debug facility names in the help text.
76 * @param buf String where to store the output.
78 void DumpDebugFacilityNames (stringb *buf)
80 /* No debug facilities? */
81 if (debug_level == endof(debug_level)) return;
83 buf->append ("List of debug facility names:\n");
85 const DebugLevel *i = debug_level;
86 for (;;) {
87 buf->append (i->name);
88 if (++i == endof(debug_level)) break;
89 buf->append (", ");
92 buf->append ("\n\n");
95 #if !defined(NO_DEBUG_MESSAGES)
97 /**
98 * Internal function for outputting the debug line.
99 * @param dbg Debug category.
100 * @param buf Text line to output.
102 static void debug_print(const char *dbg, const char *buf)
104 #if defined(ENABLE_NETWORK)
105 if (_debug_socket != INVALID_SOCKET) {
106 char buf2[1024 + 32];
108 bstrfmt (buf2, "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
109 /* Sending out an error when this fails would be nice, however... the error
110 * would have to be send over this failing socket which won't work. */
111 send(_debug_socket, buf2, (int)strlen(buf2), 0);
112 return;
114 #endif /* ENABLE_NETWORK */
115 if (strcmp(dbg, "desync") == 0) {
116 static FILE *f = FioFOpenFile("commands-out.log", "wb", AUTOSAVE_DIR);
117 if (f == NULL) return;
119 fprintf(f, "%s%s\n", GetLogPrefix(), buf);
120 fflush(f);
121 #ifdef RANDOM_DEBUG
122 } else if (strcmp(dbg, "random") == 0) {
123 static FILE *f = FioFOpenFile("random-out.log", "wb", AUTOSAVE_DIR);
124 if (f == NULL) return;
126 fprintf(f, "%s\n", buf);
127 fflush(f);
128 #endif
129 } else {
130 char buffer[512];
131 bstrfmt (buffer, "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
132 #if defined(WINCE)
133 NKDbgPrintfW(OTTD2FS(buffer));
134 #elif defined(WIN32) || defined(WIN64)
135 _fputts(OTTD2FS(buffer, true), stderr);
136 #else
137 fputs(buffer, stderr);
138 #endif
139 #ifdef ENABLE_NETWORK
140 NetworkAdminConsole(dbg, buf);
141 #endif /* ENABLE_NETWORK */
142 IConsoleDebug(dbg, buf);
147 * Output a debug line.
148 * @note Do not call directly, use the #DEBUG macro instead.
149 * @param dbg Debug category.
150 * @param format Text string a la printf, with optional arguments.
152 void CDECL debug(const char *dbg, const char *format, ...)
154 char buf[1024];
156 va_list va;
157 va_start(va, format);
158 bstrvfmt (buf, format, va);
159 va_end(va);
161 debug_print(dbg, buf);
163 #endif /* NO_DEBUG_MESSAGES */
166 * Set debugging levels by parsing the text in \a s.
167 * For setting individual levels a string like \c "net=3,grf=6" should be used.
168 * If the string starts with a number, the number is used as global debugging level.
169 * @param s Text describing the wanted debugging levels.
171 void SetDebugString(const char *s)
173 int v;
174 char *end;
175 const char *t;
177 /* global debugging level? */
178 if (*s >= '0' && *s <= '9') {
179 const DebugLevel *i;
181 v = strtoul(s, &end, 0);
182 s = end;
184 for (i = debug_level; i != endof(debug_level); ++i) *i->level = v;
187 /* individual levels */
188 for (;;) {
189 const DebugLevel *i;
190 int *p;
192 /* skip delimiters */
193 while (*s == ' ' || *s == ',' || *s == '\t') s++;
194 if (*s == '\0') break;
196 t = s;
197 while (*s >= 'a' && *s <= 'z') s++;
199 /* check debugging levels */
200 p = NULL;
201 for (i = debug_level; i != endof(debug_level); ++i) {
202 if (s == t + strlen(i->name) && strncmp(t, i->name, s - t) == 0) {
203 p = i->level;
204 break;
208 if (*s == '=') s++;
209 v = strtoul(s, &end, 0);
210 s = end;
211 if (p != NULL) {
212 *p = v;
213 } else {
214 ShowInfoF("Unknown debug level '%.*s'", (int)(s - t), t);
215 return;
221 * Print out the current debug-level.
222 * Just return a string with the values of all the debug categories.
223 * @return string with debug-levels
225 const char *GetDebugString()
227 const DebugLevel *i;
228 static char dbgstr[150];
229 stringb dbgsb (dbgstr);
231 dbgsb.zerofill();
232 i = debug_level;
233 dbgsb.fmt ("%s=%d", i->name, *i->level);
235 for (i++; i != endof(debug_level); i++) {
236 dbgsb.append_fmt (", %s=%d", i->name, *i->level);
239 return dbgstr;
243 * Get the prefix for logs; if show_date_in_logs is enabled it returns
244 * the date, otherwise it returns nothing.
245 * @return the prefix for logs (do not free), never NULL
247 const char *GetLogPrefix()
249 static char _log_prefix[24];
250 if (_settings_client.gui.show_date_in_logs) {
251 time_t cur_time = time(NULL);
252 strftime(_log_prefix, sizeof(_log_prefix), "[%Y-%m-%d %H:%M:%S] ", localtime(&cur_time));
253 } else {
254 *_log_prefix = '\0';
256 return _log_prefix;