20#include <unordered_map>
24#include "log_streambuffer.h"
41 virtual void addHandler(std::unique_ptr<AbstractHandler> handlerPtr) = 0;
49 virtual void log(LogLevel lvl,
const std::string &message)
const = 0;
62 template <
typename... Args>
void log(LogLevel lvl,
const std::string &fmt, Args &&...args)
const
64 std::string formatted = std::vformat(fmt, std::make_format_args(args...));
68#pragma region LogLevelShortcuts
74 template <
typename... Args>
void trace(
const std::string &fmt, Args &&...args)
76 log(LogLevel::TRACE, fmt, std::forward<Args>(args)...);
83 template <
typename... Args>
void verbose(
const std::string &fmt, Args &&...args)
85 log(LogLevel::VERBOSE, fmt, std::forward<Args>(args)...);
92 template <
typename... Args>
void debug(
const std::string &fmt, Args &&...args)
94 log(LogLevel::DEBUG, fmt, std::forward<Args>(args)...);
101 template <
typename... Args>
void success(
const std::string &fmt, Args &&...args)
103 log(LogLevel::SUCCESS, fmt, std::forward<Args>(args)...);
110 template <
typename... Args>
void info(
const std::string &fmt, Args &&...args)
112 log(LogLevel::INFO, fmt, std::forward<Args>(args)...);
119 template <
typename... Args>
void warning(
const std::string &fmt, Args &&...args)
121 log(LogLevel::WARNING, fmt, std::forward<Args>(args)...);
128 template <
typename... Args>
void error(
const std::string &fmt, Args &&...args)
130 log(LogLevel::ERROR, fmt, std::forward<Args>(args)...);
133#pragma endregion LogLevelShortcuts
135#pragma region LogStreamShortcuts
165#pragma endregion LogStreamShortcuts
182 explicit Logger(LogLevel lvl = LogLevel::TRACE);
186 void setLevel(LogLevel lvl)
override;
190 void addHandler(std::unique_ptr<AbstractHandler> handlerPtr)
override;
195 void log(LogLevel lvl,
const std::string& msg)
const override;
197 std::ostream&
getStream(LogLevel lvl)
override;
201 std::vector<std::unique_ptr<AbstractHandler>> handlers;
204 std::unordered_map<LogLevel, std::unique_ptr<LogStreamBuf>> buffers;
205 std::unordered_map<LogLevel, std::unique_ptr<std::ostream>> streams;
Abstract base class for logger implementations.
Definition logger.h:31
virtual void removeAllHandlers()=0
Remove all log handlers.
virtual void setLevel(LogLevel lvl)=0
Set the minimum log level for this logger.
std::ostream & trace()
Get the stream for the TRACE log level.
Definition logger.h:139
void warning(const std::string &fmt, Args &&...args)
Log a message at the WARNING level.
Definition logger.h:119
void verbose(const std::string &fmt, Args &&...args)
Log a message at the VERBOSE level.
Definition logger.h:83
void trace(const std::string &fmt, Args &&...args)
Log a message at the TRACE level.
Definition logger.h:74
virtual void log(LogLevel lvl, const std::string &message) const =0
Log a message at the specified log level.
void info(const std::string &fmt, Args &&...args)
Log a message at the INFO level.
Definition logger.h:110
void debug(const std::string &fmt, Args &&...args)
Log a message at the DEBUG level.
Definition logger.h:92
std::ostream & verbose()
Get the stream for the VERBOSE log level.
Definition logger.h:143
std::ostream & error()
Get the stream for the ERROR log level.
Definition logger.h:163
std::ostream & success()
Get the stream for the SUCCESS log level.
Definition logger.h:151
void log(LogLevel lvl, const std::string &fmt, Args &&...args) const
Log a message at the specified log level using a format string to print the passed arguments.
Definition logger.h:62
std::ostream & debug()
Get the stream for the DEBUG log level.
Definition logger.h:147
void error(const std::string &fmt, Args &&...args)
Log a message at the ERROR level.
Definition logger.h:128
std::ostream & warning()
Get the stream for the WARNING log level.
Definition logger.h:159
virtual std::ostream & getStream(LogLevel lvl)=0
Get the stream for the specified log level.
std::ostream & info()
Get the stream for the INFO log level.
Definition logger.h:155
virtual void addHandler(std::unique_ptr< AbstractHandler > handlerPtr)=0
Add a log handler.
void success(const std::string &fmt, Args &&...args)
Log a message at the SUCCESS level.
Definition logger.h:101
Logger class for handling log messages.
Definition logger.h:170
void removeAllHandlers() override
Remove all log handlers.
Definition logger.h:193
std::ostream & getStream(LogLevel lvl) override
Get the stream for the specified log level.
Definition logger.cpp:66
void addHandler(std::unique_ptr< AbstractHandler > handlerPtr) override
Add a log handler.
Definition logger.cpp:49
void log(LogLevel lvl, const std::string &msg) const override
Log a message at the specified log level.
Definition logger.cpp:54
void setLevel(LogLevel lvl) override
Set the minimum log level for this logger.
Definition logger.cpp:40