Skip to main content

LogManager Class

LogManager for managing scoped Loggers and global sink configuration. More...

Declaration

class helios::util::log::LogManager { ... }

Public Constructors Index

LogManager (const LogManager &)=delete

Private Constructors Index

LogManager ()

Creates the LogManager and registers an unscoped default logger. More...

Public Destructor Index

~LogManager ()=default

Public Operators Index

LogManager &operator= (const LogManager &)=delete

Public Member Functions Index

const Logger &logger () const noexcept

Returns a const reference to the default logger managed with this LogManager. More...

const Logger &logger (const std::string &scope) const noexcept

Returns a const reference to the logger instance for the specified scope. More...

Logger &registerLogger (const std::string &scope) noexcept

Registers a new logger with this manager. More...

voidenableLogging (bool enable) noexcept

Enables or disables all log output of the Loggers registered with this LogManager. More...

voidsetScopeFilter (const std::string &scope) noexcept

Sets the filter scope for the logger. More...

voidregisterSink (std::shared_ptr< LogSink > sink)

Registers a sink and enables it by default. More...

voidregisterSink (std::shared_ptr< LogSink > sink, bool enabled)

Registers a sink with optional auto-enable. More...

voidenableSink (SinkTypeId typeId)

Enables a sink by its type identifier. More...

voidenableSink (std::shared_ptr< LogSink > sink)

Enables a sink, registering it first if necessary. More...

voiddisableSink (SinkTypeId typeId)

Disables a sink by its type identifier. More...

voiddisableSink (std::shared_ptr< LogSink > sink)

Disables a sink by instance. More...

boolisSinkEnabled (SinkTypeId typeId) const noexcept

Checks if a sink with the given type identifier is currently enabled. More...

voidenableAllSinks ()

Enables all registered sinks. More...

voiddisableAllSinks ()

Disables all sinks. More...

Private Member Functions Index

voidupdateLoggerSinks ()

Reconfigures all logger sinks based on currently enabled sinks. More...

Private Member Attributes Index

boolloggingEnabled_ = LOGGING_ENABLED

Flag indicating whether log output should be globally enabled or disabled. More...

std::unordered_set< std::string >enabledSinks_

Set of currently enabled sink type identifiers. More...

std::vector< std::shared_ptr< LogSink > >registeredSinks_

Registered sinks (all available sinks, regardless of enabled state). More...

std::unordered_map< std::string, std::unique_ptr< Logger > >loggers_

Unordered map holding unique pointers to the loggers managed by this class, guaranteed to be not null. More...

const std::unique_ptr< Logger >defaultLogger_

Default logger if a logger for a specific scope was not found. More...

std::mutexmapMutex_

Mutex providing mutually exclusive access to the loggers map. More...

std::mutexsinkMutex_

Mutex for sink access. More...

Public Static Functions Index

static const Logger &loggerForScope (const std::string &scope) noexcept

Convenience accessor to obtain a logger for a textual scope via the singleton. More...

static LogManager &getInstance () noexcept

Returns the LogManager singleton instance. More...

Description

LogManager for managing scoped Loggers and global sink configuration.

The LogManager provides centralized control over logging output destinations. Sinks register themselves with a unique type identifier, allowing dynamic enable/disable without compile-time knowledge of all sink types.

```cpp // Register sinks (sinks define their own TYPE_ID) LogManager::getInstance().registerSink(std::make_shared<ConsoleSink>()); LogManager::getInstance().registerSink(std::make_shared<ImGuiLogSink>(widget));

// Enable/disable by type identifier LogManager::getInstance().enableSink("console"); LogManager::getInstance().disableSink("imgui");

// Check status if (LogManager::getInstance().isSinkEnabled("console")) { ... } ```

Definition at line 43 of file LogManager.ixx.

Public Constructors

LogManager()

helios::util::log::LogManager::LogManager (const LogManager &)
delete

Enforce singleton (see Meyer's Singleton)

See Also

https://en.wikipedia.org/wiki/Singleton_pattern

Definition at line 139 of file LogManager.ixx.

Private Constructors

LogManager()

helios::util::log::LogManager::LogManager ()
inline

Creates the LogManager and registers an unscoped default logger.

Definition at line 86 of file LogManager.ixx.

86 LogManager() : defaultLogger_(std::make_unique<Logger>("default")) {}

Public Destructor

~LogManager()

helios::util::log::LogManager::~LogManager ()
default

Definition at line 132 of file LogManager.ixx.

Public Operators

operator=()

LogManager & helios::util::log::LogManager::operator= (const LogManager &)
delete

Definition at line 140 of file LogManager.ixx.

Public Member Functions

disableAllSinks()

void helios::util::log::LogManager::disableAllSinks ()
inline

Disables all sinks.

Definition at line 405 of file LogManager.ixx.

406 std::lock_guard<std::mutex> lock(sinkMutex_);
407 enabledSinks_.clear();
408 updateLoggerSinks();
409 }

disableSink()

void helios::util::log::LogManager::disableSink (SinkTypeId typeId)
inline

Disables a sink by its type identifier.

Parameters
typeId

The unique type identifier of the sink.

Definition at line 361 of file LogManager.ixx.

361 void disableSink(SinkTypeId typeId) {
362 std::lock_guard<std::mutex> lock(sinkMutex_);
363 enabledSinks_.erase(typeId);
364 updateLoggerSinks();
365 }

Referenced by disableSink.

disableSink()

void helios::util::log::LogManager::disableSink (std::shared_ptr< LogSink > sink)
inline

Disables a sink by instance.

Parameters
sink

The sink instance to disable.

Definition at line 372 of file LogManager.ixx.

372 void disableSink(std::shared_ptr<LogSink> sink) {
373 if (!sink) return;
374 disableSink(sink->typeId());
375 }

Reference disableSink.

enableAllSinks()

void helios::util::log::LogManager::enableAllSinks ()
inline

Enables all registered sinks.

Definition at line 392 of file LogManager.ixx.

393 std::lock_guard<std::mutex> lock(sinkMutex_);
394 for (const auto& sink : registeredSinks_) {
395 if (sink) {
396 enabledSinks_.insert(sink->typeId());
397 }
398 }
399 updateLoggerSinks();
400 }

enableLogging()

void helios::util::log::LogManager::enableLogging (bool enable)
inline noexcept

Enables or disables all log output of the Loggers registered with this LogManager.

Parameters
enable

True to enable log output with the registered loggers, otherwise false.

Definition at line 225 of file LogManager.ixx.

225 void enableLogging(bool enable) noexcept {
226 std::lock_guard<std::mutex> lock(mapMutex_);
227
228 if (loggingEnabled_ == enable) {
229 return;
230 }
231
232 loggingEnabled_ = enable;
233
234 for (auto& [fst, snd]: loggers_) {
235 snd->enable(enable);
236 }
237 }

enableSink()

void helios::util::log::LogManager::enableSink (SinkTypeId typeId)
inline

Enables a sink by its type identifier.

If a sink with the given typeId is registered, it will be enabled. If no sink with that typeId is registered, this call has no effect.

Parameters
typeId

The unique type identifier of the sink (e.g., "console", "imgui").

Definition at line 319 of file LogManager.ixx.

319 void enableSink(SinkTypeId typeId) {
320 std::lock_guard<std::mutex> lock(sinkMutex_);
321 enabledSinks_.insert(typeId);
322 updateLoggerSinks();
323 }

enableSink()

void helios::util::log::LogManager::enableSink (std::shared_ptr< LogSink > sink)
inline

Enables a sink, registering it first if necessary.

If the sink is not yet registered, it will be added to the pool. The sink is then enabled for output.

Parameters
sink

The sink instance to enable (and register if needed).

Definition at line 333 of file LogManager.ixx.

333 void enableSink(std::shared_ptr<LogSink> sink) {
334 if (!sink) return;
335
336 std::lock_guard<std::mutex> lock(sinkMutex_);
337
338 // Check if already registered
339 bool alreadyRegistered = false;
340 for (const auto& existing : registeredSinks_) {
341 if (existing && std::strcmp(existing->typeId(), sink->typeId()) == 0) {
342 alreadyRegistered = true;
343 break;
344 }
345 }
346
347 // Auto-register if not already registered
348 if (!alreadyRegistered) {
349 registeredSinks_.push_back(sink);
350 }
351
352 enabledSinks_.insert(sink->typeId());
353 updateLoggerSinks();
354 }

isSinkEnabled()

bool helios::util::log::LogManager::isSinkEnabled (SinkTypeId typeId)
inline nodiscard noexcept

Checks if a sink with the given type identifier is currently enabled.

Parameters
typeId

The unique type identifier of the sink.

Returns

True if the sink is enabled.

Definition at line 384 of file LogManager.ixx.

384 [[nodiscard]] bool isSinkEnabled(SinkTypeId typeId) const noexcept {
385 std::lock_guard<std::mutex> lock(sinkMutex_);
386 return enabledSinks_.contains(typeId);
387 }

logger()

const Logger & helios::util::log::LogManager::logger ()
inline nodiscard noexcept

Returns a const reference to the default logger managed with this LogManager.

Returns

The default Logger instance.

Definition at line 158 of file LogManager.ixx.

158 [[nodiscard]] const Logger& logger() const noexcept {
159 return *defaultLogger_;
160 }

Referenced by registerLogger.

logger()

const Logger & helios::util::log::LogManager::logger (const std::string & scope)
inline nodiscard noexcept

Returns a const reference to the logger instance for the specified scope.

Will fall back to the default logger if the scope was not registered yet. This method is thread safe for map look-ups.

Parameters
scope

The textual scope name of the requested logger.

Returns

The logger registered with the scope, or the default logger if none was found.

Definition at line 173 of file LogManager.ixx.

173 [[nodiscard]] const Logger& logger(const std::string& scope) const noexcept {
174 // mapMutex_ is automatically released when going out of scope
175 std::lock_guard<std::mutex> lock(mapMutex_);
176
177 auto log = loggers_.find(scope);
178 if (log != loggers_.end()) {
179 return *(log->second);
180 }
181
182 return *defaultLogger_;
183 }

registerLogger()

Logger & helios::util::log::LogManager::registerLogger (const std::string & scope)
inline nodiscard noexcept

Registers a new logger with this manager.

This method is thread safe for map modifications.

Parameters
scope

The scope requested for the logger to create.

Returns

The logger registered with the scope, or the logger already registered with the LogManager under the given scope.

Definition at line 195 of file LogManager.ixx.

195 [[nodiscard]] Logger& registerLogger(const std::string& scope) noexcept {
196 // mapMutex_ is automatically released when going out of scope
197 std::lock_guard<std::mutex> lock(mapMutex_);
198
199 if (auto log = loggers_.find(scope); log != loggers_.end()) {
200 return *(log->second);
201 }
202
203 auto logger = std::make_unique<Logger>(scope);
204 loggers_[scope] = std::move(logger);
205 loggers_[scope]->enable(loggingEnabled_);
206
207 // Configure sinks for new logger
208 {
209 std::lock_guard<std::mutex> sinkLock(sinkMutex_);
210 for (const auto& sink : registeredSinks_) {
211 if (sink && enabledSinks_.contains(sink->typeId())) {
212 loggers_[scope]->addSink(sink);
213 }
214 }
215 }
216
217 return *loggers_[scope];
218 }

Reference logger.

Referenced by loggerForScope and setScopeFilter.

registerSink()

void helios::util::log::LogManager::registerSink (std::shared_ptr< LogSink > sink)
inline

Registers a sink and enables it by default.

The sink is added to the pool and immediately enabled for output.

Parameters
sink

The sink to register.

Definition at line 276 of file LogManager.ixx.

276 void registerSink(std::shared_ptr<LogSink> sink) {
277 registerSink(std::move(sink), true);
278 }

Reference registerSink.

Referenced by registerSink.

registerSink()

void helios::util::log::LogManager::registerSink (std::shared_ptr< LogSink > sink, bool enabled)
inline

Registers a sink with optional auto-enable.

Parameters
sink

The sink to register.

enabled

Whether to enable the sink immediately (default: true).

Definition at line 286 of file LogManager.ixx.

286 void registerSink(std::shared_ptr<LogSink> sink, bool enabled) {
287 if (!sink) return;
288
289 std::lock_guard<std::mutex> lock(sinkMutex_);
290
291 // Check if sink with same typeId is already registered
292 bool alreadyRegistered = false;
293 for (const auto& existing : registeredSinks_) {
294 if (existing && std::strcmp(existing->typeId(), sink->typeId()) == 0) {
295 alreadyRegistered = true;
296 break;
297 }
298 }
299
300 if (!alreadyRegistered) {
301 registeredSinks_.push_back(sink);
302 }
303
304 if (enabled) {
305 enabledSinks_.insert(sink->typeId());
306 }
307
308 updateLoggerSinks();
309 }

setScopeFilter()

void helios::util::log::LogManager::setScopeFilter (const std::string & scope)
inline noexcept

Sets the filter scope for the logger.

Will do nothing if logging is not enabled. If the logger for the scope does not exist, it will get implicitly created.

Parameters
scope

The scope to filter. Only log messages with this scope will be logged.

Definition at line 247 of file LogManager.ixx.

247 void setScopeFilter(const std::string& scope) noexcept {
248 if (!loggingEnabled_) {
249 return;
250 }
251
252 // Make sure the logger exists first (this acquires mapMutex_ internally)
253 std::ignore = LogManager::getInstance().registerLogger(scope);
254
255 // Now lock and update the filter
256 std::lock_guard<std::mutex> lock(mapMutex_);
257
258 for (auto& [fst, snd] : loggers_) {
259 if (fst == scope) {
260 snd->enable(true);
261 } else {
262 snd->enable(false);
263 }
264 }
265 }

References getInstance and registerLogger.

Private Member Functions

updateLoggerSinks()

void helios::util::log::LogManager::updateLoggerSinks ()
inline

Reconfigures all logger sinks based on currently enabled sinks.

Called internally after sink enable/disable changes.

Definition at line 93 of file LogManager.ixx.

93 void updateLoggerSinks() {
94 // Called with sinkMutex_ already held
95 std::lock_guard<std::mutex> mapLock(mapMutex_);
96
97 // Update default logger
98 defaultLogger_->clearSinks();
99 for (const auto& sink : registeredSinks_) {
100 if (sink && enabledSinks_.contains(sink->typeId())) {
101 defaultLogger_->addSink(sink);
102 }
103 }
104
105 // Update all registered loggers
106 for (auto& [scope, logger] : loggers_) {
107 logger->clearSinks();
108 for (const auto& sink : registeredSinks_) {
109 if (sink && enabledSinks_.contains(sink->typeId())) {
110 logger->addSink(sink);
111 }
112 }
113 }
114 }

Private Member Attributes

defaultLogger_

const std::unique_ptr<Logger> helios::util::log::LogManager::defaultLogger_

Default logger if a logger for a specific scope was not found.

Definition at line 71 of file LogManager.ixx.

71 const std::unique_ptr<Logger> defaultLogger_;

enabledSinks_

std::unordered_set<std::string> helios::util::log::LogManager::enabledSinks_

Set of currently enabled sink type identifiers.

Definition at line 55 of file LogManager.ixx.

55 std::unordered_set<std::string> enabledSinks_;

loggers_

std::unordered_map<std::string, std::unique_ptr<Logger> > helios::util::log::LogManager::loggers_

Unordered map holding unique pointers to the loggers managed by this class, guaranteed to be not null.

Definition at line 66 of file LogManager.ixx.

66 std::unordered_map<std::string, std::unique_ptr<Logger>> loggers_;

loggingEnabled_

bool helios::util::log::LogManager::loggingEnabled_ = LOGGING_ENABLED

Flag indicating whether log output should be globally enabled or disabled.

Definition at line 50 of file LogManager.ixx.

50 bool loggingEnabled_ = LOGGING_ENABLED;

mapMutex_

std::mutex helios::util::log::LogManager::mapMutex_
mutable

Mutex providing mutually exclusive access to the loggers map.

Definition at line 76 of file LogManager.ixx.

76 mutable std::mutex mapMutex_;

registeredSinks_

std::vector<std::shared_ptr<LogSink> > helios::util::log::LogManager::registeredSinks_

Registered sinks (all available sinks, regardless of enabled state).

Definition at line 60 of file LogManager.ixx.

60 std::vector<std::shared_ptr<LogSink>> registeredSinks_;

sinkMutex_

std::mutex helios::util::log::LogManager::sinkMutex_
mutable

Mutex for sink access.

Definition at line 81 of file LogManager.ixx.

81 mutable std::mutex sinkMutex_;

Public Static Functions

getInstance()

LogManager & helios::util::log::LogManager::getInstance ()
inline noexcept static

Returns the LogManager singleton instance.

Returns

Reference to the global LogManager instance.

Definition at line 147 of file LogManager.ixx.

147 static LogManager& getInstance() noexcept {
148 static LogManager instance;
149
150 return instance;
151 }

Referenced by loggerForScope and setScopeFilter.

loggerForScope()

const Logger & helios::util::log::LogManager::loggerForScope (const std::string & scope)
inline noexcept static

Convenience accessor to obtain a logger for a textual scope via the singleton.

This static helper forwards to `LogManager::getInstance().logger(scope)` and returns a reference to the logger registered for the given scope.

Parameters
scope

The textual scope name of the requested logger.

Returns

The logger registered with the scope, or the default logger if none was found.

Definition at line 128 of file LogManager.ixx.

128 static const Logger& loggerForScope(const std::string& scope) noexcept {
130 }

References getInstance and registerLogger.


The documentation for this class was generated from the following file:


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.