OpenTTD Source 20241224-master-gf74b0cf984
debug.cpp File Reference

Handling of printing debug messages. More...

#include "stdafx.h"
#include "console_func.h"
#include "debug.h"
#include "string_func.h"
#include "fileio_func.h"
#include "settings_type.h"
#include <mutex>
#include "3rdparty/fmt/chrono.h"
#include "network/network_admin.h"
#include "safeguards.h"

Go to the source code of this file.

Data Structures

struct  QueuedDebugItem
 Element in the queue of debug messages that have to be passed to either NetworkAdminConsole or IConsolePrint. More...
 
struct  DebugLevel
 

Macros

#define DEBUG_LEVEL(x)   { #x, &_debug_##x##_level }
 

Functions

void DumpDebugFacilityNames (std::back_insert_iterator< std::string > &output_iterator)
 Dump the available debug facility names in the help text.
 
void DebugPrint (const char *category, int level, const std::string &message)
 Internal function for outputting the debug line.
 
void SetDebugString (const char *s, void(*error_func)(const std::string &))
 Set debugging levels by parsing the text in s.
 
std::string GetDebugString ()
 Print out the current debug-level.
 
std::string GetLogPrefix (bool force)
 Get the prefix for logs.
 
void DebugSendRemoteMessages ()
 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.
 
void DebugReconsiderSendRemoteMessages ()
 Reconsider whether we need to send debug messages to either NetworkAdminConsole or IConsolePrint.
 

Variables

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
 
int _debug_misc_level
 
int _debug_net_level
 
int _debug_sprite_level
 
int _debug_oldloader_level
 
int _debug_yapf_level
 
int _debug_fontcache_level
 
int _debug_script_level
 
int _debug_sl_level
 
int _debug_gamelog_level
 
int _debug_desync_level
 
int _debug_console_level
 
static const DebugLevel _debug_levels []
 

Detailed Description

Handling of printing debug messages.

Definition in file debug.cpp.

Macro Definition Documentation

◆ DEBUG_LEVEL

#define DEBUG_LEVEL (   x)    { #x, &_debug_##x##_level }

Definition at line 61 of file debug.cpp.

Function Documentation

◆ DebugPrint()

void DebugPrint ( const char *  category,
int  level,
const std::string &  message 
)

Internal function for outputting the debug line.

Parameters
levelDebug category.
messageThe message to output.

Definition at line 109 of file debug.cpp.

References _debug_remote_console, _debug_remote_console_mutex, _debug_remote_console_queue, AUTOSAVE_DIR, FioFOpenFile(), GetLogPrefix(), and lock.

◆ DebugReconsiderSendRemoteMessages()

void DebugReconsiderSendRemoteMessages ( )

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.

Definition at line 264 of file debug.cpp.

References _debug_remote_console, _settings_client, ADMIN_FREQUENCY_AUTOMATIC, ADMIN_UPDATE_CONSOLE, GUISettings::developer, ClientSettings::gui, and ServerNetworkAdminSocketHandler::IterateActive().

Referenced by LoadFromConfig(), ServerNetworkAdminSocketHandler::Receive_ADMIN_UPDATE_FREQUENCY(), and ServerNetworkAdminSocketHandler::~ServerNetworkAdminSocketHandler().

◆ DebugSendRemoteMessages()

void DebugSendRemoteMessages ( )

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.

Definition at line 240 of file debug.cpp.

References _debug_remote_console, _debug_remote_console_mutex, _debug_remote_console_queue, _debug_remote_console_queue_spare, _settings_client, CC_DEBUG, GUISettings::developer, ClientSettings::gui, IConsolePrint(), lock, and NetworkAdminConsole().

◆ DumpDebugFacilityNames()

void DumpDebugFacilityNames ( std::back_insert_iterator< std::string > &  output_iterator)

Dump the available debug facility names in the help text.

Parameters
output_iteratorThe iterator to write the string to.

Definition at line 87 of file debug.cpp.

Referenced by ShowHelp().

◆ GetDebugString()

std::string GetDebugString ( )

Print out the current debug-level.

Just return a string with the values of all the debug categories.

Returns
string with debug-levels

Definition at line 206 of file debug.cpp.

◆ GetLogPrefix()

std::string GetLogPrefix ( bool  force)

Get the prefix for logs.

If show_date_in_logs or force is enabled it returns the date, otherwise it returns an empty string.

Returns
the prefix for logs.

Definition at line 224 of file debug.cpp.

References _settings_client, ClientSettings::gui, and GUISettings::show_date_in_logs.

Referenced by DebugPrint(), and IConsolePrint().

◆ SetDebugString()

void SetDebugString ( const char *  s,
void(*)(const std::string &)  error_func 
)

Set debugging levels by parsing the text in s.

For setting individual levels a string like "net=3,grf=6" should be used. If the string starts with a number, the number is used as global debugging level.

Parameters
sText describing the wanted debugging levels.
error_funcThe function to call if a parse error occurs.

Definition at line 143 of file debug.cpp.

Referenced by openttd_main().

Variable Documentation

◆ _debug_console_level

int _debug_console_level

Definition at line 51 of file debug.cpp.

◆ _debug_desync_level

int _debug_desync_level

Definition at line 50 of file debug.cpp.

◆ _debug_driver_level

int _debug_driver_level

Definition at line 38 of file debug.cpp.

◆ _debug_fontcache_level

int _debug_fontcache_level

Definition at line 46 of file debug.cpp.

◆ _debug_gamelog_level

int _debug_gamelog_level

Definition at line 49 of file debug.cpp.

◆ _debug_grf_level

int _debug_grf_level

Definition at line 39 of file debug.cpp.

◆ _debug_levels

const DebugLevel _debug_levels[]
static
Initial value:
= {
DEBUG_LEVEL(driver),
DEBUG_LEVEL(grf),
DEBUG_LEVEL(map),
DEBUG_LEVEL(misc),
DEBUG_LEVEL(net),
DEBUG_LEVEL(sprite),
DEBUG_LEVEL(oldloader),
DEBUG_LEVEL(yapf),
DEBUG_LEVEL(fontcache),
DEBUG_LEVEL(script),
DEBUG_LEVEL(sl),
DEBUG_LEVEL(gamelog),
DEBUG_LEVEL(desync),
DEBUG_LEVEL(console),
}

Definition at line 62 of file debug.cpp.

◆ _debug_map_level

int _debug_map_level

Definition at line 40 of file debug.cpp.

◆ _debug_misc_level

int _debug_misc_level

Definition at line 41 of file debug.cpp.

◆ _debug_net_level

int _debug_net_level

Definition at line 42 of file debug.cpp.

◆ _debug_oldloader_level

int _debug_oldloader_level

Definition at line 44 of file debug.cpp.

◆ _debug_remote_console

std::atomic<bool> _debug_remote_console

Whether we need to send data to either NetworkAdminConsole or IConsolePrint.

Definition at line 33 of file debug.cpp.

Referenced by DebugPrint(), DebugReconsiderSendRemoteMessages(), and DebugSendRemoteMessages().

◆ _debug_remote_console_mutex

std::mutex _debug_remote_console_mutex

Mutex to guard the queue of debug messages for either NetworkAdminConsole or IConsolePrint.

Definition at line 34 of file debug.cpp.

Referenced by DebugPrint(), and DebugSendRemoteMessages().

◆ _debug_remote_console_queue

std::vector<QueuedDebugItem> _debug_remote_console_queue

Queue for debug messages to be passed to NetworkAdminConsole or IConsolePrint.

Definition at line 35 of file debug.cpp.

Referenced by DebugPrint(), and DebugSendRemoteMessages().

◆ _debug_remote_console_queue_spare

std::vector<QueuedDebugItem> _debug_remote_console_queue_spare

Spare queue to swap with _debug_remote_console_queue.

Definition at line 36 of file debug.cpp.

Referenced by DebugSendRemoteMessages().

◆ _debug_script_level

int _debug_script_level

Definition at line 47 of file debug.cpp.

◆ _debug_sl_level

int _debug_sl_level

Definition at line 48 of file debug.cpp.

◆ _debug_sprite_level

int _debug_sprite_level

Definition at line 43 of file debug.cpp.

◆ _debug_yapf_level

int _debug_yapf_level

Definition at line 45 of file debug.cpp.