Add non-animated SSSE3 blitter
[openttd/fttd.git] / src / debug.cpp
blob9387bb42fdbfdd18fb77c23a4c777553071d36ea
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_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 Start address for storing the output.
77 * @param last Last valid address for storing the output.
78 * @return Next free position in the output.
80 char *DumpDebugFacilityNames(char *buf, char *last)
82 size_t length = 0;
83 for (const DebugLevel *i = debug_level; i != endof(debug_level); ++i) {
84 if (length == 0) {
85 buf = strecpy(buf, "List of debug facility names:\n", last);
86 } else {
87 buf = strecpy(buf, ", ", last);
88 length += 2;
90 buf = strecpy(buf, i->name, last);
91 length += strlen(i->name);
93 if (length > 0) {
94 buf = strecpy(buf, "\n\n", last);
96 return buf;
99 #if !defined(NO_DEBUG_MESSAGES)
102 * Internal function for outputting the debug line.
103 * @param dbg Debug category.
104 * @param buf Text line to output.
106 static void debug_print(const char *dbg, const char *buf)
108 #if defined(ENABLE_NETWORK)
109 if (_debug_socket != INVALID_SOCKET) {
110 char buf2[1024 + 32];
112 snprintf(buf2, lengthof(buf2), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
113 /* Sending out an error when this fails would be nice, however... the error
114 * would have to be send over this failing socket which won't work. */
115 send(_debug_socket, buf2, (int)strlen(buf2), 0);
116 return;
118 #endif /* ENABLE_NETWORK */
119 if (strcmp(dbg, "desync") == 0) {
120 static FILE *f = FioFOpenFile("commands-out.log", "wb", AUTOSAVE_DIR);
121 if (f == NULL) return;
123 fprintf(f, "%s%s\n", GetLogPrefix(), buf);
124 fflush(f);
125 #ifdef RANDOM_DEBUG
126 } else if (strcmp(dbg, "random") == 0) {
127 static FILE *f = FioFOpenFile("random-out.log", "wb", AUTOSAVE_DIR);
128 if (f == NULL) return;
130 fprintf(f, "%s\n", buf);
131 fflush(f);
132 #endif
133 } else {
134 char buffer[512];
135 seprintf(buffer, lastof(buffer), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
136 #if defined(WINCE)
137 NKDbgPrintfW(OTTD2FS(buffer));
138 #elif defined(WIN32) || defined(WIN64)
139 _fputts(OTTD2FS(buffer, true), stderr);
140 #else
141 fputs(buffer, stderr);
142 #endif
143 #ifdef ENABLE_NETWORK
144 NetworkAdminConsole(dbg, buf);
145 #endif /* ENABLE_NETWORK */
146 IConsoleDebug(dbg, buf);
151 * Output a debug line.
152 * @note Do not call directly, use the #DEBUG macro instead.
153 * @param dbg Debug category.
154 * @param format Text string a la printf, with optional arguments.
156 void CDECL debug(const char *dbg, const char *format, ...)
158 char buf[1024];
160 va_list va;
161 va_start(va, format);
162 vsnprintf(buf, lengthof(buf), format, va);
163 va_end(va);
165 debug_print(dbg, buf);
167 #endif /* NO_DEBUG_MESSAGES */
170 * Set debugging levels by parsing the text in \a s.
171 * For setting individual levels a string like \c "net=3,grf=6" should be used.
172 * If the string starts with a number, the number is used as global debugging level.
173 * @param s Text describing the wanted debugging levels.
175 void SetDebugString(const char *s)
177 int v;
178 char *end;
179 const char *t;
181 /* global debugging level? */
182 if (*s >= '0' && *s <= '9') {
183 const DebugLevel *i;
185 v = strtoul(s, &end, 0);
186 s = end;
188 for (i = debug_level; i != endof(debug_level); ++i) *i->level = v;
191 /* individual levels */
192 for (;;) {
193 const DebugLevel *i;
194 int *p;
196 /* skip delimiters */
197 while (*s == ' ' || *s == ',' || *s == '\t') s++;
198 if (*s == '\0') break;
200 t = s;
201 while (*s >= 'a' && *s <= 'z') s++;
203 /* check debugging levels */
204 p = NULL;
205 for (i = debug_level; i != endof(debug_level); ++i) {
206 if (s == t + strlen(i->name) && strncmp(t, i->name, s - t) == 0) {
207 p = i->level;
208 break;
212 if (*s == '=') s++;
213 v = strtoul(s, &end, 0);
214 s = end;
215 if (p != NULL) {
216 *p = v;
217 } else {
218 ShowInfoF("Unknown debug level '%.*s'", (int)(s - t), t);
219 return;
225 * Print out the current debug-level.
226 * Just return a string with the values of all the debug categories.
227 * @return string with debug-levels
229 const char *GetDebugString()
231 const DebugLevel *i;
232 static char dbgstr[150];
233 char dbgval[20];
235 memset(dbgstr, 0, sizeof(dbgstr));
236 i = debug_level;
237 snprintf(dbgstr, sizeof(dbgstr), "%s=%d", i->name, *i->level);
239 for (i++; i != endof(debug_level); i++) {
240 snprintf(dbgval, sizeof(dbgval), ", %s=%d", i->name, *i->level);
241 strecat(dbgstr, dbgval, lastof(dbgstr));
244 return dbgstr;
248 * Get the prefix for logs; if show_date_in_logs is enabled it returns
249 * the date, otherwise it returns nothing.
250 * @return the prefix for logs (do not free), never NULL
252 const char *GetLogPrefix()
254 static char _log_prefix[24];
255 if (_settings_client.gui.show_date_in_logs) {
256 time_t cur_time = time(NULL);
257 strftime(_log_prefix, sizeof(_log_prefix), "[%Y-%m-%d %H:%M:%S] ", localtime(&cur_time));
258 } else {
259 *_log_prefix = '\0';
261 return _log_prefix;