Skip to main content

Logger Class

Logger implementation with configurable output sinks. More...

Declaration

class helios::engine::util::log::Logger { ... }

Public Constructors Index

Logger (std::string scope)

Creates a new Logger, tagged with a specific scope. More...

Public Member Functions Index

voidenable (bool enable) noexcept

Enables or disables log output for this Logger instance. More...

voidaddSink (std::shared_ptr< LogSink > sink)

Adds an output sink to this logger. More...

voidclearSinks () noexcept

Removes all sinks from this logger. More...

std::size_tsinkCount () const noexcept

Returns the number of attached sinks. More...

voidwarn (const std::string &msg) const noexcept

Writes a warning message if logging is enabled. More...

template <typename... TArgs>
voidwarn (std::format_string< TArgs... > fmt, TArgs &&... args) const

Writes a formatted warning message if logging is enabled. More...

voiddebug (const std::string &msg) const noexcept

Writes a debug message if logging is enabled. More...

template <typename... TArgs>
voiddebug (std::format_string< TArgs... > fmt, TArgs &&... args) const

Writes a formatted debug message if logging is enabled. More...

voidinfo (const std::string &msg) const noexcept

Writes an info message if logging is enabled. More...

template <typename... TArgs>
voidinfo (std::format_string< TArgs... > fmt, TArgs &&... args) const

Writes a formatted info message if logging is enabled. More...

voiderror (const std::string &msg) const noexcept

Writes an error message if logging is enabled. More...

template <typename... TArgs>
voiderror (std::format_string< TArgs... > fmt, TArgs &&... args) const

Writes a formatted error message if logging is enabled. More...

Private Member Functions Index

voiddispatch (LogLevel level, const std::string &msg) const noexcept

Dispatches a message to all registered sinks. More...

template <typename... TArgs>
voiddispatchFormatted (LogLevel level, std::format_string< TArgs... > fmt, TArgs &&... args) const

Formats a message and dispatches it to all sinks. More...

Private Member Attributes Index

const std::stringscope_ = "default"
boolenabled_ = true

Flag to indicate whether this Logger's output is enabled. More...

std::vector< std::shared_ptr< LogSink > >sinks_

Collection of output sinks. More...

std::mutexsinkMutex_

Mutex for thread-safe sink access. More...

Description

Logger implementation with configurable output sinks.

This logger supports multiple output destinations through LogSink instances. By default, it writes to stdout, but sinks can be added or replaced to redirect output to ImGui widgets, files, or other destinations. In addition to plain string messages, the logger also supports std::format-based message creation.

 // Add ImGui sink while keeping console output
 logger.addSink(imguiSink);
 
 // Replace all sinks (ImGui only)
 logger.clearSinks();
 logger.addSink(imguiSink);
 
 // Format a message lazily at the call site
 logger.error("Missing resource {0} for entity {1}", resourceId, entityId);

Definition at line 42 of file Logger.ixx.

Public Constructors

Logger()

helios::engine::util::log::Logger::Logger (std::string scope)
inline explicit

Creates a new Logger, tagged with a specific scope.

Parameters
scope

The textual scope used as a prefix in log output (e.g. "helios::engine::rendering").

Definition at line 110 of file Logger.ixx.

110 explicit Logger(std::string scope) :
111 scope_(std::move(scope)) {}

Public Member Functions

addSink()

void helios::engine::util::log::Logger::addSink (std::shared_ptr< LogSink > sink)
inline

Adds an output sink to this logger.

Parameters
sink

Shared pointer to the sink to add.

Definition at line 127 of file Logger.ixx.

127 void addSink(std::shared_ptr<LogSink> sink) {
128 std::lock_guard<std::mutex> lock(sinkMutex_);
129 sinks_.push_back(std::move(sink));
130 }

Reference helios::registerComponents.

clearSinks()

void helios::engine::util::log::Logger::clearSinks ()
inline noexcept

Removes all sinks from this logger.

Definition at line 135 of file Logger.ixx.

136 std::lock_guard<std::mutex> lock(sinkMutex_);
137 sinks_.clear();
138 }

debug()

void helios::engine::util::log::Logger::debug (const std::string & msg)
inline noexcept

Writes a debug message if logging is enabled.

Parameters
msg

The message to write.

Definition at line 176 of file Logger.ixx.

176 void debug(const std::string& msg) const noexcept {
177 dispatch(LogLevel::Debug, msg);
178 }

References helios::engine::util::log::Debug and helios::registerComponents.

debug()

template <typename... TArgs>
void helios::engine::util::log::Logger::debug (std::format_string< TArgs... > fmt, TArgs &&... args)
inline

Writes a formatted debug message if logging is enabled.

Template Parameters
TArgs

Format argument types.

Parameters
fmt

Checked format string.

args

Format arguments.

Definition at line 188 of file Logger.ixx.

188 void debug(std::format_string<TArgs...> fmt, TArgs&&... args) const {
189 dispatchFormatted(LogLevel::Debug, fmt, std::forward<TArgs>(args)...);
190 }

References helios::engine::util::log::Debug and helios::registerComponents.

enable()

void helios::engine::util::log::Logger::enable (bool enable)
inline noexcept

Enables or disables log output for this Logger instance.

Parameters
enable

true to enable output, false to disable it.

Definition at line 118 of file Logger.ixx.

118 void enable(bool enable) noexcept {
119 enabled_ = enable;
120 }

Reference enable.

Referenced by enable.

error()

void helios::engine::util::log::Logger::error (const std::string & msg)
inline noexcept

Writes an error message if logging is enabled.

Parameters
msg

The message to write.

Definition at line 218 of file Logger.ixx.

218 void error(const std::string& msg) const noexcept {
219 dispatch(LogLevel::Error, msg);
220 }

References helios::engine::util::log::Error and helios::registerComponents.

Referenced by helios::engine::util::io::BasicStringFileReader::getContents and helios::engine::util::io::BasicStringFileReader::readInto.

error()

template <typename... TArgs>
void helios::engine::util::log::Logger::error (std::format_string< TArgs... > fmt, TArgs &&... args)
inline

Writes a formatted error message if logging is enabled.

Template Parameters
TArgs

Format argument types.

Parameters
fmt

Checked format string.

args

Format arguments.

Definition at line 230 of file Logger.ixx.

230 void error(std::format_string<TArgs...> fmt, TArgs&&... args) const {
231 dispatchFormatted(LogLevel::Error, fmt, std::forward<TArgs>(args)...);
232 }

References helios::engine::util::log::Error and helios::registerComponents.

info()

void helios::engine::util::log::Logger::info (const std::string & msg)
inline noexcept

Writes an info message if logging is enabled.

Parameters
msg

The message to write.

Definition at line 197 of file Logger.ixx.

197 void info(const std::string& msg) const noexcept {
198 dispatch(LogLevel::Info, msg);
199 }

References helios::engine::util::log::Info and helios::registerComponents.

info()

template <typename... TArgs>
void helios::engine::util::log::Logger::info (std::format_string< TArgs... > fmt, TArgs &&... args)
inline

Writes a formatted info message if logging is enabled.

Template Parameters
TArgs

Format argument types.

Parameters
fmt

Checked format string.

args

Format arguments.

Definition at line 209 of file Logger.ixx.

209 void info(std::format_string<TArgs...> fmt, TArgs&&... args) const {
210 dispatchFormatted(LogLevel::Info, fmt, std::forward<TArgs>(args)...);
211 }

References helios::engine::util::log::Info and helios::registerComponents.

sinkCount()

std::size_t helios::engine::util::log::Logger::sinkCount ()
inline noexcept

Returns the number of attached sinks.

Returns

The number of sinks currently attached to this logger.

Definition at line 145 of file Logger.ixx.

145 [[nodiscard]] std::size_t sinkCount() const noexcept {
146 std::lock_guard<std::mutex> lock(sinkMutex_);
147 return sinks_.size();
148 }

warn()

void helios::engine::util::log::Logger::warn (const std::string & msg)
inline noexcept

Writes a warning message if logging is enabled.

Parameters
msg

The message to write.

Definition at line 155 of file Logger.ixx.

155 void warn(const std::string& msg) const noexcept {
156 dispatch(LogLevel::Warn, msg);
157 }

References helios::registerComponents and helios::engine::util::log::Warn.

Referenced by helios::engine::input::InputManager::registerGamepads and helios::engine::input::gamepad::GamepadState::updateAxes.

warn()

template <typename... TArgs>
void helios::engine::util::log::Logger::warn (std::format_string< TArgs... > fmt, TArgs &&... args)
inline

Writes a formatted warning message if logging is enabled.

Template Parameters
TArgs

Format argument types.

Parameters
fmt

Checked format string.

args

Format arguments.

Definition at line 167 of file Logger.ixx.

167 void warn(std::format_string<TArgs...> fmt, TArgs&&... args) const {
168 dispatchFormatted(LogLevel::Warn, fmt, std::forward<TArgs>(args)...);
169 }

References helios::registerComponents and helios::engine::util::log::Warn.

Private Member Functions

dispatch()

void helios::engine::util::log::Logger::dispatch (LogLevel level, const std::string & msg)
inline noexcept

Dispatches a message to all registered sinks.

Parameters
level

The log level of the message.

msg

The message to dispatch.

Definition at line 68 of file Logger.ixx.

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 }

dispatchFormatted()

template <typename... TArgs>
void helios::engine::util::log::Logger::dispatchFormatted (LogLevel level, std::format_string< TArgs... > fmt, TArgs &&... args)
inline

Formats a message and dispatches it to all sinks.

Template Parameters
TArgs

Format argument types.

Parameters
level

Log level of the message.

fmt

Checked format string.

args

Format arguments.

Definition at line 100 of file Logger.ixx.

100 void dispatchFormatted(LogLevel level, std::format_string<TArgs...> fmt, TArgs&&... args) const {
101 dispatch(level, std::format(fmt, std::forward<TArgs>(args)...));
102 }

Private Member Attributes

enabled_

bool helios::engine::util::log::Logger::enabled_ = true

Flag to indicate whether this Logger's output is enabled.

Definition at line 50 of file Logger.ixx.

50 bool enabled_ = true;

scope_

const std::string helios::engine::util::log::Logger::scope_ = "default"

Definition at line 45 of file Logger.ixx.

45 const std::string scope_ = "default";

sinkMutex_

std::mutex helios::engine::util::log::Logger::sinkMutex_
mutable

Mutex for thread-safe sink access.

Definition at line 60 of file Logger.ixx.

60 mutable std::mutex sinkMutex_;

sinks_

std::vector<std::shared_ptr<LogSink> > helios::engine::util::log::Logger::sinks_

Collection of output sinks.

Definition at line 55 of file Logger.ixx.

55 std::vector<std::shared_ptr<LogSink>> sinks_;

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


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.