OpenTTD Source 20260206-master-g4d4e37dbf1
debug.cpp
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#include "stdafx.h"
12#include "console_func.h"
13#include "debug.h"
14#include "string_func.h"
15#include "fileio_func.h"
16#include "settings_type.h"
17#include <mutex>
18
19#if defined(_WIN32)
20#include "os/windows/win32.h"
21#endif
22
23#include "3rdparty/fmt/chrono.h"
24
26
27#include "safeguards.h"
28
31 std::string_view level;
32 std::string message;
33};
34std::atomic<bool> _debug_remote_console;
36std::vector<QueuedDebugItem> _debug_remote_console_queue;
37std::vector<QueuedDebugItem> _debug_remote_console_queue_spare;
38
39int _debug_driver_level;
40int _debug_grf_level;
41int _debug_map_level;
42int _debug_misc_level;
43int _debug_net_level;
44int _debug_sprite_level;
45int _debug_oldloader_level;
46int _debug_yapf_level;
47int _debug_fontcache_level;
48int _debug_script_level;
49int _debug_sl_level;
50int _debug_gamelog_level;
51int _debug_desync_level;
52int _debug_console_level;
53#ifdef RANDOM_DEBUG
54int _debug_random_level;
55#endif
56
57struct DebugLevel {
58 std::string_view name;
59 int *level;
60};
61
62#define DEBUG_LEVEL(x) { #x, &_debug_##x##_level }
63static const std::initializer_list<DebugLevel> _debug_levels{
64 DEBUG_LEVEL(driver),
65 DEBUG_LEVEL(grf),
66 DEBUG_LEVEL(map),
67 DEBUG_LEVEL(misc),
68 DEBUG_LEVEL(net),
69 DEBUG_LEVEL(sprite),
70 DEBUG_LEVEL(oldloader),
71 DEBUG_LEVEL(yapf),
72 DEBUG_LEVEL(fontcache),
73 DEBUG_LEVEL(script),
74 DEBUG_LEVEL(sl),
75 DEBUG_LEVEL(gamelog),
76 DEBUG_LEVEL(desync),
77 DEBUG_LEVEL(console),
78#ifdef RANDOM_DEBUG
79 DEBUG_LEVEL(random),
80#endif
81};
82#undef DEBUG_LEVEL
83
88void DumpDebugFacilityNames(std::back_insert_iterator<std::string> &output_iterator)
89{
90 bool written = false;
91 for (const auto &debug_level : _debug_levels) {
92 if (!written) {
93 fmt::format_to(output_iterator, "List of debug facility names:\n");
94 } else {
95 fmt::format_to(output_iterator, ", ");
96 }
97 fmt::format_to(output_iterator, "{}", debug_level.name);
98 written = true;
99 }
100 if (written) {
101 fmt::format_to(output_iterator, "\n\n");
102 }
103}
104
111void DebugPrint(std::string_view category, int level, std::string &&message)
112{
113 if (category == "desync" && level != 0) {
114 static auto f = FioFOpenFile("commands-out.log", "wb", AUTOSAVE_DIR);
115 if (!f.has_value()) return;
116
117 fmt::print(*f, "{}{}\n", GetLogPrefix(true), message);
118 fflush(*f);
119#ifdef RANDOM_DEBUG
120 } else if (category == "random") {
121 static auto f = FioFOpenFile("random-out.log", "wb", AUTOSAVE_DIR);
122 if (!f.has_value()) return;
123
124 fmt::print(*f, "{}\n", message);
125 fflush(*f);
126#endif
127 } else {
128 fmt::print(stderr, "{}dbg: [{}:{}] {}\n", GetLogPrefix(true), category, level, message);
129
130 if (_debug_remote_console.load()) {
131 /* Only add to the queue when there is at least one consumer of the data. */
132 std::lock_guard<std::mutex> lock(_debug_remote_console_mutex);
133 _debug_remote_console_queue.emplace_back(category, std::move(message));
134 }
135 }
136}
137
145void SetDebugString(std::string_view s, SetDebugStringErrorFunc error_func)
146{
147 StringConsumer consumer{s};
148
149 /* Store planned changes into map during parse */
150 std::map<std::string_view, int> new_levels;
151
152 /* Global debugging level? */
153 auto level = consumer.TryReadIntegerBase<int>(10);
154 if (level.has_value()) {
155 for (const auto &debug_level : _debug_levels) {
156 new_levels[debug_level.name] = *level;
157 }
158 }
159
160 static const std::string_view lowercase_letters{"abcdefghijklmnopqrstuvwxyz"};
161 static const std::string_view lowercase_letters_and_digits{"abcdefghijklmnopqrstuvwxyz0123456789"};
162
163 /* Individual levels */
164 while (consumer.AnyBytesLeft()) {
165 consumer.SkipUntilCharIn(lowercase_letters);
166 if (!consumer.AnyBytesLeft()) break;
167
168 /* Find the level by name. */
169 std::string_view key = consumer.ReadUntilCharNotIn(lowercase_letters);
170 auto it = std::ranges::find(_debug_levels, key, &DebugLevel::name);
171 if (it == std::end(_debug_levels)) {
172 error_func(fmt::format("Unknown debug level '{}'", key));
173 return;
174 }
175
176 /* Do not skip lowercase letters, so 'net misc=2' won't be resolved
177 * to setting 'net=2' and leaving misc untouched. */
178 consumer.SkipUntilCharIn(lowercase_letters_and_digits);
179 level = consumer.TryReadIntegerBase<int>(10);
180 if (!level.has_value()) {
181 error_func(fmt::format("Level for '{}' must be a valid integer.", key));
182 return;
183 }
184
185 new_levels[it->name] = *level;
186 }
187
188 /* Apply the changes after parse is successful */
189 for (const auto &debug_level : _debug_levels) {
190 const auto &nl = new_levels.find(debug_level.name);
191 if (nl != new_levels.end()) {
192 *debug_level.level = nl->second;
193 }
194 }
195}
196
202std::string GetDebugString()
203{
204 std::string result;
205 for (const auto &debug_level : _debug_levels) {
206 if (!result.empty()) result += ", ";
207 format_append(result, "{}={}", debug_level.name, *debug_level.level);
208 }
209 return result;
210}
211
220std::string GetLogPrefix(bool force)
221{
222 std::string log_prefix;
223 if (force || _settings_client.gui.show_date_in_logs) {
224 log_prefix = fmt::format("[{:%Y-%m-%d %H:%M:%S}] ", fmt::localtime(time(nullptr)));
225 }
226 return log_prefix;
227}
228
237{
238 if (!_debug_remote_console.load()) return;
239
240 {
241 std::lock_guard<std::mutex> lock(_debug_remote_console_mutex);
243 }
244
245 for (auto &item : _debug_remote_console_queue_spare) {
246 NetworkAdminConsole(item.level, item.message);
247 if (_settings_client.gui.developer >= 2) IConsolePrint(CC_DEBUG, "dbg: [{}] {}", item.level, item.message);
248 }
249
251}
252
261{
262 bool enable = _settings_client.gui.developer >= 2;
263
265 if (as->update_frequency[ADMIN_UPDATE_CONSOLE].Test(AdminUpdateFrequency::Automatic)) {
266 enable = true;
267 break;
268 }
269 }
270
271 _debug_remote_console.store(enable);
272}
Class for handling the server side of the game connection.
static Pool::IterateWrapperFiltered< ServerNetworkAdminSocketHandler, ServerNetworkAdminSocketHandlerFilter > IterateActive(size_t from=0)
Returns an iterable ensemble of all active admin sockets.
Parse data from a string / buffer.
std::optional< T > TryReadIntegerBase(int base, bool clamp=false)
Try to read and parse an integer in number 'base', and then advance the reader.
bool AnyBytesLeft() const noexcept
Check whether any bytes left to read.
std::string_view ReadUntilCharNotIn(std::string_view chars)
Read 8-bit chars, while they are in 'chars', until they are not; and advance reader.
void SkipUntilCharIn(std::string_view chars)
Skip 8-bit chars, while they are not in 'chars', until they are.
void IConsolePrint(TextColour colour_code, const std::string &string)
Handle the printing of text entered into the console or redirected there by any other means.
Definition console.cpp:90
Console functions used outside of the console code.
static const TextColour CC_DEBUG
Colour for debug output.
std::string GetLogPrefix(bool force)
Get the prefix for logs.
Definition debug.cpp:220
std::vector< QueuedDebugItem > _debug_remote_console_queue
Queue for debug messages to be passed to NetworkAdminConsole or IConsolePrint.
Definition debug.cpp:36
std::mutex _debug_remote_console_mutex
Mutex to guard the queue of debug messages for either NetworkAdminConsole or IConsolePrint.
Definition debug.cpp:35
void SetDebugString(std::string_view s, SetDebugStringErrorFunc error_func)
Set debugging levels by parsing the text in s.
Definition debug.cpp:145
void DebugReconsiderSendRemoteMessages()
Reconsider whether we need to send debug messages to either NetworkAdminConsole or IConsolePrint.
Definition debug.cpp:260
std::atomic< bool > _debug_remote_console
Whether we need to send data to either NetworkAdminConsole or IConsolePrint.
Definition debug.cpp:34
void DebugPrint(std::string_view category, int level, std::string &&message)
Internal function for outputting the debug line.
Definition debug.cpp:111
void DumpDebugFacilityNames(std::back_insert_iterator< std::string > &output_iterator)
Dump the available debug facility names in the help text.
Definition debug.cpp:88
std::vector< QueuedDebugItem > _debug_remote_console_queue_spare
Spare queue to swap with _debug_remote_console_queue.
Definition debug.cpp:37
std::string GetDebugString()
Print out the current debug-level.
Definition debug.cpp:202
void DebugSendRemoteMessages()
Send the queued Debug messages to either NetworkAdminConsole or IConsolePrint from the GameLoop threa...
Definition debug.cpp:236
Functions related to debugging.
std::optional< FileHandle > FioFOpenFile(std::string_view filename, std::string_view mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
Definition fileio.cpp:244
Functions for standard in/out file operations.
@ AUTOSAVE_DIR
Subdirectory of save for autosaves.
Definition fileio_type.h:91
void NetworkAdminConsole(std::string_view origin, std::string_view string)
Send console to the admin network (if they did opt in for the respective update).
Server part of the admin network protocol.
A number of safeguards to prevent using unsafe methods.
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:60
Types related to global configuration settings.
Definition of base types and functions in a cross-platform compatible way.
Parse strings.
Functions related to low-level strings.
Element in the queue of debug messages that have to be passed to either NetworkAdminConsole or IConso...
Definition debug.cpp:30
std::string_view level
The used debug level.
Definition debug.cpp:31
std::string message
The actual formatted message.
Definition debug.cpp:32
@ ADMIN_UPDATE_CONSOLE
The admin would like to have console messages.
Definition tcp_admin.h:85
@ Automatic
The admin gets information about this when it changes.
Definition tcp_admin.h:100
Declarations of functions for MS windows systems.
std::mutex lock
synchronization for playback status fields
Definition win32_m.cpp:35