Merge branch upstream/master while introducing compile errors and reverting parts of Android changes, video BPP options are gone
This commit is contained in:
121
src/debug.cpp
121
src/debug.cpp
@@ -14,6 +14,8 @@
|
||||
#include "string_func.h"
|
||||
#include "fileio_func.h"
|
||||
#include "settings_type.h"
|
||||
#include <mutex>
|
||||
|
||||
#ifdef __ANDROID__
|
||||
#include <android/log.h>
|
||||
#endif
|
||||
@@ -22,13 +24,23 @@
|
||||
#include "os/windows/win32.h"
|
||||
#endif
|
||||
|
||||
#include <time.h>
|
||||
#include "walltime_func.h"
|
||||
|
||||
#include "network/network_admin.h"
|
||||
SOCKET _debug_socket = INVALID_SOCKET;
|
||||
|
||||
#include "safeguards.h"
|
||||
|
||||
/** 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.
|
||||
|
||||
int _debug_driver_level;
|
||||
int _debug_grf_level;
|
||||
int _debug_map_level;
|
||||
@@ -103,70 +115,52 @@ char *DumpDebugFacilityNames(char *buf, char *last)
|
||||
|
||||
/**
|
||||
* Internal function for outputting the debug line.
|
||||
* @param dbg Debug category.
|
||||
* @param buf Text line to output.
|
||||
* @param level Debug category.
|
||||
* @param message The message to output.
|
||||
*/
|
||||
static void debug_print(const char *dbg, const char *buf)
|
||||
void DebugPrint(const char *level, const std::string &message)
|
||||
{
|
||||
#ifdef __ANDROID__
|
||||
__android_log_print(ANDROID_LOG_INFO, "OpenTTD", "[%s] %s", dbg, buf);
|
||||
#endif
|
||||
if (_debug_socket != INVALID_SOCKET) {
|
||||
char buf2[1024 + 32];
|
||||
std::string msg = fmt::format("{}dbg: [{}] {}\n", GetLogPrefix(), level, message);
|
||||
|
||||
/* Prevent sending a message concurrently, as that might cause interleaved messages. */
|
||||
static std::mutex _debug_socket_mutex;
|
||||
std::lock_guard<std::mutex> lock(_debug_socket_mutex);
|
||||
|
||||
seprintf(buf2, lastof(buf2), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
|
||||
/* 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. */
|
||||
send(_debug_socket, buf2, (int)strlen(buf2), 0);
|
||||
send(_debug_socket, msg.c_str(), (int)msg.size(), 0);
|
||||
return;
|
||||
}
|
||||
if (strcmp(dbg, "desync") == 0) {
|
||||
if (strcmp(level, "desync") == 0) {
|
||||
static FILE *f = FioFOpenFile("commands-out.log", "wb", AUTOSAVE_DIR);
|
||||
if (f == nullptr) return;
|
||||
|
||||
fprintf(f, "%s%s\n", GetLogPrefix(), buf);
|
||||
fprintf(f, "%s%s\n", GetLogPrefix(), message.c_str());
|
||||
fflush(f);
|
||||
#ifdef RANDOM_DEBUG
|
||||
} else if (strcmp(dbg, "random") == 0) {
|
||||
} else if (strcmp(level, "random") == 0) {
|
||||
static FILE *f = FioFOpenFile("random-out.log", "wb", AUTOSAVE_DIR);
|
||||
if (f == nullptr) return;
|
||||
|
||||
fprintf(f, "%s\n", buf);
|
||||
fprintf(f, "%s\n", message.c_str());
|
||||
fflush(f);
|
||||
#endif
|
||||
} else {
|
||||
char buffer[512];
|
||||
seprintf(buffer, lastof(buffer), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
|
||||
#if defined(_WIN32)
|
||||
wchar_t system_buf[512];
|
||||
convert_to_fs(buffer, system_buf, lengthof(system_buf));
|
||||
fputws(system_buf, stderr);
|
||||
#else
|
||||
fputs(buffer, stderr);
|
||||
#endif
|
||||
NetworkAdminConsole(dbg, buf);
|
||||
IConsoleDebug(dbg, buf);
|
||||
std::string msg = fmt::format("{}dbg: [{}] {}\n", GetLogPrefix(), level, message);
|
||||
fputs(msg.c_str(), stderr);
|
||||
|
||||
if (_debug_remote_console.load()) {
|
||||
/* Only add to the queue when there is at least one consumer of the data. */
|
||||
std::lock_guard<std::mutex> lock(_debug_remote_console_mutex);
|
||||
_debug_remote_console_queue.push_back({ level, message });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
void CDECL debug(const char *dbg, const char *format, ...)
|
||||
{
|
||||
char buf[1024];
|
||||
|
||||
va_list va;
|
||||
va_start(va, format);
|
||||
vseprintf(buf, lastof(buf), format, va);
|
||||
va_end(va);
|
||||
|
||||
debug_print(dbg, buf);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
@@ -254,11 +248,54 @@ const char *GetLogPrefix()
|
||||
{
|
||||
static char _log_prefix[24];
|
||||
if (_settings_client.gui.show_date_in_logs) {
|
||||
time_t cur_time = time(nullptr);
|
||||
strftime(_log_prefix, sizeof(_log_prefix), "[%Y-%m-%d %H:%M:%S] ", localtime(&cur_time));
|
||||
LocalTime::Format(_log_prefix, lastof(_log_prefix), "[%Y-%m-%d %H:%M:%S] ");
|
||||
} else {
|
||||
*_log_prefix = '\0';
|
||||
}
|
||||
return _log_prefix;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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, item.message);
|
||||
if (_settings_client.gui.developer >= 2) IConsolePrint(CC_DEBUG, "dbg: [{}] {}", item.level, item.message);
|
||||
}
|
||||
|
||||
_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;
|
||||
|
||||
for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::IterateActive()) {
|
||||
if (as->update_frequency[ADMIN_UPDATE_CONSOLE] & ADMIN_FREQUENCY_AUTOMATIC) {
|
||||
enable = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
_debug_remote_console.store(enable);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user