MantisBase v0.3.4
Loading...
Searching...
No Matches
logger.h
Go to the documentation of this file.
1
8#ifndef MANTIS_LOGGER_H
9#define MANTIS_LOGGER_H
10
11#include <nlohmann/json.hpp>
12#include <spdlog/spdlog.h>
13#include <spdlog/sinks/basic_file_sink.h>
14#include <memory>
15
16#include "log_database.h"
17#include "../../utils/utils.h"
18
19namespace mb {
20 using json = nlohmann::json;
21
22 // Forward declaration
23 class LogDatabase;
24
28 typedef enum class LogLevel : uint8_t {
29 TRACE = 0,
30 DEBUG,
31 INFO,
32 WARN,
35
40 class Logger {
41 std::unique_ptr<LogDatabase> m_logsDb;
42
43 public:
44 Logger();
45
46 inline static std::atomic<bool> isDbInitialized = false;
47
48 static void setLogLevel(const LogLevel &level = LogLevel::INFO);
49
54 [[nodiscard]] LogDatabase &logsDb() const;
55
59 static void initDb(const std::string &data_dir = "");
60
61 // Logging methods with structured format:
62 // - origin: Component/system origin (System, Auth, Database, Entity, EntitySchema, etc.)
63 // - message: Short message (e.g., "Auth Failed", "Database Connected")
64 // - details: Long description of the message
65 // - data: Optional JSON object/array with additional data
66 static void trace(const std::string &origin,
67 const std::string &message,
68 const std::string &details = "",
69 const json &data = json::object());
70
71 static void info(const std::string &origin,
72 const std::string &message,
73 const std::string &details = "",
74 const json &data = json::object());
75
76 static void debug(const std::string &origin,
77 const std::string &message,
78 const std::string &details = "",
79 const json &data = json::object());
80
81 static void warn(const std::string &origin,
82 const std::string &message,
83 const std::string &details = "",
84 const json &data = json::object());
85
86 static void critical(const std::string &origin,
87 const std::string &message,
88 const std::string &details = "",
89 const json &data = json::object());
90
91 private:
92 template<typename... Args>
93 static void trace_spdlog(fmt::format_string<Args...> msg, Args &&... args) {
94 spdlog::trace(msg, std::forward<Args>(args)...);
95 }
96
97 template<typename... Args>
98 static void info_spdlog(fmt::format_string<Args...> msg, Args &&... args) {
99 spdlog::info(msg, std::forward<Args>(args)...);
100 }
101
102 template<typename... Args>
103 static void debug_spdlog(fmt::format_string<Args...> msg, Args &&... args) {
104 spdlog::debug(msg, std::forward<Args>(args)...);
105 }
106
107 template<typename... Args>
108 static void warn_spdlog(fmt::format_string<Args...> msg, Args &&... args) {
109 spdlog::warn(msg, std::forward<Args>(args)...);
110 }
111
112 template<typename... Args>
113 static void critical_spdlog(fmt::format_string<Args...> msg, Args &&... args) {
114 spdlog::critical(msg, std::forward<Args>(args)...);
115 }
116
117 static void logToDatabase(const std::string &level,
118 const std::string &origin,
119 const std::string &message,
120 const std::string &details,
121 const json &data = json::object());
122 };
123
124 namespace logEntry {
125 // System logging
126 inline void trace(const std::string &origin,
127 const std::string &message,
128 const std::string &details = "",
129 const json &data = json::object()) {
130 Logger::trace(origin, message, details, data);
131 }
132
133 inline void info(const std::string &origin,
134 const std::string &message,
135 const std::string &details = "",
136 const json &data = json::object()) {
137 Logger::info(origin, message, details, data);
138 }
139
140 inline void debug(const std::string &origin,
141 const std::string &message,
142 const std::string &details = "",
143 const json &data = json::object()) {
144 Logger::debug(origin, message, details, data);
145 }
146
147 inline void warn(const std::string &origin,
148 const std::string &message,
149 const std::string &details = "",
150 const json &data = json::object()) {
151 Logger::warn(origin, message, details, data);
152 }
153
154 inline void critical(const std::string &origin,
155 const std::string &message,
156 const std::string &details = "",
157 const json &data = json::object()) {
158 Logger::critical(origin, message, details, data);
159 }
160 }
161
166 namespace LogOrigin {
167 // System logging
168 inline void trace(const std::string &message,
169 const std::string &details = "",
170 const json &data = json::object()) {
171 Logger::trace("System", message, details, data);
172 }
173
174 inline void info(const std::string &message,
175 const std::string &details = "",
176 const json &data = json::object()) {
177 Logger::info("System", message, details, data);
178 }
179
180 inline void debug(const std::string &message,
181 const std::string &details = "",
182 const json &data = json::object()) {
183 Logger::debug("System", message, details, data);
184 }
185
186 inline void warn(const std::string &message,
187 const std::string &details = "",
188 const json &data = json::object()) {
189 Logger::warn("System", message, details, data);
190 }
191
192 inline void critical(const std::string &message,
193 const std::string &details = "",
194 const json &data = json::object()) {
195 Logger::critical("System", message, details, data);
196 }
197
198 // Auth logging
199 inline void authTrace(const std::string &message,
200 const std::string &details = "",
201 const json &data = json::object()) {
202 Logger::trace("Auth", message, details, data);
203 }
204
205 inline void authInfo(const std::string &message,
206 const std::string &details = "",
207 const json &data = json::object()) {
208 Logger::info("Auth", message, details, data);
209 }
210
211 inline void authDebug(const std::string &message,
212 const std::string &details = "",
213 const json &data = json::object()) {
214 Logger::debug("Auth", message, details, data);
215 }
216
217 inline void authWarn(const std::string &message,
218 const std::string &details = "",
219 const json &data = json::object()) {
220 Logger::warn("Auth", message, details, data);
221 }
222
223 inline void authCritical(const std::string &message,
224 const std::string &details = "",
225 const json &data = json::object()) {
226 Logger::critical("Auth", message, details, data);
227 }
228
229 // Database logging
230 inline void dbTrace(const std::string &message,
231 const std::string &details = "",
232 const json &data = json::object()) {
233 Logger::trace("Database", message, details, data);
234 }
235
236 inline void dbInfo(const std::string &message,
237 const std::string &details = "",
238 const json &data = json::object()) {
239 Logger::info("Database", message, details, data);
240 }
241
242 inline void dbDebug(const std::string &message,
243 const std::string &details = "",
244 const json &data = json::object()) {
245 Logger::debug("Database", message, details, data);
246 }
247
248 inline void dbWarn(const std::string &message,
249 const std::string &details = "",
250 const json &data = json::object()) {
251 Logger::warn("Database", message, details, data);
252 }
253
254 inline void dbCritical(const std::string &message,
255 const std::string &details = "",
256 const json &data = json::object()) {
257 Logger::critical("Database", message, details, data);
258 }
259
260 // Entity logging
261 inline void entityTrace(const std::string &message,
262 const std::string &details = "",
263 const json &data = json::object()) {
264 Logger::trace("Entity", message, details, data);
265 }
266
267 inline void entityInfo(const std::string &message,
268 const std::string &details = "",
269 const json &data = json::object()) {
270 Logger::info("Entity", message, details, data);
271 }
272
273 inline void entityDebug(const std::string &message,
274 const std::string &details = "",
275 const json &data = json::object()) {
276 Logger::debug("Entity", message, details, data);
277 }
278
279 inline void entityWarn(const std::string &message,
280 const std::string &details = "",
281 const json &data = json::object()) {
282 Logger::warn("Entity", message, details, data);
283 }
284
285 inline void entityCritical(const std::string &message,
286 const std::string &details = "",
287 const json &data = json::object()) {
288 Logger::critical("Entity", message, details, data);
289 }
290
291 // EntitySchema logging
292 inline void entitySchemaTrace(const std::string &message,
293 const std::string &details = "",
294 const json &data = json::object()) {
295 Logger::trace("EntitySchema", message, details, data);
296 }
297
298 inline void entitySchemaInfo(const std::string &message,
299 const std::string &details = "",
300 const json &data = json::object()) {
301 Logger::info("EntitySchema", message, details, data);
302 }
303
304 inline void entitySchemaDebug(const std::string &message,
305 const std::string &details = "",
306 const json &data = json::object()) {
307 Logger::debug("EntitySchema", message, details, data);
308 }
309
310 inline void entitySchemaWarn(const std::string &message,
311 const std::string &details = "",
312 const json &data = json::object()) {
313 Logger::warn("EntitySchema", message, details, data);
314 }
315
316 inline void entitySchemaCritical(const std::string &message,
317 const std::string &details = "",
318 const json &data = json::object()) {
319 Logger::critical("EntitySchema", message, details, data);
320 }
321 }
322
328 public:
329 explicit FuncLogger(std::string msg);
330
331 ~FuncLogger();
332
333 private:
334 std::string m_msg;
335 };
336}
337
338// Macro to automatically insert logger with function name
339inline std::string getFile(const std::string &path) {
340 const std::filesystem::path p = path;
341 return p.filename().string();
342}
343
344#define MANTIS_FUNC() std::format("{} - {}()", getFile(__FILE__), __FUNCTION__)
345#define TRACE_FUNC(x) mb::FuncLogger _logger(x);
346#define TRACE_MANTIS_FUNC() try{ mb::FuncLogger _logger(MANTIS_FUNC()); } catch(...){}
347#define TRACE_CLASS_METHOD() mb::FuncLogger _logger(std::format("{} {}::{}()", getFile(__FILE__), "", __FUNCTION__));
348#define TRACE_METHOD() mb::FuncLogger _logger(std::format("{} {}()", getFile(__FILE__), __FUNCTION__));
349
350#endif //MANTIS_LOGGER_H
A class for tracing function execution [entry, exit] useful in following execution flow.
Definition logger.h:327
~FuncLogger()
Definition logger.cpp:178
Manages SQLite database for application logs.
Definition log_database.h:33
Definition logger.h:40
static void debug(const std::string &origin, const std::string &message, const std::string &details="", const json &data=json::object())
Definition logger.cpp:111
Logger()
Definition logger.cpp:40
static void trace(const std::string &origin, const std::string &message, const std::string &details="", const json &data=json::object())
Definition logger.cpp:69
static void info(const std::string &origin, const std::string &message, const std::string &details="", const json &data=json::object())
Definition logger.cpp:90
static void critical(const std::string &origin, const std::string &message, const std::string &details="", const json &data=json::object())
Definition logger.cpp:153
static std::atomic< bool > isDbInitialized
Definition logger.h:46
LogDatabase & logsDb() const
Get the log database instance (for API access).
Definition logger.cpp:54
static void setLogLevel(const LogLevel &level=LogLevel::INFO)
Definition logger.cpp:10
static void warn(const std::string &origin, const std::string &message, const std::string &details="", const json &data=json::object())
Definition logger.cpp:132
static void initDb(const std::string &data_dir="")
Initialize database connection.
Definition logger.cpp:58
SQLite database manager for application logs.
std::string getFile(const std::string &path)
Definition logger.h:339
router.h
Definition auth.h:15
LogLevel
Definition logger.h:28
@ WARN
‍Info Logging Level
@ INFO
‍Debug Logging Level
@ CRITICAL
‍Warning Logging Level
@ DEBUG
‍Trace logging level
nlohmann::json json
Shorten JSON namespace.
Definition context_store.h:18