OpenTTD Source 20261112-master-gbcbec5e63f
debug.h
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef DEBUG_H
11#define DEBUG_H
12
13#include <chrono>
14#include "debug_type.h"
15#include "core/enum_type.hpp"
16#include "core/format.hpp"
17
24inline bool IsVisibleSeverity(Facility facility, Severity severity)
25{
27 return _debug_level[facility] >= severity;
28}
29
36#define Debug(facility, severity, format_string, ...) do { if ((severity) == Severity::Critical || IsVisibleSeverity((facility), (severity))) DebugPrint(facility, severity, fmt::format(FMT_STRING(format_string) __VA_OPT__(,) __VA_ARGS__)); } while (false)
37void DebugPrint(Facility facility, Severity severity, std::string &&message);
38
39void DumpDebugFacilityNames(std::back_insert_iterator<std::string> &output_iterator);
40using SetDebugStringErrorFunc = void(std::string_view);
41void SetDebugString(std::string_view s, SetDebugStringErrorFunc error_func);
42std::string GetDebugString();
43
55struct TicToc {
57 struct State {
58 const std::string_view name;
59 const std::optional<uint32_t> max_count;
60 uint32_t count = 0;
61 uint64_t chrono_sum = 0;
62
63 using States = std::vector<State *>;
64
65 State(std::string_view name, std::optional<uint32_t> max_count = {}) : name(name), max_count(max_count)
66 {
67 GetStates().push_back(this);
68 }
69
72 {
73 /* Container might be already destroyed. */
74 if (!GetStates().empty()) std::erase(GetStates(), this);
75 }
76
77 static States &GetStates()
78 {
79 thread_local static States s_states;
80 return s_states;
81 }
82
83 void OutputAndReset(const std::string_view prefix = "")
84 {
85 Debug(Facility::Misc, Severity::Critical, "[{}] [{}] {} calls in {} us [avg: {:.1f} us]", prefix, this->name, this->count, this->chrono_sum, this->chrono_sum / static_cast<double>(this->count));
86 this->count = 0;
87 this->chrono_sum = 0;
88 }
89 };
90
91 State &state;
92 std::chrono::high_resolution_clock::time_point chrono_start;
93
94 inline TicToc(State &state) : state(state), chrono_start(std::chrono::high_resolution_clock::now()) { }
95
97 inline ~TicToc()
98 {
99 this->state.chrono_sum += (std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - this->chrono_start)).count();
100 this->state.count++;
101 if (this->state.max_count.has_value() && this->state.count == this->state.max_count.value()) {
102 this->state.OutputAndReset("MaxCount");
103 }
104 }
105
106 static void Tick(const std::string_view prefix)
107 {
108 for (auto state : State::GetStates()) {
109 if (state->max_count.has_value() || state->count == 0) continue;
110 state->OutputAndReset(prefix);
111 }
112 }
113};
114
115void ShowInfoI(std::string_view str);
116#define ShowInfo(format_string, ...) ShowInfoI(fmt::format(FMT_STRING(format_string) __VA_OPT__(,) __VA_ARGS__))
117
118std::string GetLogPrefix(bool force = false);
119
122
123#endif /* DEBUG_H */
EnumIndexArray< Severity, Facility, Facility::End > _debug_level
Severity level for each debug facility.
Definition debug.cpp:39
void SetDebugString(std::string_view s, SetDebugStringErrorFunc error_func)
Set debugging levels by parsing the text in s.
Definition debug.cpp:121
void DebugReconsiderSendRemoteMessages()
Reconsider whether we need to send debug messages to either NetworkAdminConsole or IConsolePrint.
Definition debug.cpp:230
bool IsVisibleSeverity(Facility facility, Severity severity)
Test if debug severity is visible for the given facility.
Definition debug.h:24
std::string GetLogPrefix(bool force=false)
Get the prefix for logs.
Definition debug.cpp:190
void DumpDebugFacilityNames(std::back_insert_iterator< std::string > &output_iterator)
Dump the available debug facility names in the help text.
Definition debug.cpp:64
std::string GetDebugString()
Print out the current debug-level.
Definition debug.cpp:171
#define Debug(facility, severity, format_string,...)
Output a line of debugging information.
Definition debug.h:36
void DebugSendRemoteMessages()
Send the queued Debug messages to either NetworkAdminConsole or IConsolePrint from the GameLoop threa...
Definition debug.cpp:206
void DebugPrint(Facility facility, Severity severity, std::string &&message)
Internal function for outputting the debug line.
Definition debug.cpp:87
Types related to debugging.
Facility
Debug facilities.
Definition debug_type.h:28
@ Misc
Misc message facility.
Definition debug_type.h:32
Severity
Debug message severity levels.
Definition debug_type.h:14
@ Critical
Critical, user should know about this.
Definition debug_type.h:15
Type (helpers) for enums.
EnumClassIndexContainer< std::array< T, to_underlying(N)>, Index > EnumIndexArray
A typedef for EnumClassIndexContainer using std::array as the backing container type.
String formatting functions and helpers.
Persistent state for TicToc profiling.
Definition debug.h:57
~State()
Remove ourselves from the thread local states.
Definition debug.h:71
std::chrono::high_resolution_clock::time_point chrono_start
real time count.
Definition debug.h:92
~TicToc()
Update the state with the time since the constructor call.
Definition debug.h:97