Moonlight-XboxOG latest
Moonlight Xbox OG is a port of the Moonlight Game Streaming client to the original Xbox console.
logger.h
Go to the documentation of this file.
1
5#pragma once
6
7// standard includes
8#include <cstddef>
9#include <cstdint>
10#include <deque>
11#include <functional>
12#include <mutex>
13#include <string>
14#include <string_view>
15#include <vector>
16
17namespace logging {
18
22 enum class LogLevel {
23 trace = 0,
24 debug = 1,
25 info = 2,
26 warning = 3,
27 error = 4,
28 none = 5,
29 };
30
34 struct LogTimestamp {
35 int year = 0;
36 int month = 0;
37 int day = 0;
38 int hour = 0;
39 int minute = 0;
40 int second = 0;
41 int millisecond = 0;
42 };
43
48 const char *file = nullptr;
49 int line = 0;
50
58 [[nodiscard]] static constexpr LogSourceLocation current(
59#if defined(__clang__) || defined(__GNUC__)
60 const char *currentFile = __builtin_FILE(),
61 int currentLine = __builtin_LINE()
62#else
63 const char *currentFile = nullptr,
64 int currentLine = 0
65#endif
66 ) noexcept {
67 return {currentFile, currentLine};
68 }
69
75 [[nodiscard]] bool valid() const {
76 return file != nullptr && file[0] != '\0' && line > 0;
77 }
78 };
79
83 struct LogEntry {
84 uint64_t sequence = 0;
85 LogLevel level = LogLevel::info;
86 std::string category;
87 std::string message;
90 };
91
95 using LogSink = std::function<void(const LogEntry &entry)>;
96
100 using TimestampProvider = std::function<LogTimestamp()>;
101
102 class Logger;
103
104 namespace detail {
105
110 inline static Logger *registeredLogger = nullptr;
111 inline static bool startupConsoleEnabled = true;
112 };
113
114 } // namespace detail
115
122 const char *to_string(LogLevel level);
123
130 std::string format_timestamp(const LogTimestamp &timestamp);
131
138 std::string format_source_location(const LogSourceLocation &location);
139
146 std::string format_entry(const LogEntry &entry);
147
153 void set_global_logger(Logger *logger);
154
160 [[nodiscard]] bool has_global_logger();
161
171 bool log(LogLevel level, std::string category, std::string message, LogSourceLocation location = LogSourceLocation::current());
172
181 bool trace(std::string category, std::string message, LogSourceLocation location = LogSourceLocation::current());
182
191 bool debug(std::string category, std::string message, LogSourceLocation location = LogSourceLocation::current());
192
201 bool info(std::string category, std::string message, LogSourceLocation location = LogSourceLocation::current());
202
211 bool warn(std::string category, std::string message, LogSourceLocation location = LogSourceLocation::current());
212
221 bool error(std::string category, std::string message, LogSourceLocation location = LogSourceLocation::current());
222
228 void set_minimum_level(LogLevel minimumLevel);
229
235 void set_file_sink(LogSink sink);
236
242 void set_file_minimum_level(LogLevel minimumLevel);
243
250
256 void set_startup_debug_enabled(bool enabled);
257
264 [[nodiscard]] std::vector<LogEntry> snapshot(LogLevel minimumLevel = LogLevel::trace);
265
274 [[nodiscard]] std::string format_startup_console_line(LogLevel level, std::string_view category, std::string_view message);
275
281 void set_startup_console_enabled(bool enabled);
282
288 [[nodiscard]] bool startup_console_enabled();
289
297 void print_startup_console_line(LogLevel level, std::string_view category, std::string_view message);
298
302 class Logger {
303 public:
310 explicit Logger(std::size_t capacity = 256, TimestampProvider timestampProvider = {});
311
317 std::size_t capacity() const;
318
324 void set_minimum_level(LogLevel minimumLevel);
325
331 LogLevel minimum_level() const;
332
338 void set_startup_debug_enabled(bool enabled);
339
345 bool startup_debug_enabled() const;
346
352 void set_file_sink(LogSink sink);
353
359 void set_file_minimum_level(LogLevel minimumLevel);
360
367
374
381
388 bool should_log(LogLevel level) const;
389
399 bool log(LogLevel level, std::string category, std::string message, LogSourceLocation location = LogSourceLocation::current());
400
409 bool trace(std::string category, std::string message, LogSourceLocation location = LogSourceLocation::current());
410
419 bool debug(std::string category, std::string message, LogSourceLocation location = LogSourceLocation::current());
420
429 bool info(std::string category, std::string message, LogSourceLocation location = LogSourceLocation::current());
430
439 bool warn(std::string category, std::string message, LogSourceLocation location = LogSourceLocation::current());
440
449 bool error(std::string category, std::string message, LogSourceLocation location = LogSourceLocation::current());
450
457 void add_sink(LogSink sink, LogLevel minimumLevel = LogLevel::trace);
458
464 const std::deque<LogEntry> &entries() const;
465
472 std::vector<LogEntry> snapshot(LogLevel minimumLevel = LogLevel::trace) const;
473
474 private:
475 struct RegisteredSink {
476 LogLevel minimumLevel = LogLevel::trace;
477 LogSink sink;
478 };
479
480 bool should_log_unlocked(LogLevel level) const;
481
482 std::size_t capacity_;
483 mutable std::mutex mutex_;
484 LogLevel minimumLevel_ = LogLevel::none;
485 bool startupDebugEnabled_ = true;
486 LogSink fileSink_;
487 LogLevel fileMinimumLevel_ = LogLevel::none;
488 LogLevel debuggerConsoleMinimumLevel_ = LogLevel::none;
489 uint64_t nextSequence_ = 1;
490 TimestampProvider timestampProvider_;
491 std::deque<LogEntry> entries_;
492 std::vector<RegisteredSink> sinks_;
493 };
494
495} // namespace logging
Small in-memory logger with a ring buffer and optional sinks.
Definition logger.h:302
Logger(std::size_t capacity=256, TimestampProvider timestampProvider={})
Construct a logger with the provided entry capacity.
Definition logger.cpp:298
bool trace(std::string category, std::string message, LogSourceLocation location=LogSourceLocation::current())
Record a trace entry.
Definition logger.cpp:418
std::vector< LogEntry > snapshot(LogLevel minimumLevel=LogLevel::trace) const
Copy retained entries at or above the requested level.
Definition logger.cpp:449
void set_file_minimum_level(LogLevel minimumLevel)
Set the minimum level written to the configured file sink.
Definition logger.cpp:321
const std::deque< LogEntry > & entries() const
Return the retained entries.
Definition logger.cpp:445
bool should_log(LogLevel level) const
Return whether a log level would be recorded by any enabled sink.
Definition logger.cpp:351
void set_debugger_console_minimum_level(LogLevel minimumLevel)
Set the minimum level mirrored through DbgPrint() for xemu.
Definition logger.cpp:341
bool log(LogLevel level, std::string category, std::string message, LogSourceLocation location=LogSourceLocation::current())
Record a structured entry.
Definition logger.cpp:375
void add_sink(LogSink sink, LogLevel minimumLevel=LogLevel::trace)
Register an observer that receives accepted entries.
Definition logger.cpp:438
bool info(std::string category, std::string message, LogSourceLocation location=LogSourceLocation::current())
Record an info entry.
Definition logger.cpp:426
LogLevel file_minimum_level() const
Return the minimum level written to the configured file sink.
Definition logger.cpp:326
LogLevel debugger_console_minimum_level() const
Return the minimum level mirrored through DbgPrint() for xemu.
Definition logger.cpp:346
bool error(std::string category, std::string message, LogSourceLocation location=LogSourceLocation::current())
Record an error entry.
Definition logger.cpp:434
void set_startup_debug_enabled(bool enabled)
Enable or disable pre-splash startup output through debugPrint().
Definition logger.cpp:331
void set_minimum_level(LogLevel minimumLevel)
Set the minimum retained in-memory log level.
Definition logger.cpp:306
void set_file_sink(LogSink sink)
Install or replace the runtime file sink callback.
Definition logger.cpp:316
bool debug(std::string category, std::string message, LogSourceLocation location=LogSourceLocation::current())
Record a debug entry.
Definition logger.cpp:422
LogLevel minimum_level() const
Return the minimum retained in-memory log level.
Definition logger.cpp:311
std::size_t capacity() const
Return the maximum number of retained entries.
Definition logger.cpp:302
bool startup_debug_enabled() const
Return whether pre-splash startup output is currently enabled.
Definition logger.cpp:336
bool warn(std::string category, std::string message, LogSourceLocation location=LogSourceLocation::current())
Record a warning entry.
Definition logger.cpp:430
@ logging
Logging and diagnostics options.
std::string format_entry(const LogEntry &entry)
Format a log entry for text consoles or overlays.
Definition logger.cpp:172
void set_startup_console_enabled(bool enabled)
Enable or disable startup console output.
Definition logger.cpp:275
bool info(std::string category, std::string message, LogSourceLocation location)
Record an info entry through the registered global logger.
Definition logger.cpp:215
std::string format_startup_console_line(LogLevel level, std::string_view category, std::string_view message)
Format one startup console line without timestamps or source locations.
Definition logger.cpp:265
void print_startup_console_line(LogLevel level, std::string_view category, std::string_view message)
Print one startup console line when output is enabled.
Definition logger.cpp:283
std::string format_timestamp(const LogTimestamp &timestamp)
Format a local wall-clock timestamp for log prefixes.
Definition logger.cpp:141
void set_global_logger(Logger *logger)
Register the process-wide logger used by convenience logging helpers.
Definition logger.cpp:191
bool log(LogLevel level, std::string category, std::string message, LogSourceLocation location)
Record a structured entry through the registered global logger.
Definition logger.cpp:199
bool has_global_logger()
Return whether a global logger is currently available.
Definition logger.cpp:195
void set_minimum_level(LogLevel minimumLevel)
Update the retained in-memory minimum level on the registered global logger.
Definition logger.cpp:227
std::vector< LogEntry > snapshot(LogLevel minimumLevel)
Return a filtered snapshot from the registered global logger.
Definition logger.cpp:257
void set_file_minimum_level(LogLevel minimumLevel)
Update the runtime file sink minimum level on the registered global logger.
Definition logger.cpp:239
bool warn(std::string category, std::string message, LogSourceLocation location)
Record a warning entry through the registered global logger.
Definition logger.cpp:219
void set_startup_debug_enabled(bool enabled)
Enable or disable startup debug mirroring on the registered global logger.
Definition logger.cpp:251
bool trace(std::string category, std::string message, LogSourceLocation location)
Record a trace entry through the registered global logger.
Definition logger.cpp:207
void set_debugger_console_minimum_level(LogLevel minimumLevel)
Update the debugger-console minimum level on the registered global logger.
Definition logger.cpp:245
bool debug(std::string category, std::string message, LogSourceLocation location)
Record a debug entry through the registered global logger.
Definition logger.cpp:211
bool error(std::string category, std::string message, LogSourceLocation location)
Record an error entry through the registered global logger.
Definition logger.cpp:223
bool startup_console_enabled()
Return whether startup console output is currently enabled.
Definition logger.cpp:279
void set_file_sink(LogSink sink)
Install or replace the runtime file sink on the registered global logger.
Definition logger.cpp:233
std::string format_source_location(const LogSourceLocation &location)
Format a source location for text consoles or overlays.
Definition logger.cpp:159
LogLevel
Severity levels used by the Moonlight client logger.
Definition logger.h:22
@ warning
Recoverable issue or degraded behavior.
std::function< void(const LogEntry &entry)> LogSink
Callback invoked for each accepted log entry.
Definition logger.h:95
std::function< LogTimestamp()> TimestampProvider
Callback that supplies timestamps for new log entries.
Definition logger.h:100
Structured log entry stored by the in-memory logger.
Definition logger.h:83
std::string category
Subsystem category such as ui or network.
Definition logger.h:86
LogTimestamp timestamp
Local wall-clock timestamp captured for the entry.
Definition logger.h:88
std::string message
Human-readable log message.
Definition logger.h:87
uint64_t sequence
Monotonic sequence number assigned by the logger.
Definition logger.h:84
LogSourceLocation sourceLocation
Optional file-and-line source location for the entry.
Definition logger.h:89
LogLevel level
Severity associated with the entry.
Definition logger.h:85
Optional source location captured for a structured log entry.
Definition logger.h:47
const char * file
Translation-unit file path where the entry originated.
Definition logger.h:48
static constexpr LogSourceLocation current(const char *currentFile=nullptr, int currentLine=0) noexcept
Capture the current call-site source location when the compiler supports it.
Definition logger.h:58
int line
One-based source line number where the entry originated.
Definition logger.h:49
bool valid() const
Return whether this source-location payload contains usable data.
Definition logger.h:75
Local wall-clock timestamp captured for each retained log entry.
Definition logger.h:34
int day
One-based day of month in local time.
Definition logger.h:37
int year
Full calendar year in local time.
Definition logger.h:35
int millisecond
Millisecond component in local time.
Definition logger.h:41
int hour
Hour component in 24-hour local time.
Definition logger.h:38
int minute
Minute component in local time.
Definition logger.h:39
int second
Second component in local time.
Definition logger.h:40
int month
One-based calendar month in local time.
Definition logger.h:36
Process-wide mutable logger state shared by the logging helpers.
Definition logger.h:109
static Logger * registeredLogger
Process-wide logger used by namespace-level helpers.
Definition logger.h:110
static bool startupConsoleEnabled
True when startup console output is enabled.
Definition logger.h:111