OpenTTD Source: src/debug.cpp Source File

OpenTTD Source 12.0-beta1

debug.cpp
Go to the documentation of this file.
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 */
7
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"
17
18 #if defined(_WIN32)
19 #include "os/windows/win32.h"
20 #endif
21
22 #include "walltime_func.h"
23
24 #include "network/network_admin.h"
25 SOCKET _debug_socket = INVALID_SOCKET;
26
27 #include "safeguards.h"
28
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
47
48 struct DebugLevel {
49 const char *name;
50 int *level;
51 };
52
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
73 };
74 #undef DEBUG_LEVEL
75
82 char *DumpDebugFacilityNames(char *buf, char *last)
83 {
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;
91 }
92 buf = strecpy(buf, i->name, last);
93 length += strlen(i->name);
94 }
95 if (length > 0) {
96 buf = strecpy(buf, "\n\n", last);
97 }
98 return buf;
99 }
100
106 void DebugPrint(const char *level, const std::string &message)
107 {
108 if (_debug_socket != INVALID_SOCKET) {
109 std::string msg = fmt::format("{}dbg: [{}] {}\n", GetLogPrefix(), level, message);
110 /* Sending out an error when this fails would be nice, however... the error
111 * would have to be send over this failing socket which won't work. */
112 send(_debug_socket, msg.c_str(), (int)msg.size(), 0);
113 return;
114 }
115 if (strcmp(level, "desync") == 0) {
116 static FILE *f = FioFOpenFile("commands-out.log", "wb", AUTOSAVE_DIR);
117 if (f == nullptr) return;
118
119 fprintf(f, "%s%s\n", GetLogPrefix(), message.c_str());
120 fflush(f);
121 #ifdef RANDOM_DEBUG
122 } else if (strcmp(level, "random") == 0) {
123 static FILE *f = FioFOpenFile("random-out.log", "wb", AUTOSAVE_DIR);
124 if (f == nullptr) return;
125
126 fprintf(f, "%s\n", message.c_str());
127 fflush(f);
128 #endif
129 } else {
130 std::string msg = fmt::format("{}dbg: [{}] {}\n", GetLogPrefix(), level, message);
131 fputs(msg.c_str(), stderr);
132
133 NetworkAdminConsole(level, message);
134 if (_settings_client.gui.developer >= 2) IConsolePrint(CC_DEBUG, "dbg: [{}] {}", level, message);
135 }
136 }
137
144 void SetDebugString(const char *s)
145 {
146 int v;
147 char *end;
148 const char *t;
149
150 /* global debugging level? */
151 if (*s >= '0' && *s <= '9') {
152 const DebugLevel *i;
153
154 v = strtoul(s, &end, 0);
155 s = end;
156
157 for (i = debug_level; i != endof(debug_level); ++i) *i->level = v;
158 }
159
160 /* individual levels */
161 for (;;) {
162 const DebugLevel *i;
163 int *p;
164
165 /* skip delimiters */
166 while (*s == ' ' || *s == ',' || *s == '\t') s++;
167 if (*s == '\0') break;
168
169 t = s;
170 while (*s >= 'a' && *s <= 'z') s++;
171
172 /* check debugging levels */
173 p = nullptr;
174 for (i = debug_level; i != endof(debug_level); ++i) {
175 if (s == t + strlen(i->name) && strncmp(t, i->name, s - t) == 0) {
176 p = i->level;
177 break;
178 }
179 }
180
181 if (*s == '=') s++;
182 v = strtoul(s, &end, 0);
183 s = end;
184 if (p != nullptr) {
185 *p = v;
186 } else {
187 ShowInfoF("Unknown debug level '%.*s'", (int)(s - t), t);
188 return;
189 }
190 }
191 }
192
198 const char *GetDebugString()
199 {
200 const DebugLevel *i;
201 static char dbgstr[150];
202 char dbgval[20];
203
204 memset(dbgstr, 0, sizeof(dbgstr));
205 i = debug_level;
206 seprintf(dbgstr, lastof(dbgstr), "%s=%d", i->name, *i->level);
207
208 for (i++; i != endof(debug_level); i++) {
209 seprintf(dbgval, lastof(dbgval), ", %s=%d", i->name, *i->level);
210 strecat(dbgstr, dbgval, lastof(dbgstr));
211 }
212
213 return dbgstr;
214 }
215
221 const char *GetLogPrefix()
222 {
223 static char _log_prefix[24];
224 if (_settings_client.gui.show_date_in_logs) {
225 LocalTime::Format(_log_prefix, lastof(_log_prefix), "[%Y-%m-%d %H:%M:%S] ");
226 } else {
227 *_log_prefix = '\0';
228 }
229 return _log_prefix;
230 }
231
SetDebugString
void SetDebugString(const char *s)
Set debugging levels by parsing the text in s.
Definition: debug.cpp:144
win32.h
fileio_func.h
AUTOSAVE_DIR
@ AUTOSAVE_DIR
Subdirectory of save for autosaves.
Definition: fileio_type.h:111
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:52
NetworkAdminConsole
void NetworkAdminConsole(const std::string_view origin, const std::string_view string)
Send console to the admin network (if they did opt in for the respective update).
Definition: network_admin.cpp:927
GUISettings::show_date_in_logs
bool show_date_in_logs
whether to show dates in console logs
Definition: settings_type.h:185
DumpDebugFacilityNames
char * DumpDebugFacilityNames(char *buf, char *last)
Dump the available debug facility names in the help text.
Definition: debug.cpp:82
walltime_func.h
GetLogPrefix
const char * GetLogPrefix()
Get the prefix for logs; if show_date_in_logs is enabled it returns the date, otherwise it returns no...
Definition: debug.cpp:221
FioFOpenFile
FILE * FioFOpenFile(const std::string &filename, const char *mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
Definition: fileio.cpp:245
safeguards.h
CC_DEBUG
static const TextColour CC_DEBUG
Colour for debug output.
Definition: console_type.h:28
GUISettings::developer
uint8 developer
print non-fatal warnings in console (>= 1), copy debug output to console (== 2)
Definition: settings_type.h:184
Time::Format
static size_t Format(char *buffer, const char *last, const char *format) NOACCESS(2) WARN_TIME_FORMAT(3)
Format the current time with the given strftime format specifiers.
Definition: walltime_func.h:58
settings_type.h
stdafx.h
string_func.h
endof
#define endof(x)
Get the end element of an fixed size array.
Definition: stdafx.h:386
GetDebugString
const char * GetDebugString()
Print out the current debug-level.
Definition: debug.cpp:198
DebugPrint
void DebugPrint(const char *level, const std::string &message)
Internal function for outputting the debug line.
Definition: debug.cpp:106
ShowInfoF
void CDECL ShowInfoF(const char *str,...)
Shows some information on the console/a popup box depending on the OS.
Definition: openttd.cpp:154
seprintf
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:535
DebugLevel
Definition: debug.cpp:48
network_admin.h
console_func.h
strecpy
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: string.cpp:112
strecat
char * strecat(char *dst, const char *src, const char *last)
Appends characters from one string to another.
Definition: string.cpp:84
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:394
ClientSettings::gui
GUISettings gui
settings related to the GUI
Definition: settings_type.h:593
debug.h
IConsolePrint
void IConsolePrint(TextColour colour_code, const std::string &string)
Handle the printing of text entered into the console or redirected there by any other means.
Definition: console.cpp:94