2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file debug.cpp Handling of printing debug messages. */
|
2007-02-23 18:55:07 +00:00
|
|
|
|
2005-02-05 15:58:59 +00:00
|
|
|
#include "stdafx.h"
|
2005-02-05 21:57:01 +00:00
|
|
|
#include <stdarg.h>
|
2008-05-24 10:15:06 +00:00
|
|
|
#include "console_func.h"
|
2012-01-03 21:47:01 +00:00
|
|
|
#include "debug.h"
|
|
|
|
#include "string_func.h"
|
2008-12-29 21:50:25 +00:00
|
|
|
#include "fileio_func.h"
|
2009-09-09 15:11:46 +00:00
|
|
|
#include "settings_type.h"
|
|
|
|
|
2018-12-09 01:28:14 +00:00
|
|
|
#if defined(_WIN32)
|
2018-05-20 01:15:22 +00:00
|
|
|
#include "os/windows/win32.h"
|
|
|
|
#endif
|
|
|
|
|
2009-09-09 15:11:46 +00:00
|
|
|
#include <time.h>
|
2007-02-08 12:27:53 +00:00
|
|
|
|
2010-10-17 17:43:01 +00:00
|
|
|
#include "network/network_admin.h"
|
2007-02-08 12:27:53 +00:00
|
|
|
SOCKET _debug_socket = INVALID_SOCKET;
|
2005-02-05 15:58:59 +00:00
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2005-07-27 19:57:12 +00:00
|
|
|
int _debug_driver_level;
|
2005-02-05 15:58:59 +00:00
|
|
|
int _debug_grf_level;
|
|
|
|
int _debug_map_level;
|
|
|
|
int _debug_misc_level;
|
|
|
|
int _debug_net_level;
|
2006-12-26 17:36:18 +00:00
|
|
|
int _debug_sprite_level;
|
2005-02-06 18:28:35 +00:00
|
|
|
int _debug_oldloader_level;
|
2005-04-11 19:53:44 +00:00
|
|
|
int _debug_npf_level;
|
2006-05-27 16:12:16 +00:00
|
|
|
int _debug_yapf_level;
|
2006-11-16 22:05:33 +00:00
|
|
|
int _debug_freetype_level;
|
2011-12-01 12:04:10 +00:00
|
|
|
int _debug_script_level;
|
2006-12-26 17:36:18 +00:00
|
|
|
int _debug_sl_level;
|
2008-06-03 18:35:58 +00:00
|
|
|
int _debug_gamelog_level;
|
2008-12-29 21:50:25 +00:00
|
|
|
int _debug_desync_level;
|
2010-02-10 16:10:05 +00:00
|
|
|
int _debug_console_level;
|
2012-01-26 17:24:56 +00:00
|
|
|
#ifdef RANDOM_DEBUG
|
|
|
|
int _debug_random_level;
|
|
|
|
#endif
|
2005-02-05 15:58:59 +00:00
|
|
|
|
2007-03-07 12:11:48 +00:00
|
|
|
struct DebugLevel {
|
2005-05-20 17:59:24 +00:00
|
|
|
const char *name;
|
|
|
|
int *level;
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2005-05-20 17:59:24 +00:00
|
|
|
|
|
|
|
#define DEBUG_LEVEL(x) { #x, &_debug_##x##_level }
|
|
|
|
static const DebugLevel debug_level[] = {
|
2005-07-27 19:57:12 +00:00
|
|
|
DEBUG_LEVEL(driver),
|
2005-05-20 17:59:24 +00:00
|
|
|
DEBUG_LEVEL(grf),
|
|
|
|
DEBUG_LEVEL(map),
|
|
|
|
DEBUG_LEVEL(misc),
|
|
|
|
DEBUG_LEVEL(net),
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG_LEVEL(sprite),
|
2005-05-20 17:59:24 +00:00
|
|
|
DEBUG_LEVEL(oldloader),
|
2006-05-27 16:12:16 +00:00
|
|
|
DEBUG_LEVEL(npf),
|
2006-11-16 22:05:33 +00:00
|
|
|
DEBUG_LEVEL(yapf),
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG_LEVEL(freetype),
|
2011-12-01 12:04:10 +00:00
|
|
|
DEBUG_LEVEL(script),
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG_LEVEL(sl),
|
2008-06-03 18:35:58 +00:00
|
|
|
DEBUG_LEVEL(gamelog),
|
2008-12-29 21:50:25 +00:00
|
|
|
DEBUG_LEVEL(desync),
|
2010-02-10 16:12:54 +00:00
|
|
|
DEBUG_LEVEL(console),
|
2012-01-26 17:24:56 +00:00
|
|
|
#ifdef RANDOM_DEBUG
|
|
|
|
DEBUG_LEVEL(random),
|
|
|
|
#endif
|
2005-05-20 17:59:24 +00:00
|
|
|
};
|
|
|
|
#undef DEBUG_LEVEL
|
2005-02-05 15:58:59 +00:00
|
|
|
|
2012-04-07 20:55:55 +00:00
|
|
|
/**
|
|
|
|
* Dump the available debug facility names in the help text.
|
|
|
|
* @param buf Start address for storing the output.
|
|
|
|
* @param last Last valid address for storing the output.
|
|
|
|
* @return Next free position in the output.
|
|
|
|
*/
|
|
|
|
char *DumpDebugFacilityNames(char *buf, char *last)
|
|
|
|
{
|
2012-04-09 13:08:20 +00:00
|
|
|
size_t length = 0;
|
2012-04-07 20:55:55 +00:00
|
|
|
for (const DebugLevel *i = debug_level; i != endof(debug_level); ++i) {
|
|
|
|
if (length == 0) {
|
|
|
|
buf = strecpy(buf, "List of debug facility names:\n", last);
|
|
|
|
} else {
|
|
|
|
buf = strecpy(buf, ", ", last);
|
|
|
|
length += 2;
|
|
|
|
}
|
|
|
|
buf = strecpy(buf, i->name, last);
|
|
|
|
length += strlen(i->name);
|
|
|
|
}
|
|
|
|
if (length > 0) {
|
|
|
|
buf = strecpy(buf, "\n\n", last);
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2010-09-11 11:48:37 +00:00
|
|
|
/**
|
|
|
|
* Internal function for outputting the debug line.
|
|
|
|
* @param dbg Debug category.
|
|
|
|
* @param buf Text line to output.
|
|
|
|
*/
|
2008-07-18 12:11:46 +00:00
|
|
|
static void debug_print(const char *dbg, const char *buf)
|
2006-12-28 19:38:09 +00:00
|
|
|
{
|
2008-01-24 18:47:05 +00:00
|
|
|
if (_debug_socket != INVALID_SOCKET) {
|
2008-06-03 18:35:58 +00:00
|
|
|
char buf2[1024 + 32];
|
2007-02-08 12:27:53 +00:00
|
|
|
|
2014-04-23 21:12:09 +00:00
|
|
|
seprintf(buf2, lastof(buf2), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
|
2013-11-23 13:15:07 +00:00
|
|
|
/* Sending out an error when this fails would be nice, however... the error
|
|
|
|
* would have to be send over this failing socket which won't work. */
|
2008-05-27 21:41:00 +00:00
|
|
|
send(_debug_socket, buf2, (int)strlen(buf2), 0);
|
2009-08-20 10:23:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-01-26 17:24:56 +00:00
|
|
|
if (strcmp(dbg, "desync") == 0) {
|
|
|
|
static FILE *f = FioFOpenFile("commands-out.log", "wb", AUTOSAVE_DIR);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (f == nullptr) return;
|
2012-01-26 17:24:56 +00:00
|
|
|
|
|
|
|
fprintf(f, "%s%s\n", GetLogPrefix(), buf);
|
|
|
|
fflush(f);
|
|
|
|
#ifdef RANDOM_DEBUG
|
|
|
|
} else if (strcmp(dbg, "random") == 0) {
|
|
|
|
static FILE *f = FioFOpenFile("random-out.log", "wb", AUTOSAVE_DIR);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (f == nullptr) return;
|
2012-01-26 17:24:56 +00:00
|
|
|
|
|
|
|
fprintf(f, "%s\n", buf);
|
|
|
|
fflush(f);
|
|
|
|
#endif
|
|
|
|
} else {
|
2013-08-05 20:36:47 +00:00
|
|
|
char buffer[512];
|
|
|
|
seprintf(buffer, lastof(buffer), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
|
2018-12-09 01:28:14 +00:00
|
|
|
#if defined(_WIN32)
|
2021-02-21 19:48:21 +00:00
|
|
|
wchar_t system_buf[512];
|
2021-04-02 16:35:00 +00:00
|
|
|
convert_to_fs(buffer, system_buf, lengthof(system_buf));
|
2021-02-21 16:03:19 +00:00
|
|
|
fputws(system_buf, stderr);
|
2007-08-03 23:26:12 +00:00
|
|
|
#else
|
2013-08-05 20:36:47 +00:00
|
|
|
fputs(buffer, stderr);
|
2007-08-03 23:26:12 +00:00
|
|
|
#endif
|
2010-10-17 17:43:01 +00:00
|
|
|
NetworkAdminConsole(dbg, buf);
|
2008-01-24 18:47:05 +00:00
|
|
|
IConsoleDebug(dbg, buf);
|
2006-12-28 19:38:09 +00:00
|
|
|
}
|
|
|
|
}
|
2008-06-03 18:35:58 +00:00
|
|
|
|
2010-09-11 11:48:37 +00:00
|
|
|
/**
|
|
|
|
* Output a debug line.
|
|
|
|
* @note Do not call directly, use the #DEBUG macro instead.
|
|
|
|
* @param dbg Debug category.
|
|
|
|
* @param format Text string a la printf, with optional arguments.
|
|
|
|
*/
|
2009-05-10 17:27:25 +00:00
|
|
|
void CDECL debug(const char *dbg, const char *format, ...)
|
2008-06-03 18:35:58 +00:00
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
|
2009-05-10 17:27:25 +00:00
|
|
|
va_list va;
|
|
|
|
va_start(va, format);
|
2014-04-24 19:51:45 +00:00
|
|
|
vseprintf(buf, lastof(buf), format, va);
|
2008-06-03 18:35:58 +00:00
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
debug_print(dbg, buf);
|
|
|
|
}
|
2006-12-28 19:38:09 +00:00
|
|
|
|
2010-09-11 11:48:37 +00:00
|
|
|
/**
|
|
|
|
* Set debugging levels by parsing the text in \a s.
|
|
|
|
* For setting individual levels a string like \c "net=3,grf=6" should be used.
|
|
|
|
* If the string starts with a number, the number is used as global debugging level.
|
|
|
|
* @param s Text describing the wanted debugging levels.
|
|
|
|
*/
|
2005-02-05 15:58:59 +00:00
|
|
|
void SetDebugString(const char *s)
|
|
|
|
{
|
|
|
|
int v;
|
|
|
|
char *end;
|
|
|
|
const char *t;
|
|
|
|
|
2007-02-23 18:55:07 +00:00
|
|
|
/* global debugging level? */
|
2005-02-05 15:58:59 +00:00
|
|
|
if (*s >= '0' && *s <= '9') {
|
|
|
|
const DebugLevel *i;
|
|
|
|
|
|
|
|
v = strtoul(s, &end, 0);
|
|
|
|
s = end;
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
for (i = debug_level; i != endof(debug_level); ++i) *i->level = v;
|
2005-02-05 15:58:59 +00:00
|
|
|
}
|
|
|
|
|
2007-02-23 18:55:07 +00:00
|
|
|
/* individual levels */
|
2005-11-14 19:48:04 +00:00
|
|
|
for (;;) {
|
2005-02-05 15:58:59 +00:00
|
|
|
const DebugLevel *i;
|
|
|
|
int *p;
|
|
|
|
|
2007-02-23 18:55:07 +00:00
|
|
|
/* skip delimiters */
|
2005-02-05 15:58:59 +00:00
|
|
|
while (*s == ' ' || *s == ',' || *s == '\t') s++;
|
|
|
|
if (*s == '\0') break;
|
|
|
|
|
|
|
|
t = s;
|
|
|
|
while (*s >= 'a' && *s <= 'z') s++;
|
|
|
|
|
2007-02-23 18:55:07 +00:00
|
|
|
/* check debugging levels */
|
2019-04-10 21:07:06 +00:00
|
|
|
p = nullptr;
|
2010-07-24 10:14:39 +00:00
|
|
|
for (i = debug_level; i != endof(debug_level); ++i) {
|
2005-02-05 15:58:59 +00:00
|
|
|
if (s == t + strlen(i->name) && strncmp(t, i->name, s - t) == 0) {
|
|
|
|
p = i->level;
|
|
|
|
break;
|
|
|
|
}
|
2010-07-24 10:14:39 +00:00
|
|
|
}
|
2005-02-05 15:58:59 +00:00
|
|
|
|
|
|
|
if (*s == '=') s++;
|
|
|
|
v = strtoul(s, &end, 0);
|
|
|
|
s = end;
|
2019-04-10 21:07:06 +00:00
|
|
|
if (p != nullptr) {
|
2005-02-05 15:58:59 +00:00
|
|
|
*p = v;
|
2005-11-14 19:48:04 +00:00
|
|
|
} else {
|
2009-05-10 17:27:25 +00:00
|
|
|
ShowInfoF("Unknown debug level '%.*s'", (int)(s - t), t);
|
2005-02-05 15:58:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-05-20 17:59:24 +00:00
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
2010-09-11 11:48:37 +00:00
|
|
|
* Print out the current debug-level.
|
|
|
|
* Just return a string with the values of all the debug categories.
|
2005-05-20 17:59:24 +00:00
|
|
|
* @return string with debug-levels
|
|
|
|
*/
|
2007-03-07 11:47:46 +00:00
|
|
|
const char *GetDebugString()
|
2005-05-20 17:59:24 +00:00
|
|
|
{
|
|
|
|
const DebugLevel *i;
|
2008-01-04 10:09:57 +00:00
|
|
|
static char dbgstr[150];
|
2005-05-20 17:59:24 +00:00
|
|
|
char dbgval[20];
|
|
|
|
|
|
|
|
memset(dbgstr, 0, sizeof(dbgstr));
|
|
|
|
i = debug_level;
|
2014-04-23 21:12:09 +00:00
|
|
|
seprintf(dbgstr, lastof(dbgstr), "%s=%d", i->name, *i->level);
|
2005-05-20 17:59:24 +00:00
|
|
|
|
|
|
|
for (i++; i != endof(debug_level); i++) {
|
2014-04-23 21:12:09 +00:00
|
|
|
seprintf(dbgval, lastof(dbgval), ", %s=%d", i->name, *i->level);
|
2008-11-02 11:20:15 +00:00
|
|
|
strecat(dbgstr, dbgval, lastof(dbgstr));
|
2005-05-20 17:59:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return dbgstr;
|
|
|
|
}
|
2009-09-09 15:11:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the prefix for logs; if show_date_in_logs is enabled it returns
|
|
|
|
* the date, otherwise it returns nothing.
|
2019-04-10 21:07:06 +00:00
|
|
|
* @return the prefix for logs (do not free), never nullptr
|
2009-09-09 15:11:46 +00:00
|
|
|
*/
|
|
|
|
const char *GetLogPrefix()
|
|
|
|
{
|
|
|
|
static char _log_prefix[24];
|
|
|
|
if (_settings_client.gui.show_date_in_logs) {
|
2019-04-10 21:07:06 +00:00
|
|
|
time_t cur_time = time(nullptr);
|
2009-09-09 15:11:46 +00:00
|
|
|
strftime(_log_prefix, sizeof(_log_prefix), "[%Y-%m-%d %H:%M:%S] ", localtime(&cur_time));
|
|
|
|
} else {
|
|
|
|
*_log_prefix = '\0';
|
|
|
|
}
|
|
|
|
return _log_prefix;
|
|
|
|
}
|
|
|
|
|