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"
|
2019-05-22 18:57:16 +00:00
|
|
|
#include "date_func.h"
|
2021-07-23 20:36:17 +00:00
|
|
|
#include "thread.h"
|
2019-05-22 18:57:16 +00:00
|
|
|
#include <array>
|
2021-07-23 20:36:17 +00:00
|
|
|
#include <mutex>
|
2009-09-09 15:11:46 +00:00
|
|
|
|
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
|
|
|
|
|
2021-05-13 08:00:41 +00:00
|
|
|
#include "walltime_func.h"
|
2007-02-08 12:27:53 +00:00
|
|
|
|
2010-10-17 17:43:01 +00:00
|
|
|
#include "network/network_admin.h"
|
2005-02-05 15:58:59 +00:00
|
|
|
|
2015-11-30 19:23:47 +00:00
|
|
|
#if defined(RANDOM_DEBUG) && defined(UNIX) && defined(__GLIBC__)
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2021-07-23 20:36:17 +00:00
|
|
|
/** Element in the queue of debug messages that have to be passed to either NetworkAdminConsole or IConsolePrint.*/
|
|
|
|
struct QueuedDebugItem {
|
|
|
|
std::string level; ///< The used debug level.
|
|
|
|
std::string message; ///< The actual formatted message.
|
|
|
|
};
|
|
|
|
std::atomic<bool> _debug_remote_console; ///< Whether we need to send data to either NetworkAdminConsole or IConsolePrint.
|
|
|
|
std::mutex _debug_remote_console_mutex; ///< Mutex to guard the queue of debug messages for either NetworkAdminConsole or IConsolePrint.
|
|
|
|
std::vector<QueuedDebugItem> _debug_remote_console_queue; ///< Queue for debug messages to be passed to NetworkAdminConsole or IConsolePrint.
|
|
|
|
std::vector<QueuedDebugItem> _debug_remote_console_queue_spare; ///< Spare queue to swap with _debug_remote_console_queue.
|
|
|
|
|
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;
|
2022-09-15 17:21:27 +00:00
|
|
|
int _debug_fontcache_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;
|
2015-10-16 18:16:34 +00:00
|
|
|
int _debug_yapfdesync_level;
|
2010-02-10 16:10:05 +00:00
|
|
|
int _debug_console_level;
|
2016-10-31 00:21:01 +00:00
|
|
|
int _debug_linkgraph_level;
|
2017-08-11 18:52:21 +00:00
|
|
|
int _debug_sound_level;
|
2023-05-23 20:28:01 +00:00
|
|
|
int _debug_command_level;
|
2012-01-26 17:24:56 +00:00
|
|
|
#ifdef RANDOM_DEBUG
|
|
|
|
int _debug_random_level;
|
2021-01-18 00:58:43 +00:00
|
|
|
int _debug_statecsum_level;
|
2012-01-26 17:24:56 +00:00
|
|
|
#endif
|
2005-02-05 15:58:59 +00:00
|
|
|
|
2019-05-21 00:49:40 +00:00
|
|
|
const char *_savegame_DBGL_data = nullptr;
|
|
|
|
std::string _loadgame_DBGL_data;
|
2020-04-21 17:17:13 +00:00
|
|
|
bool _save_DBGC_data = false;
|
|
|
|
std::string _loadgame_DBGC_data;
|
2019-05-21 00:49:40 +00:00
|
|
|
|
2024-01-07 16:41:53 +00:00
|
|
|
uint32_t _misc_debug_flags;
|
2021-04-11 23:18:13 +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),
|
2022-09-15 17:21:27 +00:00
|
|
|
DEBUG_LEVEL(fontcache),
|
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),
|
2015-10-16 18:16:34 +00:00
|
|
|
DEBUG_LEVEL(yapfdesync),
|
2010-02-10 16:12:54 +00:00
|
|
|
DEBUG_LEVEL(console),
|
2016-10-31 00:21:01 +00:00
|
|
|
DEBUG_LEVEL(linkgraph),
|
2017-08-11 18:52:21 +00:00
|
|
|
DEBUG_LEVEL(sound),
|
2023-05-23 20:28:01 +00:00
|
|
|
DEBUG_LEVEL(command),
|
2012-01-26 17:24:56 +00:00
|
|
|
#ifdef RANDOM_DEBUG
|
|
|
|
DEBUG_LEVEL(random),
|
2021-01-18 00:58:43 +00:00
|
|
|
DEBUG_LEVEL(statecsum),
|
2012-01-26 17:24:56 +00:00
|
|
|
#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.
|
|
|
|
*/
|
2023-03-03 23:59:49 +00:00
|
|
|
void debug_print(const char *dbg, const char *buf)
|
2006-12-28 19:38:09 +00:00
|
|
|
{
|
2007-02-08 12:27:53 +00:00
|
|
|
|
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-11 17:14:13 +00:00
|
|
|
if (f != nullptr) {
|
2023-12-29 23:56:33 +00:00
|
|
|
fprintf(f, "%s%s\n", log_prefix().GetLogPrefix(), buf);
|
2015-09-27 14:09:11 +00:00
|
|
|
fflush(f);
|
|
|
|
}
|
2012-01-26 17:24:56 +00:00
|
|
|
#ifdef RANDOM_DEBUG
|
2021-01-18 00:58:43 +00:00
|
|
|
} else if (strcmp(dbg, "random") == 0 || strcmp(dbg, "statecsum") == 0) {
|
2015-11-30 19:23:47 +00:00
|
|
|
#if defined(UNIX) && defined(__GLIBC__)
|
|
|
|
static bool have_inited = false;
|
2019-04-11 17:14:13 +00:00
|
|
|
static FILE *f = nullptr;
|
2015-11-30 19:23:47 +00:00
|
|
|
|
|
|
|
if (!have_inited) {
|
|
|
|
have_inited = true;
|
|
|
|
unsigned int num = 0;
|
|
|
|
int pid = getpid();
|
|
|
|
for(;;) {
|
2023-05-29 23:53:31 +00:00
|
|
|
std::string fn = stdstr_fmt("random-out-%d-%u.log", pid, num);
|
|
|
|
f = FioFOpenFile(fn.c_str(), "wx", AUTOSAVE_DIR);
|
2019-04-11 17:14:13 +00:00
|
|
|
if (f == nullptr && errno == EEXIST) {
|
2015-11-30 19:23:47 +00:00
|
|
|
num++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2012-01-26 17:24:56 +00:00
|
|
|
static FILE *f = FioFOpenFile("random-out.log", "wb", AUTOSAVE_DIR);
|
2015-11-30 19:23:47 +00:00
|
|
|
#endif
|
2019-04-11 17:14:13 +00:00
|
|
|
if (f != nullptr) {
|
2015-09-27 14:09:11 +00:00
|
|
|
fprintf(f, "%s\n", buf);
|
|
|
|
return;
|
|
|
|
}
|
2012-01-26 17:24:56 +00:00
|
|
|
#endif
|
2015-09-27 14:09:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char buffer[512];
|
2023-12-29 23:56:33 +00:00
|
|
|
seprintf(buffer, lastof(buffer), "%sdbg: [%s] %s\n", log_prefix().GetLogPrefix(), dbg, buf);
|
2015-11-23 18:34:57 +00:00
|
|
|
|
2016-02-16 19:52:41 +00:00
|
|
|
str_strip_colours(buffer);
|
|
|
|
|
2015-11-23 18:34:57 +00:00
|
|
|
/* do not write desync messages to the console on Windows platforms, as they do
|
|
|
|
* not seem able to handle text direction change characters in a console without
|
|
|
|
* crashing, and NetworkTextMessage includes these */
|
2018-12-09 01:28:14 +00:00
|
|
|
#if defined(_WIN32)
|
2018-06-01 22:32:33 +00:00
|
|
|
if (strcmp(dbg, "desync") != 0) {
|
2021-11-02 00:51:54 +00:00
|
|
|
fputs(buffer, stderr);
|
2018-06-01 22:32:33 +00:00
|
|
|
}
|
2007-08-03 23:26:12 +00:00
|
|
|
#else
|
2015-09-27 14:09:11 +00:00
|
|
|
fputs(buffer, stderr);
|
2007-08-03 23:26:12 +00:00
|
|
|
#endif
|
2015-11-23 18:34:57 +00:00
|
|
|
|
2021-07-23 20:36:17 +00:00
|
|
|
if (_debug_remote_console.load()) {
|
|
|
|
/* Only add to the queue when there is at least one consumer of the data. */
|
|
|
|
if (IsNonGameThread()) {
|
|
|
|
std::lock_guard<std::mutex> lock(_debug_remote_console_mutex);
|
|
|
|
_debug_remote_console_queue.push_back({ dbg, buf });
|
|
|
|
} else {
|
|
|
|
NetworkAdminConsole(dbg, buf);
|
|
|
|
if (_settings_client.gui.developer >= 2) IConsolePrintF(CC_DEBUG, "dbg: [%s] %s", 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.
|
2022-08-26 10:08:47 +00:00
|
|
|
* @param error_func The function to call if a parse error occurs.
|
2010-09-11 11:48:37 +00:00
|
|
|
*/
|
2022-08-26 10:08:47 +00:00
|
|
|
void SetDebugString(const char *s, void (*error_func)(const char *))
|
2005-02-05 15:58:59 +00:00
|
|
|
{
|
|
|
|
int v;
|
|
|
|
char *end;
|
|
|
|
const char *t;
|
|
|
|
|
2022-08-26 10:45:02 +00:00
|
|
|
/* Store planned changes into map during parse */
|
|
|
|
std::map<const char *, int> new_levels;
|
|
|
|
|
|
|
|
/* Global debugging level? */
|
2005-02-05 15:58:59 +00:00
|
|
|
if (*s >= '0' && *s <= '9') {
|
|
|
|
const DebugLevel *i;
|
|
|
|
|
2023-04-26 11:56:14 +00:00
|
|
|
v = std::strtoul(s, &end, 0);
|
2005-02-05 15:58:59 +00:00
|
|
|
s = end;
|
|
|
|
|
2022-08-26 10:45:02 +00:00
|
|
|
for (i = debug_level; i != endof(debug_level); ++i) {
|
|
|
|
new_levels[i->name] = v;
|
|
|
|
}
|
2005-02-05 15:58:59 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 10:45:02 +00:00
|
|
|
/* Individual levels */
|
2005-11-14 19:48:04 +00:00
|
|
|
for (;;) {
|
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 */
|
2022-08-26 10:45:02 +00:00
|
|
|
const DebugLevel *found = nullptr;
|
|
|
|
for (const DebugLevel *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) {
|
2022-08-26 10:45:02 +00:00
|
|
|
found = i;
|
2005-02-05 15:58:59 +00:00
|
|
|
break;
|
|
|
|
}
|
2010-07-24 10:14:39 +00:00
|
|
|
}
|
2005-02-05 15:58:59 +00:00
|
|
|
|
|
|
|
if (*s == '=') s++;
|
2023-04-26 11:56:14 +00:00
|
|
|
v = std::strtoul(s, &end, 0);
|
2005-02-05 15:58:59 +00:00
|
|
|
s = end;
|
2022-08-26 10:45:02 +00:00
|
|
|
if (found != nullptr) {
|
|
|
|
new_levels[found->name] = v;
|
2005-11-14 19:48:04 +00:00
|
|
|
} else {
|
2022-08-27 14:50:50 +00:00
|
|
|
char buf[1024];
|
|
|
|
seprintf(buf, lastof(buf), "Unknown debug level '%*s'", (int)(s - t), t);
|
|
|
|
error_func(buf);
|
2005-02-05 15:58:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2022-08-26 10:45:02 +00:00
|
|
|
|
|
|
|
/* Apply the changes after parse is successful */
|
|
|
|
for (const DebugLevel *i = debug_level; i != endof(debug_level); ++i) {
|
|
|
|
const auto &nl = new_levels.find(i->name);
|
|
|
|
if (nl != new_levels.end()) {
|
|
|
|
*i->level = nl->second;
|
|
|
|
}
|
|
|
|
}
|
2005-02-05 15:58:59 +00:00
|
|
|
}
|
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
|
|
|
|
*/
|
2023-06-01 18:31:55 +00:00
|
|
|
std::string GetDebugString()
|
2005-05-20 17:59:24 +00:00
|
|
|
{
|
2023-06-01 18:31:55 +00:00
|
|
|
std::string result;
|
|
|
|
for (size_t i = 0; i < lengthof(debug_level); i++) {
|
|
|
|
result += stdstr_fmt("%s%s=%d", i == 0 ? "" : ", ", debug_level[i].name, *(debug_level[i].level));
|
2005-05-20 17:59:24 +00:00
|
|
|
}
|
2023-06-01 18:31:55 +00:00
|
|
|
return result;
|
2005-05-20 17:59:24 +00:00
|
|
|
}
|
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
|
|
|
*/
|
2023-12-29 23:56:33 +00:00
|
|
|
const char *log_prefix::GetLogPrefix()
|
2009-09-09 15:11:46 +00:00
|
|
|
{
|
|
|
|
if (_settings_client.gui.show_date_in_logs) {
|
2023-12-29 23:56:33 +00:00
|
|
|
LocalTime::Format(this->buffer, lastof(this->buffer), "[%Y-%m-%d %H:%M:%S] ");
|
2009-09-09 15:11:46 +00:00
|
|
|
} else {
|
2023-12-29 23:56:33 +00:00
|
|
|
this->buffer[0] = '\0';
|
2009-09-09 15:11:46 +00:00
|
|
|
}
|
2023-12-29 23:56:33 +00:00
|
|
|
return this->buffer;
|
2009-09-09 15:11:46 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 18:57:16 +00:00
|
|
|
struct DesyncMsgLogEntry {
|
|
|
|
Date date;
|
|
|
|
DateFract date_fract;
|
2024-01-07 16:41:53 +00:00
|
|
|
uint8_t tick_skip_counter;
|
|
|
|
uint32_t src_id;
|
2019-05-22 18:57:16 +00:00
|
|
|
std::string msg;
|
|
|
|
|
|
|
|
DesyncMsgLogEntry() { }
|
|
|
|
|
|
|
|
DesyncMsgLogEntry(std::string msg)
|
2021-04-24 00:15:05 +00:00
|
|
|
: date(_date), date_fract(_date_fract), tick_skip_counter(_tick_skip_counter), src_id(0), msg(msg) { }
|
2019-05-22 18:57:16 +00:00
|
|
|
};
|
|
|
|
|
2021-04-23 23:21:50 +00:00
|
|
|
struct DesyncMsgLog {
|
2021-10-04 20:01:03 +00:00
|
|
|
std::array<DesyncMsgLogEntry, 256> log;
|
2021-04-23 23:21:50 +00:00
|
|
|
unsigned int count = 0;
|
|
|
|
unsigned int next = 0;
|
|
|
|
|
|
|
|
void Clear()
|
|
|
|
{
|
|
|
|
this->count = 0;
|
|
|
|
this->next = 0;
|
|
|
|
}
|
2019-05-22 18:57:16 +00:00
|
|
|
|
2021-04-24 00:15:05 +00:00
|
|
|
void LogMsg(DesyncMsgLogEntry entry)
|
2021-04-23 23:21:50 +00:00
|
|
|
{
|
2021-04-24 00:15:05 +00:00
|
|
|
this->log[this->next] = std::move(entry);
|
2021-04-23 23:21:50 +00:00
|
|
|
this->next = (this->next + 1) % this->log.size();
|
|
|
|
this->count++;
|
|
|
|
}
|
2019-05-22 18:57:16 +00:00
|
|
|
|
2021-04-24 00:15:05 +00:00
|
|
|
template <typename F>
|
|
|
|
char *Dump(char *buffer, const char *last, const char *prefix, F handler)
|
2021-04-23 23:21:50 +00:00
|
|
|
{
|
|
|
|
if (!this->count) return buffer;
|
2019-05-22 18:57:16 +00:00
|
|
|
|
2021-06-11 21:54:27 +00:00
|
|
|
const unsigned int count = std::min<unsigned int>(this->count, (uint)this->log.size());
|
|
|
|
unsigned int log_index = (this->next + (uint)this->log.size() - count) % (uint)this->log.size();
|
2021-04-23 23:21:50 +00:00
|
|
|
unsigned int display_num = this->count - count;
|
2019-05-22 18:57:16 +00:00
|
|
|
|
2021-04-24 00:15:05 +00:00
|
|
|
buffer += seprintf(buffer, last, "%s:\n Showing most recent %u of %u messages\n", prefix, count, this->count);
|
2019-05-22 18:57:16 +00:00
|
|
|
|
2021-04-23 23:21:50 +00:00
|
|
|
for (unsigned int i = 0 ; i < count; i++) {
|
|
|
|
const DesyncMsgLogEntry &entry = this->log[log_index];
|
2019-05-22 18:57:16 +00:00
|
|
|
|
2021-04-24 00:15:05 +00:00
|
|
|
buffer += handler(display_num, buffer, last, entry);
|
2021-04-23 23:21:50 +00:00
|
|
|
log_index = (log_index + 1) % this->log.size();
|
|
|
|
display_num++;
|
|
|
|
}
|
|
|
|
buffer += seprintf(buffer, last, "\n");
|
|
|
|
return buffer;
|
2019-05-22 18:57:16 +00:00
|
|
|
}
|
2021-04-23 23:21:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static DesyncMsgLog _desync_msg_log;
|
2021-04-24 00:15:05 +00:00
|
|
|
static DesyncMsgLog _remote_desync_msg_log;
|
2021-04-23 23:21:50 +00:00
|
|
|
|
|
|
|
void ClearDesyncMsgLog()
|
|
|
|
{
|
|
|
|
_desync_msg_log.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
char *DumpDesyncMsgLog(char *buffer, const char *last)
|
|
|
|
{
|
2021-04-24 00:15:05 +00:00
|
|
|
buffer = _desync_msg_log.Dump(buffer, last, "Desync Msg Log", [](int display_num, char *buffer, const char *last, const DesyncMsgLogEntry &entry) -> int {
|
2023-12-29 14:46:53 +00:00
|
|
|
YearMonthDay ymd = ConvertDateToYMD(entry.date);
|
2021-04-24 00:15:05 +00:00
|
|
|
return seprintf(buffer, last, "%5u | %4i-%02i-%02i, %2i, %3i | %s\n", display_num, ymd.year, ymd.month + 1, ymd.day, entry.date_fract, entry.tick_skip_counter, entry.msg.c_str());
|
|
|
|
});
|
|
|
|
buffer = _remote_desync_msg_log.Dump(buffer, last, "Remote Client Desync Msg Log", [](int display_num, char *buffer, const char *last, const DesyncMsgLogEntry &entry) -> int {
|
2023-12-29 14:46:53 +00:00
|
|
|
YearMonthDay ymd = ConvertDateToYMD(entry.date);
|
2021-04-24 00:15:05 +00:00
|
|
|
return seprintf(buffer, last, "%5u | Client %5u | %4i-%02i-%02i, %2i, %3i | %s\n", display_num, entry.src_id, ymd.year, ymd.month + 1, ymd.day, entry.date_fract, entry.tick_skip_counter, entry.msg.c_str());
|
|
|
|
});
|
2019-05-22 18:57:16 +00:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LogDesyncMsg(std::string msg)
|
|
|
|
{
|
2021-04-24 00:15:05 +00:00
|
|
|
if (_networking && !_network_server) {
|
|
|
|
NetworkClientSendDesyncMsg(msg.c_str());
|
|
|
|
}
|
|
|
|
_desync_msg_log.LogMsg(DesyncMsgLogEntry(std::move(msg)));
|
|
|
|
}
|
|
|
|
|
2024-01-07 16:41:53 +00:00
|
|
|
void LogRemoteDesyncMsg(Date date, DateFract date_fract, uint8_t tick_skip_counter, uint32_t src_id, std::string msg)
|
2021-04-24 00:15:05 +00:00
|
|
|
{
|
|
|
|
DesyncMsgLogEntry entry(std::move(msg));
|
|
|
|
entry.date = date;
|
|
|
|
entry.date_fract = date_fract;
|
|
|
|
entry.tick_skip_counter = tick_skip_counter;
|
|
|
|
entry.src_id = src_id;
|
|
|
|
_remote_desync_msg_log.LogMsg(std::move(entry));
|
2019-05-22 18:57:16 +00:00
|
|
|
}
|
2021-07-23 20:36:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send the queued Debug messages to either NetworkAdminConsole or IConsolePrint from the
|
|
|
|
* GameLoop thread to prevent concurrent accesses to both the NetworkAdmin's packet queue
|
|
|
|
* as well as IConsolePrint's buffers.
|
|
|
|
*
|
|
|
|
* This is to be called from the GameLoop thread.
|
|
|
|
*/
|
|
|
|
void DebugSendRemoteMessages()
|
|
|
|
{
|
|
|
|
if (!_debug_remote_console.load()) return;
|
|
|
|
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(_debug_remote_console_mutex);
|
|
|
|
std::swap(_debug_remote_console_queue, _debug_remote_console_queue_spare);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &item : _debug_remote_console_queue_spare) {
|
|
|
|
NetworkAdminConsole(item.level.c_str(), item.message.c_str());
|
|
|
|
if (_settings_client.gui.developer >= 2) IConsolePrintF(CC_DEBUG, "dbg: [%s] %s", item.level.c_str(), item.message.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
_debug_remote_console_queue_spare.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reconsider whether we need to send debug messages to either NetworkAdminConsole
|
|
|
|
* or IConsolePrint. The former is when they have enabled console handling whereas
|
|
|
|
* the latter depends on the gui.developer setting's value.
|
|
|
|
*
|
|
|
|
* This is to be called from the GameLoop thread.
|
|
|
|
*/
|
|
|
|
void DebugReconsiderSendRemoteMessages()
|
|
|
|
{
|
|
|
|
bool enable = _settings_client.gui.developer >= 2;
|
|
|
|
|
|
|
|
if (!enable) {
|
|
|
|
for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::IterateActive()) {
|
|
|
|
if (as->update_frequency[ADMIN_UPDATE_CONSOLE] & ADMIN_FREQUENCY_AUTOMATIC) {
|
|
|
|
enable = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_debug_remote_console.store(enable);
|
|
|
|
}
|