Corium 1.1.0
High-Performance Zero-Heap C++20 MPSC Application Runtime
Loading...
Searching...
No Matches
Logger.hpp
Go to the documentation of this file.
1
7#pragma once
8
9#include <cstdio>
10#include <utility>
11
16
17namespace corium::logging {
18
23template <typename LogSink = sinks::ConsoleLogSink, std::size_t MaxMessageSize = 256>
24class LoggerT {
25public:
27
28 explicit LoggerT(const char* category = "App", LogLevel minLevel = LogLevel::Info, LogSink sink = LogSink{})
29 : _category(category ? category : "App"), _minLevel(minLevel), _sink(std::move(sink))
30 {}
31
33 void setMinLevel(LogLevel level) noexcept
34 {
35 _minLevel = level;
36 }
37
39 [[nodiscard]] LogLevel minLevel() const noexcept
40 {
41 return _minLevel;
42 }
43
45 void setCategory(const char* category) noexcept
46 {
47 _category = category ? category : "App";
48 }
49
51 [[nodiscard]] const char* category() const noexcept
52 {
53 return _category;
54 }
55
57 [[nodiscard]] LogSink& sink() noexcept { return _sink; }
58 [[nodiscard]] const LogSink& sink() const noexcept { return _sink; }
59
61 template <typename... Args>
62 void log(LogLevel level, const char* fmt, Args&&... args) const
63 {
64 if (level < _minLevel || _minLevel == LogLevel::Off) {
65 return;
66 }
67
68 EventType event{};
69 event.level = level;
70 event.category = _category;
71
72 if constexpr (sizeof...(Args) == 0) {
73 event.setMessage(fmt);
74 } else {
75 int written = std::snprintf(event.message.data(), MaxMessageSize, fmt, std::forward<Args>(args)...);
76 if (written > 0) {
77 event.length = static_cast<std::size_t>(written) < MaxMessageSize ? static_cast<std::size_t>(written) : (MaxMessageSize - 1);
78 }
79 }
80
81 _sink.write(event);
82 }
83
85 template <typename TargetEventSink, typename... Args>
86 void logToSink(TargetEventSink&& targetSink, LogLevel level, const char* fmt, Args&&... args) const
87 {
88 if (level < _minLevel || _minLevel == LogLevel::Off) {
89 return;
90 }
91
92 EventType event{};
93 event.level = level;
94 event.category = _category;
95
96 if constexpr (sizeof...(Args) == 0) {
97 event.setMessage(fmt);
98 } else {
99 int written = std::snprintf(event.message.data(), MaxMessageSize, fmt, std::forward<Args>(args)...);
100 if (written > 0) {
101 event.length = static_cast<std::size_t>(written) < MaxMessageSize ? static_cast<std::size_t>(written) : (MaxMessageSize - 1);
102 }
103 }
104
105 targetSink.post(std::move(event));
106 }
107
108 template <typename... Args>
109 void trace(const char* fmt, Args&&... args) const { log(LogLevel::Trace, fmt, std::forward<Args>(args)...); }
110
111 template <typename... Args>
112 void debug(const char* fmt, Args&&... args) const { log(LogLevel::Debug, fmt, std::forward<Args>(args)...); }
113
114 template <typename... Args>
115 void info(const char* fmt, Args&&... args) const { log(LogLevel::Info, fmt, std::forward<Args>(args)...); }
116
117 template <typename... Args>
118 void warn(const char* fmt, Args&&... args) const { log(LogLevel::Warn, fmt, std::forward<Args>(args)...); }
119
120 template <typename... Args>
121 void error(const char* fmt, Args&&... args) const { log(LogLevel::Error, fmt, std::forward<Args>(args)...); }
122
123 template <typename... Args>
124 void critical(const char* fmt, Args&&... args) const { log(LogLevel::Critical, fmt, std::forward<Args>(args)...); }
125
126private:
127 const char* _category = "App";
128 LogLevel _minLevel = LogLevel::Info;
129 LogSink _sink{};
130};
131
134
137
140
141} // namespace corium::logging
Standard console output log sink with ANSI color support.
Synchronous append-only file logging sink.
Zero-heap fixed-capacity inline buffer log event record.
No-op compile-time disabled log sink for zero overhead.
Static compile-time logger wrapper managing severity level filtering and zero-heap string formatting.
Definition Logger.hpp:24
LogSink & sink() noexcept
Access underlying log sink reference.
Definition Logger.hpp:57
void debug(const char *fmt, Args &&... args) const
Definition Logger.hpp:112
void error(const char *fmt, Args &&... args) const
Definition Logger.hpp:121
const char * category() const noexcept
Get category tag.
Definition Logger.hpp:51
void trace(const char *fmt, Args &&... args) const
Definition Logger.hpp:109
void warn(const char *fmt, Args &&... args) const
Definition Logger.hpp:118
void critical(const char *fmt, Args &&... args) const
Definition Logger.hpp:124
LogLevel minLevel() const noexcept
Get minimum log severity level.
Definition Logger.hpp:39
void setMinLevel(LogLevel level) noexcept
Set minimum log severity level.
Definition Logger.hpp:33
void setCategory(const char *category) noexcept
Set category string tag.
Definition Logger.hpp:45
void logToSink(TargetEventSink &&targetSink, LogLevel level, const char *fmt, Args &&... args) const
Post formatted LogEvent to a target Corium EventSink (lock-free MPSC event bus).
Definition Logger.hpp:86
LoggerT(const char *category="App", LogLevel minLevel=LogLevel::Info, LogSink sink=LogSink{})
Definition Logger.hpp:28
void log(LogLevel level, const char *fmt, Args &&... args) const
Log formatted message directly to sink if severity level meets minimum threshold.
Definition Logger.hpp:62
const LogSink & sink() const noexcept
Definition Logger.hpp:58
void info(const char *fmt, Args &&... args) const
Definition Logger.hpp:115
Definition LogBackgroundService.hpp:17
LogLevel
Logging severity levels.
Definition LogLevel.hpp:14
Zero-heap log event carrying fixed-size inline message buffer across MPSC event bus.
Definition LogEvent.hpp:21
LogLevel level
Definition LogEvent.hpp:22