Fix: Data races on cursor state in OpenGL backends
[openttd-github.git] / src / debug.cpp
blob25a0f1a82f2d4fcba7326541d22113faf4650d4e
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
8 /** @file debug.cpp Handling of printing debug messages. */
10 #include "stdafx.h"
11 #include <stdarg.h>
12 #include "console_func.h"
13 #include "debug.h"
14 #include "string_func.h"
15 #include "fileio_func.h"
16 #include "settings_type.h"
18 #if defined(_WIN32)
19 #include "os/windows/win32.h"
20 #endif
22 #include <time.h>
24 #include "network/network_admin.h"
25 SOCKET _debug_socket = INVALID_SOCKET;
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 #ifdef RANDOM_DEBUG
45 int _debug_random_level;
46 #endif
48 struct DebugLevel {
49 const char *name;
50 int *level;
53 #define DEBUG_LEVEL(x) { #x, &_debug_##x##_level }
54 static const DebugLevel debug_level[] = {
55 DEBUG_LEVEL(driver),
56 DEBUG_LEVEL(grf),
57 DEBUG_LEVEL(map),
58 DEBUG_LEVEL(misc),
59 DEBUG_LEVEL(net),
60 DEBUG_LEVEL(sprite),
61 DEBUG_LEVEL(oldloader),
62 DEBUG_LEVEL(npf),
63 DEBUG_LEVEL(yapf),
64 DEBUG_LEVEL(freetype),
65 DEBUG_LEVEL(script),
66 DEBUG_LEVEL(sl),
67 DEBUG_LEVEL(gamelog),
68 DEBUG_LEVEL(desync),
69 DEBUG_LEVEL(console),
70 #ifdef RANDOM_DEBUG
71 DEBUG_LEVEL(random),
72 #endif
74 #undef DEBUG_LEVEL
76 /**
77 * Dump the available debug facility names in the help text.
78 * @param buf Start address for storing the output.
79 * @param last Last valid address for storing the output.
80 * @return Next free position in the output.
82 char *DumpDebugFacilityNames(char *buf, char *last)
84 size_t length = 0;
85 for (const DebugLevel *i = debug_level; i != endof(debug_level); ++i) {
86 if (length == 0) {
87 buf = strecpy(buf, "List of debug facility names:\n", last);
88 } else {
89 buf = strecpy(buf, ", ", last);
90 length += 2;
92 buf = strecpy(buf, i->name, last);
93 length += strlen(i->name);
95 if (length > 0) {
96 buf = strecpy(buf, "\n\n", last);
98 return buf;
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 (_debug_socket != INVALID_SOCKET) {
109 char buf2[1024 + 32];
111 seprintf(buf2, lastof(buf2), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
112 /* Sending out an error when this fails would be nice, however... the error
113 * would have to be send over this failing socket which won't work. */
114 send(_debug_socket, buf2, (int)strlen(buf2), 0);
115 return;
117 if (strcmp(dbg, "desync") == 0) {
118 static FILE *f = FioFOpenFile("commands-out.log", "wb", AUTOSAVE_DIR);
119 if (f == nullptr) return;
121 fprintf(f, "%s%s\n", GetLogPrefix(), buf);
122 fflush(f);
123 #ifdef RANDOM_DEBUG
124 } else if (strcmp(dbg, "random") == 0) {
125 static FILE *f = FioFOpenFile("random-out.log", "wb", AUTOSAVE_DIR);
126 if (f == nullptr) return;
128 fprintf(f, "%s\n", buf);
129 fflush(f);
130 #endif
131 } else {
132 char buffer[512];
133 seprintf(buffer, lastof(buffer), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
134 #if defined(_WIN32)
135 wchar_t system_buf[512];
136 convert_to_fs(buffer, system_buf, lengthof(system_buf));
137 fputws(system_buf, stderr);
138 #else
139 fputs(buffer, stderr);
140 #endif
141 NetworkAdminConsole(dbg, buf);
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 vseprintf(buf, lastof(buf), format, va);
159 va_end(va);
161 debug_print(dbg, buf);
165 * Set debugging levels by parsing the text in \a s.
166 * For setting individual levels a string like \c "net=3,grf=6" should be used.
167 * If the string starts with a number, the number is used as global debugging level.
168 * @param s Text describing the wanted debugging levels.
170 void SetDebugString(const char *s)
172 int v;
173 char *end;
174 const char *t;
176 /* global debugging level? */
177 if (*s >= '0' && *s <= '9') {
178 const DebugLevel *i;
180 v = strtoul(s, &end, 0);
181 s = end;
183 for (i = debug_level; i != endof(debug_level); ++i) *i->level = v;
186 /* individual levels */
187 for (;;) {
188 const DebugLevel *i;
189 int *p;
191 /* skip delimiters */
192 while (*s == ' ' || *s == ',' || *s == '\t') s++;
193 if (*s == '\0') break;
195 t = s;
196 while (*s >= 'a' && *s <= 'z') s++;
198 /* check debugging levels */
199 p = nullptr;
200 for (i = debug_level; i != endof(debug_level); ++i) {
201 if (s == t + strlen(i->name) && strncmp(t, i->name, s - t) == 0) {
202 p = i->level;
203 break;
207 if (*s == '=') s++;
208 v = strtoul(s, &end, 0);
209 s = end;
210 if (p != nullptr) {
211 *p = v;
212 } else {
213 ShowInfoF("Unknown debug level '%.*s'", (int)(s - t), t);
214 return;
220 * Print out the current debug-level.
221 * Just return a string with the values of all the debug categories.
222 * @return string with debug-levels
224 const char *GetDebugString()
226 const DebugLevel *i;
227 static char dbgstr[150];
228 char dbgval[20];
230 memset(dbgstr, 0, sizeof(dbgstr));
231 i = debug_level;
232 seprintf(dbgstr, lastof(dbgstr), "%s=%d", i->name, *i->level);
234 for (i++; i != endof(debug_level); i++) {
235 seprintf(dbgval, lastof(dbgval), ", %s=%d", i->name, *i->level);
236 strecat(dbgstr, dbgval, lastof(dbgstr));
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 nullptr
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(nullptr);
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;