(svn r23005) -Fix (r23004): Of course there's still the 16-sprite version for shore...
[openttd/fttd.git] / src / debug.cpp
blob3cdb6aaaeab6ce84bef5d86986b48ec5147e660b
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_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 int _debug_ai_level;
28 int _debug_driver_level;
29 int _debug_grf_level;
30 int _debug_map_level;
31 int _debug_misc_level;
32 int _debug_net_level;
33 int _debug_sprite_level;
34 int _debug_oldloader_level;
35 int _debug_npf_level;
36 int _debug_yapf_level;
37 int _debug_freetype_level;
38 int _debug_sl_level;
39 int _debug_gamelog_level;
40 int _debug_desync_level;
41 int _debug_console_level;
43 uint32 _realtime_tick = 0;
45 struct DebugLevel {
46 const char *name;
47 int *level;
50 #define DEBUG_LEVEL(x) { #x, &_debug_##x##_level }
51 static const DebugLevel debug_level[] = {
52 DEBUG_LEVEL(ai),
53 DEBUG_LEVEL(driver),
54 DEBUG_LEVEL(grf),
55 DEBUG_LEVEL(map),
56 DEBUG_LEVEL(misc),
57 DEBUG_LEVEL(net),
58 DEBUG_LEVEL(sprite),
59 DEBUG_LEVEL(oldloader),
60 DEBUG_LEVEL(npf),
61 DEBUG_LEVEL(yapf),
62 DEBUG_LEVEL(freetype),
63 DEBUG_LEVEL(sl),
64 DEBUG_LEVEL(gamelog),
65 DEBUG_LEVEL(desync),
66 DEBUG_LEVEL(console),
68 #undef DEBUG_LEVEL
70 #if !defined(NO_DEBUG_MESSAGES)
72 /**
73 * Internal function for outputting the debug line.
74 * @param dbg Debug category.
75 * @param buf Text line to output.
77 static void debug_print(const char *dbg, const char *buf)
79 #if defined(ENABLE_NETWORK)
80 if (_debug_socket != INVALID_SOCKET) {
81 char buf2[1024 + 32];
83 snprintf(buf2, lengthof(buf2), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
84 send(_debug_socket, buf2, (int)strlen(buf2), 0);
85 return;
87 #endif /* ENABLE_NETWORK */
88 if (strcmp(dbg, "desync") != 0) {
89 #if defined(WINCE)
90 /* We need to do OTTD2FS twice, but as it uses a static buffer, we need to store one temporary */
91 TCHAR tbuf[512];
92 _sntprintf(tbuf, sizeof(tbuf), _T("%s"), OTTD2FS(dbg));
93 NKDbgPrintfW(_T("dbg: [%s] %s\n"), tbuf, OTTD2FS(buf));
94 #else
95 fprintf(stderr, "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
96 #endif
97 #ifdef ENABLE_NETWORK
98 NetworkAdminConsole(dbg, buf);
99 #endif /* ENABLE_NETWORK */
100 IConsoleDebug(dbg, buf);
101 } else {
102 static FILE *f = FioFOpenFile("commands-out.log", "wb", AUTOSAVE_DIR);
103 if (f == NULL) return;
105 fprintf(f, "%s%s\n", GetLogPrefix(), buf);
106 fflush(f);
111 * Output a debug line.
112 * @note Do not call directly, use the #DEBUG macro instead.
113 * @param dbg Debug category.
114 * @param format Text string a la printf, with optional arguments.
116 void CDECL debug(const char *dbg, const char *format, ...)
118 char buf[1024];
120 va_list va;
121 va_start(va, format);
122 vsnprintf(buf, lengthof(buf), format, va);
123 va_end(va);
125 debug_print(dbg, buf);
127 #endif /* NO_DEBUG_MESSAGES */
130 * Set debugging levels by parsing the text in \a s.
131 * For setting individual levels a string like \c "net=3,grf=6" should be used.
132 * If the string starts with a number, the number is used as global debugging level.
133 * @param s Text describing the wanted debugging levels.
135 void SetDebugString(const char *s)
137 int v;
138 char *end;
139 const char *t;
141 /* global debugging level? */
142 if (*s >= '0' && *s <= '9') {
143 const DebugLevel *i;
145 v = strtoul(s, &end, 0);
146 s = end;
148 for (i = debug_level; i != endof(debug_level); ++i) *i->level = v;
151 /* individual levels */
152 for (;;) {
153 const DebugLevel *i;
154 int *p;
156 /* skip delimiters */
157 while (*s == ' ' || *s == ',' || *s == '\t') s++;
158 if (*s == '\0') break;
160 t = s;
161 while (*s >= 'a' && *s <= 'z') s++;
163 /* check debugging levels */
164 p = NULL;
165 for (i = debug_level; i != endof(debug_level); ++i) {
166 if (s == t + strlen(i->name) && strncmp(t, i->name, s - t) == 0) {
167 p = i->level;
168 break;
172 if (*s == '=') s++;
173 v = strtoul(s, &end, 0);
174 s = end;
175 if (p != NULL) {
176 *p = v;
177 } else {
178 ShowInfoF("Unknown debug level '%.*s'", (int)(s - t), t);
179 return;
185 * Print out the current debug-level.
186 * Just return a string with the values of all the debug categories.
187 * @return string with debug-levels
189 const char *GetDebugString()
191 const DebugLevel *i;
192 static char dbgstr[150];
193 char dbgval[20];
195 memset(dbgstr, 0, sizeof(dbgstr));
196 i = debug_level;
197 snprintf(dbgstr, sizeof(dbgstr), "%s=%d", i->name, *i->level);
199 for (i++; i != endof(debug_level); i++) {
200 snprintf(dbgval, sizeof(dbgval), ", %s=%d", i->name, *i->level);
201 strecat(dbgstr, dbgval, lastof(dbgstr));
204 return dbgstr;
208 * Get the prefix for logs; if show_date_in_logs is enabled it returns
209 * the date, otherwise it returns nothing.
210 * @return the prefix for logs (do not free), never NULL
212 const char *GetLogPrefix()
214 static char _log_prefix[24];
215 if (_settings_client.gui.show_date_in_logs) {
216 time_t cur_time = time(NULL);
217 strftime(_log_prefix, sizeof(_log_prefix), "[%Y-%m-%d %H:%M:%S] ", localtime(&cur_time));
218 } else {
219 *_log_prefix = '\0';
221 return _log_prefix;