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/>.
8 /** @file debug.cpp Handling of printing debug messages. */
12 #include "console_func.h"
14 #include "string_func.h"
15 #include "fileio_func.h"
16 #include "settings_type.h"
19 #include "os/windows/win32.h"
24 #include "network/network_admin.h"
25 SOCKET _debug_socket
= INVALID_SOCKET
;
27 #include "safeguards.h"
29 int _debug_driver_level
;
32 int _debug_misc_level
;
34 int _debug_sprite_level
;
35 int _debug_oldloader_level
;
37 int _debug_yapf_level
;
38 int _debug_freetype_level
;
39 int _debug_script_level
;
41 int _debug_gamelog_level
;
42 int _debug_desync_level
;
43 int _debug_console_level
;
45 int _debug_random_level
;
53 #define DEBUG_LEVEL(x) { #x, &_debug_##x##_level }
54 static const DebugLevel debug_level
[] = {
61 DEBUG_LEVEL(oldloader
),
64 DEBUG_LEVEL(freetype
),
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
)
85 for (const DebugLevel
*i
= debug_level
; i
!= endof(debug_level
); ++i
) {
87 buf
= strecpy(buf
, "List of debug facility names:\n", last
);
89 buf
= strecpy(buf
, ", ", last
);
92 buf
= strecpy(buf
, i
->name
, last
);
93 length
+= strlen(i
->name
);
96 buf
= strecpy(buf
, "\n\n", last
);
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);
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
);
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
);
133 seprintf(buffer
, lastof(buffer
), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg
, buf
);
135 wchar_t system_buf
[512];
136 convert_to_fs(buffer
, system_buf
, lengthof(system_buf
));
137 fputws(system_buf
, stderr
);
139 fputs(buffer
, stderr
);
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
, ...)
157 va_start(va
, format
);
158 vseprintf(buf
, lastof(buf
), format
, 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
)
176 /* global debugging level? */
177 if (*s
>= '0' && *s
<= '9') {
180 v
= strtoul(s
, &end
, 0);
183 for (i
= debug_level
; i
!= endof(debug_level
); ++i
) *i
->level
= v
;
186 /* individual levels */
191 /* skip delimiters */
192 while (*s
== ' ' || *s
== ',' || *s
== '\t') s
++;
193 if (*s
== '\0') break;
196 while (*s
>= 'a' && *s
<= 'z') s
++;
198 /* check debugging levels */
200 for (i
= debug_level
; i
!= endof(debug_level
); ++i
) {
201 if (s
== t
+ strlen(i
->name
) && strncmp(t
, i
->name
, s
- t
) == 0) {
208 v
= strtoul(s
, &end
, 0);
213 ShowInfoF("Unknown debug level '%.*s'", (int)(s
- t
), t
);
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()
227 static char dbgstr
[150];
230 memset(dbgstr
, 0, sizeof(dbgstr
));
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
));
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
));