OpenTTD Source  13.2.1
debug.h File Reference
#include "cpu.h"
#include <chrono>
#include "3rdparty/fmt/format.h"

Go to the source code of this file.

Macros

#define Debug(name, level, format_string, ...)   if ((level) == 0 || _debug_ ## name ## _level >= (level)) DebugPrint(#name, fmt::format(FMT_STRING(format_string), ## __VA_ARGS__))
 Ouptut a line of debugging information. More...
 
#define FILE_LINE   __FILE__, __LINE__
 
#define TIC()
 
#define TOC(str, count)
 
#define TICC()
 
#define TOCC(str, _count_)
 

Functions

void DebugPrint (const char *level, const std::string &message)
 Internal function for outputting the debug line. More...
 
char * DumpDebugFacilityNames (char *buf, char *last)
 Dump the available debug facility names in the help text. More...
 
void SetDebugString (const char *s, void(*error_func)(const char *))
 Set debugging levels by parsing the text in s. More...
 
const char * GetDebugString ()
 Print out the current debug-level. More...
 
void ShowInfo (const char *str)
 
void CDECL ShowInfoF (const char *str,...) WARN_FORMAT(1
 
void CDECL const char * GetLogPrefix ()
 Get the prefix for logs; if show_date_in_logs is enabled it returns the date, otherwise it returns nothing. More...
 
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. More...
 
void DebugReconsiderSendRemoteMessages ()
 Reconsider whether we need to send debug messages to either NetworkAdminConsole or IConsolePrint. More...
 

Variables

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_npf_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
 

Detailed Description

Functions related to debugging.

Definition in file debug.h.

Macro Definition Documentation

◆ Debug

#define Debug (   name,
  level,
  format_string,
  ... 
)    if ((level) == 0 || _debug_ ## name ## _level >= (level)) DebugPrint(#name, fmt::format(FMT_STRING(format_string), ## __VA_ARGS__))

Ouptut a line of debugging information.

Parameters
nameThe category of debug information.
levelThe maximum debug level this message should be shown at. When the debug level for this category is set lower, then the message will not be shown.
format_stringThe formatting string of the message.

Definition at line 37 of file debug.h.

◆ TIC

#define TIC ( )
Value:
{\
uint64 _xxx_ = ottd_rdtsc();\
static uint64 _sum_ = 0;\
static uint32 _i_ = 0;

Definition at line 90 of file debug.h.

◆ TICC

#define TICC ( )
Value:
{\
auto _start_ = std::chrono::high_resolution_clock::now();\
static uint64 _sum_ = 0;\
static uint32 _i_ = 0;

Definition at line 105 of file debug.h.

◆ TOC

#define TOC (   str,
  count 
)
Value:
_sum_ += ottd_rdtsc() - _xxx_;\
if (++_i_ == count) {\
Debug(misc, 0, "[{}] {} [avg: {:.1f}]", str, _sum_, _sum_/(double)_i_);\
_i_ = 0;\
_sum_ = 0;\
}\
}

Definition at line 95 of file debug.h.

◆ TOCC

#define TOCC (   str,
  _count_ 
)
Value:
_sum_ += (std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - _start_)).count();\
if (++_i_ == _count_) {\
Debug(misc, 0, "[{}] {} us [avg: {:.1f} us]", str, _sum_, _sum_/(double)_i_);\
_i_ = 0;\
_sum_ = 0;\
}\
}

Definition at line 110 of file debug.h.

Function Documentation

◆ DebugPrint()

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

Internal function for outputting the debug line.

Parameters
levelDebug category.
messageThe message to output.

Definition at line 117 of file debug.cpp.

◆ 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 294 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 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 270 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()

char* DumpDebugFacilityNames ( char *  buf,
char *  last 
)

Dump the available debug facility names in the help text.

Parameters
bufStart address for storing the output.
lastLast valid address for storing the output.
Returns
Next free position in the output.

Definition at line 93 of file debug.cpp.

◆ GetDebugString()

const char* 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 229 of file debug.cpp.

◆ GetLogPrefix()

void CDECL const char* GetLogPrefix ( )

Get the prefix for logs; if show_date_in_logs is enabled it returns the date, otherwise it returns nothing.

Returns
the prefix for logs (do not free), never nullptr

Definition at line 252 of file debug.cpp.

References _settings_client, Time< T >::Format(), ClientSettings::gui, lastof, and GUISettings::show_date_in_logs.

◆ SetDebugString()

void SetDebugString ( const char *  s,
void(*)(const char *)  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 164 of file debug.cpp.

ottd_rdtsc
uint64 ottd_rdtsc()
Get the tick counter from the CPU (high precision timing).
Definition: cpu.cpp:98