Monad Engine da02fba2
> Undercity Codex_
Loading...
Searching...
No Matches
Log.h
Go to the documentation of this file.
1/**
2 * @file Log.h
3 * @ingroup mnd_types
4 * @brief Console logging macros for all engine and game code.
5 *
6 * Four severity levels: INFO, WARN, ERROR, FATAL. Each macro prefixes the
7 * message with the module-relative source file path and line number, prints
8 * to stdout/stderr with ANSI colour, and pushes the formatted line into a
9 * ring buffer the in-game editor's Console panel reads.
10 */
11
12#pragma once
13
14#include <cstdarg>
15#include <cstdlib>
16#include <cstring>
17#include <deque>
18#include <string>
19
20#include "Constants.h"
21#include "Types.h"
22
23namespace mnd
24{
25
26inline const char *shortFile(const char *file)
27{
28 static char buffer[kLogPrefixBufSize];
29
30 const char *ptr = std::strstr(file, "monad-engine/");
31 if (ptr != nullptr) {
32 ptr += 13;
33 } else {
34 ptr = file;
35 }
36
37 const char *module = nullptr;
38 if (std::strncmp(ptr, "engine/", 7) == 0) {
39 module = "engine/";
40 } else if (std::strncmp(ptr, "game/", 5) == 0) {
41 module = "game/";
42 } else {
43 return ptr;
44 }
45
46 const char *last_slash = std::strrchr(ptr, '/');
47 const char *base_name = (last_slash != nullptr) ? last_slash + 1 : ptr;
48
49 std::snprintf(buffer, sizeof(buffer), "%s%s", module, base_name);
50 return buffer;
51}
52
53enum class LogLevel : u8
54{
55 Info,
56 Warn,
57 Error,
58 Fatal
59};
60
62{
64 std::string text;
65};
66
67/// Emit a formatted log line to stdout/stderr and the ring buffer.
68void LogEmit(LogLevel level, const char *file, int line, const char *fmt, ...)
69 __attribute__((format(printf, 4, 5)));
70
71/// Snapshot of the in-memory log buffer (bounded ring).
72const std::deque<LogEntry> &LogGetEntries();
73
74/// Clear the in-memory log buffer.
75void LogClear();
76
77} // namespace mnd
78
79#define LOG_INFO(fmt, ...) ::mnd::LogEmit(::mnd::LogLevel::Info, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
80#define LOG_WARN(fmt, ...) ::mnd::LogEmit(::mnd::LogLevel::Warn, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
81#define LOG_ERROR(fmt, ...) ::mnd::LogEmit(::mnd::LogLevel::Error, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
82#define LOG_FATAL(fmt, ...) do { ::mnd::LogEmit(::mnd::LogLevel::Fatal, __FILE__, __LINE__, fmt, ##__VA_ARGS__); std::abort(); } while (0)
Engine-wide compile-time tunables and ANSI colour codes.
constexpr mnd::i32 kLogPrefixBufSize
Definition Constants.h:33
Engine-wide primitive type aliases and GLM math imports.
uint8_t u8
Unsigned 8-bit integer.
Definition Types.h:30
void LogClear()
Clear the in-memory log buffer.
Definition Log.cpp:67
const std::deque< LogEntry > & LogGetEntries()
Snapshot of the in-memory log buffer (bounded ring).
Definition Log.cpp:62
const char * shortFile(const char *file)
Definition Log.h:26
LogLevel
Definition Log.h:54
void LogEmit(LogLevel level, const char *file, int line, const char *fmt,...)
Emit a formatted log line to stdout/stderr and the ring buffer.
Definition Log.cpp:37
LogLevel level
Definition Log.h:63
std::string text
Definition Log.h:64