ZenitUI
Retained-mode C++20 UI framework
Loading...
Searching...
No Matches
Logger.hpp
Go to the documentation of this file.
1#pragma once
2
16
17#include "Common.hpp"
18#include <cstdio>
19
20namespace ZenitUI
21{
22
26
28 enum class LogLevel
29 {
30 Debug,
31 Info,
32 Warning,
33 Error
34 };
35
38 {
39 LogLevel level{LogLevel::Info};
40 std::string category;
41 std::string file;
42 int line{0};
43 int column{0};
44 std::string text;
45 };
46
48 class ILogger
49 {
50 public:
51 virtual ~ILogger() = default;
52 virtual void log(const LogMessage &msg) = 0;
53 };
54
56 class ConsoleLogger : public ILogger
57 {
58 public:
59 void log(const LogMessage &msg) override
60 {
61 const char *lvl = "INFO ";
62 switch (msg.level)
63 {
64 case LogLevel::Debug:
65 lvl = "DEBUG";
66 break;
67 case LogLevel::Info:
68 lvl = "INFO ";
69 break;
70 case LogLevel::Warning:
71 lvl = "WARN ";
72 break;
73 case LogLevel::Error:
74 lvl = "ERROR";
75 break;
76 }
77
78 std::fprintf(stderr, "[%s] [%s] ", lvl, msg.category.c_str());
79 if (!msg.file.empty())
80 {
81 if (msg.line > 0)
82 std::fprintf(stderr, "%s(%d:%d): ", msg.file.c_str(), msg.line, msg.column);
83 else
84 std::fprintf(stderr, "%s: ", msg.file.c_str());
85 }
86 std::fprintf(stderr, "%s\n", msg.text.c_str());
87 }
88 };
89
91 class Logger
92 {
93 public:
95 static ILogger &get() { return *instance(); }
96
101 static void set(ILogger *logger)
102 {
103 instance() = logger ? logger : defaultLogger();
104 }
105
106 private:
107 static ConsoleLogger *defaultLogger()
108 {
109 static ConsoleLogger l;
110 return &l;
111 }
112 static ILogger *&instance()
113 {
114 static ILogger *current = defaultLogger();
115 return current;
116 }
117 };
118
120 inline void logImpl(LogLevel level, const std::string &cat,
121 const std::string &file, int line, int col,
122 const std::string &text)
123 {
124 LogMessage m;
125 m.level = level;
126 m.category = cat;
127 m.file = file;
128 m.line = line;
129 m.column = col;
130 m.text = text;
131 Logger::get().log(m);
132 }
133
134 // ---- Convenience entry points ----
135
136 inline void logDebug(const std::string &c, const std::string &f, int l, int col, const std::string &t) { logImpl(LogLevel::Debug, c, f, l, col, t); }
137 inline void logInfo(const std::string &c, const std::string &f, int l, int col, const std::string &t) { logImpl(LogLevel::Info, c, f, l, col, t); }
138 inline void logWarn(const std::string &c, const std::string &f, int l, int col, const std::string &t) { logImpl(LogLevel::Warning, c, f, l, col, t); }
139 inline void logError(const std::string &c, const std::string &f, int l, int col, const std::string &t) { logImpl(LogLevel::Error, c, f, l, col, t); }
141} // namespace ZenitUI
Centralized standard library includes used throughout ZenitUI.
Default ILogger that prints to stderr.
Definition Logger.hpp:57
Abstract sink for LogMessage records.
Definition Logger.hpp:49
Global registry for the active logger.
Definition Logger.hpp:92
static void set(ILogger *logger)
Install logger as the active logger.
Definition Logger.hpp:101
static ILogger & get()
Return the active logger.
Definition Logger.hpp:95
void logImpl(LogLevel level, const std::string &cat, const std::string &file, int line, int col, const std::string &text)
Internal dispatch: build a LogMessage and forward it to the active logger.
Definition Logger.hpp:120
LogLevel
Severity level attached to a LogMessage.
Definition Logger.hpp:29
A single log record passed to ILogger::log.
Definition Logger.hpp:38
std::string category
Subsystem tag, e.g. "ZMarkup".
Definition Logger.hpp:40
std::string file
Source file (may be empty).
Definition Logger.hpp:41
LogLevel level
Severity.
Definition Logger.hpp:39
int line
Source line (0 if unknown).
Definition Logger.hpp:42
std::string text
The message body.
Definition Logger.hpp:44
int column
Source column (0 if unknown).
Definition Logger.hpp:43