LogViewer
A modern log viewer with AI-assisted analysis (Qt 6)
Loading...
Searching...
No Matches
Logger.hpp
Go to the documentation of this file.
1
11#pragma once
12
13#include <memory>
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>
18#include <string>
19#include <string_view>
20
21namespace util {
22
27enum class LogLevel {
28 Trace,
29 Debug,
30 Info,
31 Warning,
32 Error,
33 Critical,
34 Off
35};
36
44class ILogger {
45public:
46 virtual ~ILogger() = default;
47
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;
54
55 virtual void setLevel(LogLevel level) = 0;
56 virtual LogLevel getLevel() const = 0;
57};
58
66class SpdLogger : public ILogger {
67public:
75 explicit SpdLogger(LogLevel level = LogLevel::Debug, const std::string& logFile = "", const std::string& name = "LogViewer")
76 {
77 if (m_logger.get() == nullptr) {
78 std::vector<spdlog::sink_ptr> sinks;
79
80
81 // Console sink for debug output
82 auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
83 console_sink->set_level(toSpdlogLevel(level));
84 sinks.push_back(console_sink);
85
86 // Additional spdlog configuration can go here
87 if (!logFile.empty()) {
88 auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(logFile, true);
89 file_sink->set_level(toSpdlogLevel(level));
90 sinks.push_back(file_sink);
91 }
92
93 auto logger = std::make_shared<spdlog::logger>(name, sinks.begin(), sinks.end());
94 logger->set_level(toSpdlogLevel(level));
95 spdlog::set_default_logger(logger);
96 m_logger = std::move(logger);
97 // Flush on every log call (trace and above covers all levels).
98 // Do NOT use spdlog::flush_every(): it spawns a background thread
99 // that sleeps periodically and then flushes to disk — the exact
100 // pattern (background thread + timed sleep + file I/O) that
101 // triggers Windows Defender's ransomware ML heuristic (Wacatac.C!ml).
102 spdlog::flush_on(spdlog::level::trace);
103 }
104 }
105
106 void trace(std::string_view message) override
107 {
108 m_logger->trace(message);
109 }
110
111 void debug(std::string_view message) override
112 {
113 m_logger->debug(message);
114 }
115
116 void info(std::string_view message) override { m_logger->info(message); }
117
118 void warn(std::string_view message) override { m_logger->warn(message); }
119
120 void error(std::string_view message) override
121 {
122 m_logger->error(message);
123 }
124
125 void critical(std::string_view message) override
126 {
127 m_logger->critical(message);
128 }
129
130 void setLevel(LogLevel level) override
131 {
132 auto spdlog_level = toSpdlogLevel(level);
133 m_logger->set_level(spdlog_level);
134 // Update all sink levels to match — in spdlog, both logger AND sinks have independent
135 // level checks. If only logger level changes, sinks may reject messages below their
136 // individual levels, causing debug/trace to disappear from file even if logger allows it.
137 for (auto& sink : m_logger->sinks()) {
138 sink->set_level(spdlog_level);
139 }
140 }
141
142 LogLevel getLevel() const override
143 {
144 return fromSpdlogLevel(m_logger->level());
145 }
146
147private:
148 static spdlog::level::level_enum toSpdlogLevel(LogLevel level)
149 {
150 switch (level) {
151 case LogLevel::Trace:
152 return spdlog::level::trace;
153 case LogLevel::Debug:
154 return spdlog::level::debug;
155 case LogLevel::Info:
156 return spdlog::level::info;
158 return spdlog::level::warn;
159 case LogLevel::Error:
160 return spdlog::level::err;
162 return spdlog::level::critical;
163 case LogLevel::Off:
164 return spdlog::level::off;
165 }
166 return spdlog::level::info;
167 }
168
169 static LogLevel fromSpdlogLevel(spdlog::level::level_enum level)
170 {
171 switch (level) {
172 case spdlog::level::trace:
173 return LogLevel::Trace;
174 case spdlog::level::debug:
175 return LogLevel::Debug;
176 case spdlog::level::info:
177 return LogLevel::Info;
178 case spdlog::level::warn:
179 return LogLevel::Warning;
180 case spdlog::level::err:
181 return LogLevel::Error;
182 case spdlog::level::critical:
183 return LogLevel::Critical;
184 case spdlog::level::off:
185 return LogLevel::Off;
186 default:
187 return LogLevel::Info;
188 }
189 }
190
191 std::shared_ptr<spdlog::logger> m_logger;
192};
193
215class Logger {
216public:
222 static void Initialize(LogLevel level = LogLevel::Info,
223 const std::string& logFile = "")
224 {
225 if (s_instance) {
226 return; // Already initialized
227 }
228
229 s_instance = std::make_shared<SpdLogger>(level, logFile);
230 }
231
235 static LogLevel fromStrLevel(const std::string& levelStr)
236 {
237 if (levelStr == "trace")
238 return LogLevel::Trace;
239 else if (levelStr == "debug")
240 return LogLevel::Debug;
241 else if (levelStr == "info")
242 return LogLevel::Info;
243 else if (levelStr == "warning")
244 return LogLevel::Warning;
245 else if (levelStr == "error")
246 return LogLevel::Error;
247 else if (levelStr == "critical")
248 return LogLevel::Critical;
249 else if (levelStr == "off")
250 return LogLevel::Off;
251 else
252 return LogLevel::Info;
253 }
254
259 static void SetInstance(std::shared_ptr<ILogger> logger)
260 {
261 s_instance = std::move(logger);
262 }
263
268 static std::shared_ptr<ILogger> GetInstance()
269 {
270 if (!s_instance) {
271 Initialize();
272 }
273 return s_instance;
274 }
275
276 // Convenience methods
277
278 template<typename... Args>
279 static void Trace(fmt::format_string<Args...> fmt, Args&&... args)
280 {
281 GetInstance()->trace(fmt::format(fmt, std::forward<Args>(args)...));
282 }
283
284 template<typename... Args>
285 static void Debug(fmt::format_string<Args...> fmt, Args&&... args)
286 {
287 GetInstance()->debug(fmt::format(fmt, std::forward<Args>(args)...));
288 }
289
290 template<typename... Args>
291 static void Info(fmt::format_string<Args...> fmt, Args&&... args)
292 {
293 GetInstance()->info(fmt::format(fmt, std::forward<Args>(args)...));
294 }
295
296 template<typename... Args>
297 static void Warn(fmt::format_string<Args...> fmt, Args&&... args)
298 {
299 GetInstance()->warn(fmt::format(fmt, std::forward<Args>(args)...));
300 }
301
302 template<typename... Args>
303 static void Error(fmt::format_string<Args...> fmt, Args&&... args)
304 {
305 GetInstance()->error(fmt::format(fmt, std::forward<Args>(args)...));
306 }
307
308 template<typename... Args>
309 static void Critical(fmt::format_string<Args...> fmt, Args&&... args)
310 {
311 GetInstance()->critical(fmt::format(fmt, std::forward<Args>(args)...));
312 }
313
318 static void SetLevel(LogLevel level)
319 {
320 GetInstance()->setLevel(level);
321 }
322
327 static LogLevel GetLevel() { return GetInstance()->getLevel(); }
328
329private:
330 static std::shared_ptr<ILogger> s_instance;
331};
332
333// Definition must be in .cpp file
334inline std::shared_ptr<ILogger> Logger::s_instance = nullptr;
335
336} // namespace util
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.