morph 0.1.0
Asynchronous bridge between GUI thread and business models
Loading...
Searching...
No Matches
morph::log::ScopedLoggerOverride Class Reference

RAII helper that swaps the global logger and level for the lifetime of the object, and restores them in the destructor. More...

#include <logger.hpp>

Public Member Functions

 ScopedLoggerOverride ()
 Snapshots the current sink + level so they can be restored.
 ScopedLoggerOverride (std::function< void(LogLevel, std::string_view)> sink, LogLevel level=LogLevel::debug)
 Installs sink and level; saves whatever was there before.
 ~ScopedLoggerOverride ()
 Restores the saved sink and level.
 ScopedLoggerOverride (const ScopedLoggerOverride &)=delete
ScopedLoggerOverride & operator= (const ScopedLoggerOverride &)=delete
 ScopedLoggerOverride (ScopedLoggerOverride &&)=delete
ScopedLoggerOverride & operator= (ScopedLoggerOverride &&)=delete

Detailed Description

RAII helper that swaps the global logger and level for the lifetime of the object, and restores them in the destructor.

Designed for tests that want to capture log output without leaking the custom sink into other tests. Thread-safe construction and destruction — the global mutex is acquired briefly during each.

{
std::vector<std::string> captured;
[&](morph::log::LogLevel, std::string_view msg) { captured.emplace_back(msg); },
morph::log::LogLevel::debug,
};
// ... run code that logs ...
} // previous sink + level restored here
RAII helper that swaps the global logger and level for the lifetime of the object,...
Definition logger.hpp:279

Definition at line 279 of file logger.hpp.

Constructor & Destructor Documentation

◆ ScopedLoggerOverride() [1/2]

morph::log::ScopedLoggerOverride::ScopedLoggerOverride ( )
inline

Snapshots the current sink + level so they can be restored.

Use this when test code will install its own sink mid-test via setLogger() / setLogLevel() and just wants automatic restoration.

Definition at line 285 of file logger.hpp.

◆ ScopedLoggerOverride() [2/2]

morph::log::ScopedLoggerOverride::ScopedLoggerOverride ( std::function< void(LogLevel, std::string_view)> sink,
LogLevel level = LogLevel::debug )
inlineexplicit

Installs sink and level; saves whatever was there before.

Parameters
sinkNew logger sink. Pass a no-op lambda to suppress output entirely.
levelNew minimum level. Defaults to debug so every message reaches sink.

Definition at line 304 of file logger.hpp.

◆ ~ScopedLoggerOverride()

morph::log::ScopedLoggerOverride::~ScopedLoggerOverride ( )
inline

Restores the saved sink and level.

Definition at line 318 of file logger.hpp.


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