14#include <source_location>
15#include <spdlog/spdlog.h>
16#include <spdlog/sinks/basic_file_sink.h>
17#include <spdlog/sinks/stdout_color_sinks.h>
48 virtual void trace(std::string_view message) = 0;
49 virtual void debug(std::string_view message) = 0;
50 virtual void info(std::string_view message) = 0;
51 virtual void warn(std::string_view message) = 0;
52 virtual void error(std::string_view message) = 0;
53 virtual void critical(std::string_view message) = 0;
78 std::vector<spdlog::sink_ptr> sinks;
82 auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
84 sinks.push_back(console_sink);
87 if (!logFile.empty()) {
88 auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(logFile,
true);
90 sinks.push_back(file_sink);
93 auto logger = std::make_shared<spdlog::logger>(name, sinks.begin(), sinks.end());
95 spdlog::set_default_logger(logger);
102 spdlog::flush_on(spdlog::level::trace);
106 void trace(std::string_view message)
override
111 void debug(std::string_view message)
override
116 void info(std::string_view message)
override {
m_logger->info(message); }
118 void warn(std::string_view message)
override {
m_logger->warn(message); }
120 void error(std::string_view message)
override
137 for (
auto& sink :
m_logger->sinks()) {
138 sink->set_level(spdlog_level);
152 return spdlog::level::trace;
154 return spdlog::level::debug;
156 return spdlog::level::info;
158 return spdlog::level::warn;
160 return spdlog::level::err;
162 return spdlog::level::critical;
164 return spdlog::level::off;
166 return spdlog::level::info;
172 case spdlog::level::trace:
174 case spdlog::level::debug:
176 case spdlog::level::info:
178 case spdlog::level::warn:
180 case spdlog::level::err:
182 case spdlog::level::critical:
184 case spdlog::level::off:
223 const std::string& logFile =
"")
229 s_instance = std::make_shared<SpdLogger>(level, logFile);
237 if (levelStr ==
"trace")
239 else if (levelStr ==
"debug")
241 else if (levelStr ==
"info")
243 else if (levelStr ==
"warning")
245 else if (levelStr ==
"error")
247 else if (levelStr ==
"critical")
249 else if (levelStr ==
"off")
278 template<
typename... Args>
279 static void Trace(fmt::format_string<Args...> fmt, Args&&... args)
281 GetInstance()->trace(fmt::format(fmt, std::forward<Args>(args)...));
284 template<
typename... Args>
285 static void Debug(fmt::format_string<Args...> fmt, Args&&... args)
287 GetInstance()->debug(fmt::format(fmt, std::forward<Args>(args)...));
290 template<
typename... Args>
291 static void Info(fmt::format_string<Args...> fmt, Args&&... args)
293 GetInstance()->info(fmt::format(fmt, std::forward<Args>(args)...));
296 template<
typename... Args>
297 static void Warn(fmt::format_string<Args...> fmt, Args&&... args)
299 GetInstance()->warn(fmt::format(fmt, std::forward<Args>(args)...));
302 template<
typename... Args>
303 static void Error(fmt::format_string<Args...> fmt, Args&&... args)
305 GetInstance()->error(fmt::format(fmt, std::forward<Args>(args)...));
308 template<
typename... Args>
309 static void Critical(fmt::format_string<Args...> fmt, Args&&... args)
311 GetInstance()->critical(fmt::format(fmt, std::forward<Args>(args)...));
Interface for logging implementations.
Definition Logger.hpp:44
virtual void trace(std::string_view message)=0
virtual void info(std::string_view message)=0
virtual void critical(std::string_view message)=0
virtual ~ILogger()=default
virtual void error(std::string_view message)=0
virtual void setLevel(LogLevel level)=0
virtual void debug(std::string_view message)=0
virtual LogLevel getLevel() const =0
virtual void warn(std::string_view message)=0
Application-wide logging singleton.
Definition Logger.hpp:215
static LogLevel GetLevel()
Gets the current logging level.
Definition Logger.hpp:327
static void Warn(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logger.hpp:297
static std::shared_ptr< ILogger > s_instance
Definition Logger.hpp:330
static void Critical(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logger.hpp:309
static void Initialize(LogLevel level=LogLevel::Info, const std::string &logFile="")
Initializes the logger with default configuration.
Definition Logger.hpp:222
static void SetLevel(LogLevel level)
Sets the global logging level.
Definition Logger.hpp:318
static void SetInstance(std::shared_ptr< ILogger > logger)
Sets a custom logger instance (for testing)
Definition Logger.hpp:259
static void Trace(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logger.hpp:279
static void Debug(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logger.hpp:285
static LogLevel fromStrLevel(const std::string &levelStr)
Convert string to log level.
Definition Logger.hpp:235
static void Error(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logger.hpp:303
static void Info(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logger.hpp:291
static std::shared_ptr< ILogger > GetInstance()
Gets the current logger instance.
Definition Logger.hpp:268
spdlog-based implementation of ILogger
Definition Logger.hpp:66
void warn(std::string_view message) override
Definition Logger.hpp:118
void error(std::string_view message) override
Definition Logger.hpp:120
std::shared_ptr< spdlog::logger > m_logger
Definition Logger.hpp:191
static spdlog::level::level_enum toSpdlogLevel(LogLevel level)
Definition Logger.hpp:148
static LogLevel fromSpdlogLevel(spdlog::level::level_enum level)
Definition Logger.hpp:169
void setLevel(LogLevel level) override
Definition Logger.hpp:130
SpdLogger(LogLevel level=LogLevel::Debug, const std::string &logFile="", const std::string &name="LogViewer")
Constructs a logger implementation backed by spdlog.
Definition Logger.hpp:75
void debug(std::string_view message) override
Definition Logger.hpp:111
void info(std::string_view message) override
Definition Logger.hpp:116
void trace(std::string_view message) override
Definition Logger.hpp:106
void critical(std::string_view message) override
Definition Logger.hpp:125
LogLevel getLevel() const override
Definition Logger.hpp:142
Utility components and helper types for the LogViewer application.
LogLevel
Logging severity levels.
Definition Logger.hpp:27
@ Warning
Warning messages for potential issues.
@ Critical
Critical errors that may cause termination.
@ Info
General information about application flow.
@ Error
Error messages for failures.
@ Debug
Detailed debug information for development.
@ Off
Disable all logging.
@ Trace
Very detailed debug information.