Skip to main content

Logger.ixx File

Simple synchronous logger with configurable output sinks. More...

Included Headers

#include <format> #include <string> #include <vector> #include <memory> #include <mutex> #include <iostream> #include <cstddef> #include <utility> #include <helios.engine.util.log.LogSink>

Namespaces Index

namespacehelios
namespaceengine
namespaceutil
namespacelog

Classes Index

classLogger

Logger implementation with configurable output sinks. More...

Description

Simple synchronous logger with configurable output sinks.

File Listing

The file content with the documentation metadata removed is:

1
5module;
6
7#include <format>
8#include <string>
9#include <vector>
10#include <memory>
11#include <mutex>
12#include <iostream>
13#include <cstddef>
14#include <utility>
15
16export module helios.engine.util.log.Logger;
17
18import helios.engine.util.log.LogSink;
19
20export namespace helios::engine::util::log {
21
42 class Logger {
43
44 private:
45 const std::string scope_ = "default";
46
50 bool enabled_ = true;
51
55 std::vector<std::shared_ptr<LogSink>> sinks_;
56
60 mutable std::mutex sinkMutex_;
61
68 void dispatch(LogLevel level, const std::string& msg) const noexcept {
69 if (!enabled_) return;
70
71 std::lock_guard<std::mutex> lock(sinkMutex_);
72 if (sinks_.empty()) {
73 // Fallback to stdout if no sinks configured
74 const char* levelStr = "";
75 switch (level) {
76 case LogLevel::Debug: levelStr = "[DEBUG]"; break;
77 case LogLevel::Info: levelStr = "[INFO]"; break;
78 case LogLevel::Warn: levelStr = "[WARN]"; break;
79 case LogLevel::Error: levelStr = "[ERROR]"; break;
80 }
81 std::cout << levelStr << "[" << scope_ << "] " << msg << std::endl;
82 } else {
83 for (const auto& sink : sinks_) {
84 if (sink) {
85 sink->write(level, scope_, msg);
86 }
87 }
88 }
89 }
90
99 template<typename... TArgs>
100 void dispatchFormatted(LogLevel level, std::format_string<TArgs...> fmt, TArgs&&... args) const {
101 dispatch(level, std::format(fmt, std::forward<TArgs>(args)...));
102 }
103
104 public:
110 explicit Logger(std::string scope) :
111 scope_(std::move(scope)) {}
112
118 void enable(bool enable) noexcept {
119 enabled_ = enable;
120 }
121
127 void addSink(std::shared_ptr<LogSink> sink) {
128 std::lock_guard<std::mutex> lock(sinkMutex_);
129 sinks_.push_back(std::move(sink));
130 }
131
136 std::lock_guard<std::mutex> lock(sinkMutex_);
137 sinks_.clear();
138 }
139
145 [[nodiscard]] std::size_t sinkCount() const noexcept {
146 std::lock_guard<std::mutex> lock(sinkMutex_);
147 return sinks_.size();
148 }
149
155 void warn(const std::string& msg) const noexcept {
156 dispatch(LogLevel::Warn, msg);
157 }
158
166 template<typename... TArgs>
167 void warn(std::format_string<TArgs...> fmt, TArgs&&... args) const {
168 dispatchFormatted(LogLevel::Warn, fmt, std::forward<TArgs>(args)...);
169 }
170
176 void debug(const std::string& msg) const noexcept {
177 dispatch(LogLevel::Debug, msg);
178 }
179
187 template<typename... TArgs>
188 void debug(std::format_string<TArgs...> fmt, TArgs&&... args) const {
189 dispatchFormatted(LogLevel::Debug, fmt, std::forward<TArgs>(args)...);
190 }
191
197 void info(const std::string& msg) const noexcept {
198 dispatch(LogLevel::Info, msg);
199 }
200
208 template<typename... TArgs>
209 void info(std::format_string<TArgs...> fmt, TArgs&&... args) const {
210 dispatchFormatted(LogLevel::Info, fmt, std::forward<TArgs>(args)...);
211 }
212
218 void error(const std::string& msg) const noexcept {
219 dispatch(LogLevel::Error, msg);
220 }
221
229 template<typename... TArgs>
230 void error(std::format_string<TArgs...> fmt, TArgs&&... args) const {
231 dispatchFormatted(LogLevel::Error, fmt, std::forward<TArgs>(args)...);
232 }
233 };
234
235
236}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.