Skip to main content

Logger.ixx File

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

Included Headers

#include <string> #include <vector> #include <memory> #include <mutex> #include <iostream> #include <helios.util.log.LogSink>

Namespaces Index

namespacehelios
namespaceutil

Utility functions and helper classes. More...

namespacelog

Logging system with self-registering output sinks. More...

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/**
2 * @file Logger.ixx
3 * @brief Simple synchronous logger with configurable output sinks.
4 */
5module;
6
7#include <string>
8#include <vector>
9#include <memory>
10#include <mutex>
11#include <iostream>
12
13export module helios.util.log.Logger;
14
15import helios.util.log.LogSink;
16
17export namespace helios::util::log {
18
19 /**
20 * @brief Logger implementation with configurable output sinks.
21 *
22 * This logger supports multiple output destinations through LogSink instances.
23 * By default, it writes to stdout, but sinks can be added or replaced to redirect
24 * output to ImGui widgets, files, or other destinations.
25 *
26 * ```cpp
27 * // Add ImGui sink while keeping console output
28 * logger.addSink(imguiSink);
29 *
30 * // Replace all sinks (ImGui only)
31 * logger.clearSinks();
32 * logger.addSink(imguiSink);
33 * ```
34 */
35 class Logger {
36
37 private:
38 const std::string scope_ = "default";
39
40 /**
41 * @brief Flag to indicate whether this Logger's output is enabled.
42 */
43 bool enabled_ = true;
44
45 /**
46 * @brief Collection of output sinks.
47 */
48 std::vector<std::shared_ptr<LogSink>> sinks_;
49
50 /**
51 * @brief Mutex for thread-safe sink access.
52 */
53 mutable std::mutex sinkMutex_;
54
55 /**
56 * @brief Dispatches a message to all registered sinks.
57 *
58 * @param level The log level of the message.
59 * @param msg The message to dispatch.
60 */
61 void dispatch(LogLevel level, const std::string& msg) const noexcept {
62 if (!enabled_) return;
63
64 std::lock_guard<std::mutex> lock(sinkMutex_);
65 if (sinks_.empty()) {
66 // Fallback to stdout if no sinks configured
67 const char* levelStr = "";
68 switch (level) {
69 case LogLevel::Debug: levelStr = "[DEBUG]"; break;
70 case LogLevel::Info: levelStr = "[INFO]"; break;
71 case LogLevel::Warn: levelStr = "[WARN]"; break;
72 case LogLevel::Error: levelStr = "[ERROR]"; break;
73 }
74 std::cout << levelStr << "[" << scope_ << "] " << msg << std::endl;
75 } else {
76 for (const auto& sink : sinks_) {
77 if (sink) {
78 sink->write(level, scope_, msg);
79 }
80 }
81 }
82 }
83
84 public:
85 /**
86 * @brief Creates a new Logger, tagged with a specific scope.
87 *
88 * @param scope The textual scope used as a prefix in log output (e.g. "helios::rendering").
89 */
90 explicit Logger(std::string scope) :
91 scope_(std::move(scope)) {}
92
93 /**
94 * @brief Enables or disables log output for this Logger instance.
95 *
96 * @param enable true to enable output, false to disable it.
97 */
98 void enable(bool enable) noexcept {
99 enabled_ = enable;
100 }
101
102 /**
103 * @brief Adds an output sink to this logger.
104 *
105 * @param sink Shared pointer to the sink to add.
106 */
107 void addSink(std::shared_ptr<LogSink> sink) {
108 std::lock_guard<std::mutex> lock(sinkMutex_);
109 sinks_.push_back(std::move(sink));
110 }
111
112 /**
113 * @brief Removes all sinks from this logger.
114 */
115 void clearSinks() noexcept {
116 std::lock_guard<std::mutex> lock(sinkMutex_);
117 sinks_.clear();
118 }
119
120 /**
121 * @brief Returns the number of attached sinks.
122 *
123 * @return The number of sinks currently attached to this logger.
124 */
125 [[nodiscard]] size_t sinkCount() const noexcept {
126 std::lock_guard<std::mutex> lock(sinkMutex_);
127 return sinks_.size();
128 }
129
130 /**
131 * @brief Writes a warning message if logging is enabled.
132 *
133 * @param msg The message to write.
134 */
135 void warn(const std::string& msg) const noexcept {
136 dispatch(LogLevel::Warn, msg);
137 }
138
139 /**
140 * @brief Writes a debug message if logging is enabled.
141 *
142 * @param msg The message to write.
143 */
144 void debug(const std::string& msg) const noexcept {
145 dispatch(LogLevel::Debug, msg);
146 }
147
148 /**
149 * @brief Writes an info message if logging is enabled.
150 *
151 * @param msg The message to write.
152 */
153 void info(const std::string& msg) const noexcept {
154 dispatch(LogLevel::Info, msg);
155 }
156
157 /**
158 * @brief Writes an error message if logging is enabled.
159 *
160 * @param msg The message to write.
161 */
162 void error(const std::string& msg) const noexcept {
163 dispatch(LogLevel::Error, msg);
164 }
165 };
166
167
168}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.