1 /* $Id: debug.cpp 26058 2013-11-23 13:15:07Z rubidium $ */
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
;
44 int _debug_sound_level
;
46 int _debug_random_level
;
49 uint32 _realtime_tick
= 0;
56 #define DEBUG_LEVEL(x) { #x, &_debug_##x##_level }
57 static const DebugLevel debug_level
[] = {
64 DEBUG_LEVEL(oldloader
),
67 DEBUG_LEVEL(freetype
),
81 * Dump the available debug facility names in the help text.
82 * @param buf Start address for storing the output.
83 * @param last Last valid address for storing the output.
84 * @return Next free position in the output.
86 char *DumpDebugFacilityNames(char *buf
, char *last
)
89 for (const DebugLevel
*i
= debug_level
; i
!= endof(debug_level
); ++i
) {
91 buf
= strecpy(buf
, "List of debug facility names:\n", last
);
93 buf
= strecpy(buf
, ", ", last
);
96 buf
= strecpy(buf
, i
->name
, last
);
97 length
+= strlen(i
->name
);
100 buf
= strecpy(buf
, "\n\n", last
);
105 #if !defined(NO_DEBUG_MESSAGES)
108 * Internal function for outputting the debug line.
109 * @param dbg Debug category.
110 * @param buf Text line to output.
112 static void debug_print(const char *dbg
, const char *buf
)
114 #if defined(ENABLE_NETWORK)
115 if (_debug_socket
!= INVALID_SOCKET
) {
116 char buf2
[1024 + 32];
118 seprintf(buf2
, lastof(buf2
), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg
, buf
);
119 /* Sending out an error when this fails would be nice, however... the error
120 * would have to be send over this failing socket which won't work. */
121 send(_debug_socket
, buf2
, (int)strlen(buf2
), 0);
124 #endif /* ENABLE_NETWORK */
125 if (strcmp(dbg
, "desync") == 0) {
126 static FILE *f
= FioFOpenFile("commands-out.log", "wb", AUTOSAVE_DIR
);
127 if (f
== nullptr) return;
129 fprintf(f
, "%s%s\n", GetLogPrefix(), buf
);
132 } else if (strcmp(dbg
, "random") == 0) {
133 static FILE *f
= FioFOpenFile("random-out.log", "wb", AUTOSAVE_DIR
);
134 if (f
== nullptr) return;
136 fprintf(f
, "%s\n", buf
);
141 seprintf(buffer
, lastof(buffer
), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg
, buf
);
143 NKDbgPrintfW(OTTD2FS(buffer
));
144 #elif defined(WIN32) || defined(WIN64)
145 _fputts(OTTD2FS(buffer
, true), stderr
);
147 fputs(buffer
, stderr
);
149 #ifdef ENABLE_NETWORK
150 NetworkAdminConsole(dbg
, buf
);
151 #endif /* ENABLE_NETWORK */
152 IConsoleDebug(dbg
, buf
);
157 * Output a debug line.
158 * @note Do not call directly, use the #DEBUG macro instead.
159 * @param dbg Debug category.
160 * @param format Text string a la printf, with optional arguments.
162 void CDECL
debug(const char *dbg
, const char *format
, ...)
167 va_start(va
, format
);
168 vseprintf(buf
, lastof(buf
), format
, va
);
171 debug_print(dbg
, buf
);
173 #endif /* NO_DEBUG_MESSAGES */
176 * Set debugging levels by parsing the text in \a s.
177 * For setting individual levels a string like \c "net=3,grf=6" should be used.
178 * If the string starts with a number, the number is used as global debugging level.
179 * @param s Text describing the wanted debugging levels.
181 void SetDebugString(const char *s
)
187 /* global debugging level? */
188 if (*s
>= '0' && *s
<= '9') {
191 v
= strtoul(s
, &end
, 0);
194 for (i
= debug_level
; i
!= endof(debug_level
); ++i
) *i
->level
= v
;
197 /* individual levels */
202 /* skip delimiters */
203 while (*s
== ' ' || *s
== ',' || *s
== '\t') s
++;
204 if (*s
== '\0') break;
207 while (*s
>= 'a' && *s
<= 'z') s
++;
209 /* check debugging levels */
211 for (i
= debug_level
; i
!= endof(debug_level
); ++i
) {
212 if (s
== t
+ strlen(i
->name
) && strncmp(t
, i
->name
, s
- t
) == 0) {
219 v
= strtoul(s
, &end
, 0);
224 ShowInfoF("Unknown debug level '%.*s'", (int)(s
- t
), t
);
231 * Print out the current debug-level.
232 * Just return a string with the values of all the debug categories.
233 * @return string with debug-levels
235 const char *GetDebugString()
238 static char dbgstr
[150];
241 memset(dbgstr
, 0, sizeof(dbgstr
));
243 seprintf(dbgstr
, lastof(dbgstr
), "%s=%d", i
->name
, *i
->level
);
245 for (i
++; i
!= endof(debug_level
); i
++) {
246 seprintf(dbgval
, lastof(dbgval
), ", %s=%d", i
->name
, *i
->level
);
247 strecat(dbgstr
, dbgval
, lastof(dbgstr
));
254 * Get the prefix for logs; if show_date_in_logs is enabled it returns
255 * the date, otherwise it returns nothing.
256 * @return the prefix for logs (do not free), never nullptr
258 const char *GetLogPrefix()
260 static char _log_prefix
[24];
261 if (_settings_client
.gui
.show_date_in_logs
) {
262 time_t cur_time
= time(nullptr);
263 strftime(_log_prefix
, sizeof(_log_prefix
), "[%Y-%m-%d %H:%M:%S] ", localtime(&cur_time
));