42#include <source_location>
48namespace utility::logging
99 std::atomic<LogLevel> _minLevel {
102 std::atomic<FlushPolicy> _flushPolicy {
103 FlushPolicy::BUFFERED
115 bool isEnabled(
LogLevel level)
const noexcept
118 >=
levelValue(_minLevel.load(std::memory_order_relaxed));
136 std::source_location _location;
137 std::unique_ptr<std::ostringstream>
145 std::ostringstream &ensureStream(
void)
148 _stream = std::make_unique<std::ostringstream>();
179 ensureStream() << std::forward<T>(value);
192 ensureStream() << manip;
207 if (!_active || !_logger || !_stream) {
215 .message = std::move(*_stream).str(),
217 .loggerName = _logger->
getName(),
219 if (_level == LogLevel::DEBUG_LEVEL) {
220 record.file = _location.file_name();
221 record.line =
static_cast<int>(_location.line());
222 record.function = _location.function_name();
224 std::lock_guard<std::mutex> guard(_logger->
_mutex);
238 Logger(
const std::string &name);
258 return LogLevel::WARNING_LEVEL;
260 return LogLevel::DEBUG_LEVEL;
315 case LogLevel::DEBUG_LEVEL:
317 case LogLevel::INFO_LEVEL:
319 case LogLevel::WARNING_LEVEL:
321 case LogLevel::ERROR_LEVEL:
326 return std::numeric_limits<int>::max();
336 debug(std::source_location loc = std::source_location::current())
338 return LogMessage(
this, LogLevel::DEBUG_LEVEL, loc,
339 isEnabled(LogLevel::DEBUG_LEVEL));
348 info(std::source_location loc = std::source_location::current())
350 return LogMessage(
this, LogLevel::INFO_LEVEL, loc,
351 isEnabled(LogLevel::INFO_LEVEL));
360 warning(std::source_location loc = std::source_location::current())
362 return LogMessage(
this, LogLevel::WARNING_LEVEL, loc,
363 isEnabled(LogLevel::WARNING_LEVEL));
372 error(std::source_location loc = std::source_location::current())
374 return LogMessage(
this, LogLevel::ERROR_LEVEL, loc,
375 isEnabled(LogLevel::ERROR_LEVEL));
386 std::source_location loc = std::source_location::current())
388 return LogMessage(
this, level, loc, isEnabled(level));
411 template<
typename Type>
Proxy object returned by debug/info/warning/error.
~LogMessage()
Destructor emits the accumulated message via the parent logger.
LogMessage & operator<<(T &&value)
Stream any value into the log buffer.
LogMessage & operator<<(std::ostream &(*manip)(std::ostream &))
Stream an ostream manipulator (e.g. std::endl).
LogMessage(Logger *logger, LogLevel level, std::source_location loc, bool active)
Construct a LogMessage.
Abstract logger interface defining stream-style logging operations.
std::mutex _mutex
Serializes output access.
LogLevel getMinLevel(void) const noexcept
Get the current minimum log level.
static constexpr LogLevel defaultMinLevel(void) noexcept
Default minimum level for the current build type.
virtual void output(const LogRecord &record)=0
Output a fully-formed log record.
const std::string & getName(void) const
Get the logger name.
LogMessage info(std::source_location loc=std::source_location::current())
Begin an info-level log message.
FlushPolicy getFlushPolicy(void) const noexcept
Get the current output flush policy.
void setMinLevel(LogLevel level) noexcept
Set the minimum log level. Messages below this level are suppressed.
LogMessage debug(std::source_location loc=std::source_location::current())
Begin a debug-level log message.
LogMessage error(std::source_location loc=std::source_location::current())
Begin an error-level log message.
static std::string levelToString(LogLevel level)
Get string representation of log level.
void setFlushPolicy(FlushPolicy policy) noexcept
Set the output flush policy.
LogMessage warning(std::source_location loc=std::source_location::current())
Begin a warning-level log message.
static std::string getTimestamp(void)
Get current timestamp as formatted string.
static constexpr int levelValue(LogLevel level) noexcept
Get numeric value of a log level for comparison.
LogMessage log(LogLevel level, std::source_location loc=std::source_location::current())
Begin a log message with dynamic level.
virtual ~Logger(void)=default
Virtual destructor for proper cleanup.
Concept to ensure a type inherits from Logger.
FlushPolicy
Controls when a stream-backed logger flushes its output.
@ NEVER
Never flush except on destruction.
@ BUFFERED
Buffer debug/info; flush warnings/errors (default)
@ ALWAYS
Flush after every message (legacy behavior)
LogLevel
Log severity levels.
@ ERROR_LEVEL
Error messages for serious problems.
@ DEBUG_LEVEL
Detailed debugging information.
@ INFO_LEVEL
General informational messages.
Structured log record carrying metadata for a single log entry.
LogLevel level
Severity level.
int line
Source line number.
std::string file
Source file path.
std::string loggerName
Name of the logger that emitted the record.
std::string timestamp
Formatted timestamp string.
std::string function
Function name.
std::string message
Log message content.