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/>.
10 /** @file debug.cpp Handling of printing debug messages. */
14 #include "console_func.h"
16 #include "string_func.h"
17 #include "fileio_func.h"
18 #include "settings_type.h"
22 #if defined(ENABLE_NETWORK)
23 #include "network/network_admin.h"
24 SOCKET _debug_socket
= INVALID_SOCKET
;
25 #endif /* ENABLE_NETWORK */
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
;
48 uint32 _realtime_tick
= 0;
55 #define DEBUG_LEVEL(x) { #x, &_debug_##x##_level }
56 static const DebugLevel debug_level
[] = {
63 DEBUG_LEVEL(oldloader
),
66 DEBUG_LEVEL(freetype
),
79 * Dump the available debug facility names in the help text.
80 * @param buf Start address for storing the output.
81 * @param last Last valid address for storing the output.
82 * @return Next free position in the output.
84 char *DumpDebugFacilityNames(char *buf
, char *last
)
87 for (const DebugLevel
*i
= debug_level
; i
!= endof(debug_level
); ++i
) {
89 buf
= strecpy(buf
, "List of debug facility names:\n", last
);
91 buf
= strecpy(buf
, ", ", last
);
94 buf
= strecpy(buf
, i
->name
, last
);
95 length
+= strlen(i
->name
);
98 buf
= strecpy(buf
, "\n\n", last
);
103 #if !defined(NO_DEBUG_MESSAGES)
106 * Internal function for outputting the debug line.
107 * @param dbg Debug category.
108 * @param buf Text line to output.
110 static void debug_print(const char *dbg
, const char *buf
)
112 #if defined(ENABLE_NETWORK)
113 if (_debug_socket
!= INVALID_SOCKET
) {
114 char buf2
[1024 + 32];
116 seprintf(buf2
, lastof(buf2
), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg
, buf
);
117 /* Sending out an error when this fails would be nice, however... the error
118 * would have to be send over this failing socket which won't work. */
119 send(_debug_socket
, buf2
, (int)strlen(buf2
), 0);
122 #endif /* ENABLE_NETWORK */
123 if (strcmp(dbg
, "desync") == 0) {
124 static FILE *f
= FioFOpenFile("commands-out.log", "wb", AUTOSAVE_DIR
);
125 if (f
== NULL
) return;
127 fprintf(f
, "%s%s\n", GetLogPrefix(), buf
);
130 } else if (strcmp(dbg
, "random") == 0) {
131 static FILE *f
= FioFOpenFile("random-out.log", "wb", AUTOSAVE_DIR
);
132 if (f
== NULL
) return;
134 fprintf(f
, "%s\n", buf
);
139 seprintf(buffer
, lastof(buffer
), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg
, buf
);
141 NKDbgPrintfW(OTTD2FS(buffer
));
142 #elif defined(WIN32) || defined(WIN64)
143 _fputts(OTTD2FS(buffer
, true), stderr
);
145 fputs(buffer
, stderr
);
147 #ifdef ENABLE_NETWORK
148 NetworkAdminConsole(dbg
, buf
);
149 #endif /* ENABLE_NETWORK */
150 IConsoleDebug(dbg
, buf
);
155 * Output a debug line.
156 * @note Do not call directly, use the #DEBUG macro instead.
157 * @param dbg Debug category.
158 * @param format Text string a la printf, with optional arguments.
160 void CDECL
debug(const char *dbg
, const char *format
, ...)
165 va_start(va
, format
);
166 vseprintf(buf
, lastof(buf
), format
, va
);
169 debug_print(dbg
, buf
);
171 #endif /* NO_DEBUG_MESSAGES */
174 * Set debugging levels by parsing the text in \a s.
175 * For setting individual levels a string like \c "net=3,grf=6" should be used.
176 * If the string starts with a number, the number is used as global debugging level.
177 * @param s Text describing the wanted debugging levels.
179 void SetDebugString(const char *s
)
185 /* global debugging level? */
186 if (*s
>= '0' && *s
<= '9') {
189 v
= strtoul(s
, &end
, 0);
192 for (i
= debug_level
; i
!= endof(debug_level
); ++i
) *i
->level
= v
;
195 /* individual levels */
200 /* skip delimiters */
201 while (*s
== ' ' || *s
== ',' || *s
== '\t') s
++;
202 if (*s
== '\0') break;
205 while (*s
>= 'a' && *s
<= 'z') s
++;
207 /* check debugging levels */
209 for (i
= debug_level
; i
!= endof(debug_level
); ++i
) {
210 if (s
== t
+ strlen(i
->name
) && strncmp(t
, i
->name
, s
- t
) == 0) {
217 v
= strtoul(s
, &end
, 0);
222 ShowInfoF("Unknown debug level '%.*s'", (int)(s
- t
), t
);
229 * Print out the current debug-level.
230 * Just return a string with the values of all the debug categories.
231 * @return string with debug-levels
233 const char *GetDebugString()
236 static char dbgstr
[150];
239 memset(dbgstr
, 0, sizeof(dbgstr
));
241 seprintf(dbgstr
, lastof(dbgstr
), "%s=%d", i
->name
, *i
->level
);
243 for (i
++; i
!= endof(debug_level
); i
++) {
244 seprintf(dbgval
, lastof(dbgval
), ", %s=%d", i
->name
, *i
->level
);
245 strecat(dbgstr
, dbgval
, lastof(dbgstr
));
252 * Get the prefix for logs; if show_date_in_logs is enabled it returns
253 * the date, otherwise it returns nothing.
254 * @return the prefix for logs (do not free), never NULL
256 const char *GetLogPrefix()
258 static char _log_prefix
[24];
259 if (_settings_client
.gui
.show_date_in_logs
) {
260 time_t cur_time
= time(NULL
);
261 strftime(_log_prefix
, sizeof(_log_prefix
), "[%Y-%m-%d %H:%M:%S] ", localtime(&cur_time
));