Add: INR currency (#8136)
[openttd-github.git] / src / debug.cpp
blobded6bf1ccdd4f6d3495a9ec96f44815eda910575
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 uint32 _realtime_tick = 0;
50 struct DebugLevel {
51 const char *name;
52 int *level;
55 #define DEBUG_LEVEL(x) { #x, &_debug_##x##_level }
56 static const DebugLevel debug_level[] = {
57 DEBUG_LEVEL(driver),
58 DEBUG_LEVEL(grf),
59 DEBUG_LEVEL(map),
60 DEBUG_LEVEL(misc),
61 DEBUG_LEVEL(net),
62 DEBUG_LEVEL(sprite),
63 DEBUG_LEVEL(oldloader),
64 DEBUG_LEVEL(npf),
65 DEBUG_LEVEL(yapf),
66 DEBUG_LEVEL(freetype),
67 DEBUG_LEVEL(script),
68 DEBUG_LEVEL(sl),
69 DEBUG_LEVEL(gamelog),
70 DEBUG_LEVEL(desync),
71 DEBUG_LEVEL(console),
72 #ifdef RANDOM_DEBUG
73 DEBUG_LEVEL(random),
74 #endif
76 #undef DEBUG_LEVEL
78 /**
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)
86 size_t length = 0;
87 for (const DebugLevel *i = debug_level; i != endof(debug_level); ++i) {
88 if (length == 0) {
89 buf = strecpy(buf, "List of debug facility names:\n", last);
90 } else {
91 buf = strecpy(buf, ", ", last);
92 length += 2;
94 buf = strecpy(buf, i->name, last);
95 length += strlen(i->name);
97 if (length > 0) {
98 buf = strecpy(buf, "\n\n", last);
100 return buf;
104 * Internal function for outputting the debug line.
105 * @param dbg Debug category.
106 * @param buf Text line to output.
108 static void debug_print(const char *dbg, const char *buf)
110 if (_debug_socket != INVALID_SOCKET) {
111 char buf2[1024 + 32];
113 seprintf(buf2, lastof(buf2), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
114 /* Sending out an error when this fails would be nice, however... the error
115 * would have to be send over this failing socket which won't work. */
116 send(_debug_socket, buf2, (int)strlen(buf2), 0);
117 return;
119 if (strcmp(dbg, "desync") == 0) {
120 static FILE *f = FioFOpenFile("commands-out.log", "wb", AUTOSAVE_DIR);
121 if (f == nullptr) return;
123 fprintf(f, "%s%s\n", GetLogPrefix(), buf);
124 fflush(f);
125 #ifdef RANDOM_DEBUG
126 } else if (strcmp(dbg, "random") == 0) {
127 static FILE *f = FioFOpenFile("random-out.log", "wb", AUTOSAVE_DIR);
128 if (f == nullptr) return;
130 fprintf(f, "%s\n", buf);
131 fflush(f);
132 #endif
133 } else {
134 char buffer[512];
135 seprintf(buffer, lastof(buffer), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
136 #if defined(_WIN32)
137 TCHAR system_buf[512];
138 convert_to_fs(buffer, system_buf, lengthof(system_buf), true);
139 _fputts(system_buf, stderr);
140 #else
141 fputs(buffer, stderr);
142 #endif
143 NetworkAdminConsole(dbg, buf);
144 IConsoleDebug(dbg, buf);
149 * Output a debug line.
150 * @note Do not call directly, use the #DEBUG macro instead.
151 * @param dbg Debug category.
152 * @param format Text string a la printf, with optional arguments.
154 void CDECL debug(const char *dbg, const char *format, ...)
156 char buf[1024];
158 va_list va;
159 va_start(va, format);
160 vseprintf(buf, lastof(buf), format, va);
161 va_end(va);
163 debug_print(dbg, buf);
167 * Set debugging levels by parsing the text in \a s.
168 * For setting individual levels a string like \c "net=3,grf=6" should be used.
169 * If the string starts with a number, the number is used as global debugging level.
170 * @param s Text describing the wanted debugging levels.
172 void SetDebugString(const char *s)
174 int v;
175 char *end;
176 const char *t;
178 /* global debugging level? */
179 if (*s >= '0' && *s <= '9') {
180 const DebugLevel *i;
182 v = strtoul(s, &end, 0);
183 s = end;
185 for (i = debug_level; i != endof(debug_level); ++i) *i->level = v;
188 /* individual levels */
189 for (;;) {
190 const DebugLevel *i;
191 int *p;
193 /* skip delimiters */
194 while (*s == ' ' || *s == ',' || *s == '\t') s++;
195 if (*s == '\0') break;
197 t = s;
198 while (*s >= 'a' && *s <= 'z') s++;
200 /* check debugging levels */
201 p = nullptr;
202 for (i = debug_level; i != endof(debug_level); ++i) {
203 if (s == t + strlen(i->name) && strncmp(t, i->name, s - t) == 0) {
204 p = i->level;
205 break;
209 if (*s == '=') s++;
210 v = strtoul(s, &end, 0);
211 s = end;
212 if (p != nullptr) {
213 *p = v;
214 } else {
215 ShowInfoF("Unknown debug level '%.*s'", (int)(s - t), t);
216 return;
222 * Print out the current debug-level.
223 * Just return a string with the values of all the debug categories.
224 * @return string with debug-levels
226 const char *GetDebugString()
228 const DebugLevel *i;
229 static char dbgstr[150];
230 char dbgval[20];
232 memset(dbgstr, 0, sizeof(dbgstr));
233 i = debug_level;
234 seprintf(dbgstr, lastof(dbgstr), "%s=%d", i->name, *i->level);
236 for (i++; i != endof(debug_level); i++) {
237 seprintf(dbgval, lastof(dbgval), ", %s=%d", i->name, *i->level);
238 strecat(dbgstr, dbgval, lastof(dbgstr));
241 return dbgstr;
245 * Get the prefix for logs; if show_date_in_logs is enabled it returns
246 * the date, otherwise it returns nothing.
247 * @return the prefix for logs (do not free), never nullptr
249 const char *GetLogPrefix()
251 static char _log_prefix[24];
252 if (_settings_client.gui.show_date_in_logs) {
253 time_t cur_time = time(nullptr);
254 strftime(_log_prefix, sizeof(_log_prefix), "[%Y-%m-%d %H:%M:%S] ", localtime(&cur_time));
255 } else {
256 *_log_prefix = '\0';
258 return _log_prefix;