MantisBase v0.4.9
Loading...
Searching...
No Matches
logger.h
Go to the documentation of this file.
1
7
8#ifndef MB_LOGGER_H
9#define MB_LOGGER_H
10
11#include <iostream>
12#include <nlohmann/json.hpp>
13#include <spdlog/spdlog.h>
14#include <spdlog/sinks/basic_file_sink.h>
15#include <memory>
16
17#include "log_database.h"
18#include "../../utils/utils.h"
19#include "../../core/types.h"
20
21namespace mb {
22 using json = nlohmann::json;
23
24 // Forward declaration
25 class MbLogDatabase;
26
30 typedef enum class MbLogLevel : uint8_t {
31 TRACE = 0,
37
43 std::unique_ptr<MbLogDatabase> m_logsDb;
44 const MantisBase &mApp;
45
46 public:
47 explicit MbLogger(const MantisBase &app);
48
49 static void setLogLevel(const MbLogLevel &level = MbLogLevel::INFO);
50
55 [[nodiscard]] MbLogDatabase &logsDb() const;
56
60 void initDb(const std::string &data_dir = "") const;
61
62 // Logging methods with structured format:
63 // - origin: Component/system origin (System, MbAuth, MbDatabase, MbEntity, MbEntitySchema, etc.)
64 // - message: Short message (e.g., "MbAuth Failed", "MbDatabase Connected")
65 // - details: Long description of the message
66 // - data: Optional JSON object/array with additional data
67 void trace(const std::string &origin,
68 const std::string &message,
69 const std::string &details = "",
70 const json &data = json::object()) const;
71
72 void info(const std::string &origin,
73 const std::string &message,
74 const std::string &details = "",
75 const json &data = json::object()) const;
76
77 void debug(const std::string &origin,
78 const std::string &message,
79 const std::string &details = "",
80 const json &data = json::object()) const;
81
82 void warn(const std::string &origin,
83 const std::string &message,
84 const std::string &details = "",
85 const json &data = json::object()) const;
86
87 void critical(const std::string &origin,
88 const std::string &message,
89 const std::string &details = "",
90 const json &data = json::object()) const;
91
92#ifdef MB_SCRIPTING_ENABLED
93 void jsInfo(const std::string &message) const;
94 void jsWarn(const std::string &message) const;
95 void jsError(const std::string &message) const;
96 void jsCritical(const std::string &message) const;
97 void jsDebug(const std::string &message) const;
98 void jsTrace(const std::string &message) const;
99#endif
100
101 inline static std::atomic<bool> isDbInitialized = false;
102
103 private:
104 template<typename... Args>
105 static void trace_spdlog(fmt::format_string<Args...> msg, Args &&... args) {
106 spdlog::trace(msg, std::forward<Args>(args)...);
107 }
108
109 template<typename... Args>
110 static void info_spdlog(fmt::format_string<Args...> msg, Args &&... args) {
111 spdlog::info(msg, std::forward<Args>(args)...);
112 }
113
114 template<typename... Args>
115 static void debug_spdlog(fmt::format_string<Args...> msg, Args &&... args) {
116 spdlog::debug(msg, std::forward<Args>(args)...);
117 }
118
119 template<typename... Args>
120 static void warn_spdlog(fmt::format_string<Args...> msg, Args &&... args) {
121 spdlog::warn(msg, std::forward<Args>(args)...);
122 }
123
124 template<typename... Args>
125 static void critical_spdlog(fmt::format_string<Args...> msg, Args &&... args) {
126 spdlog::critical(msg, std::forward<Args>(args)...);
127 }
128
129 void logToDatabase(const std::string &level,
130 const std::string &origin,
131 const std::string &message,
132 const std::string &details,
133 const json &data = json::object()) const;
134 };
135}
136
137// Macro to automatically insert logger with function name
138inline std::string getFile(const std::string &path) {
139 const std::filesystem::path p = path;
140 return p.filename().string();
141}
142
143#define MB_FUNC() std::format("{} - {}()", getFile(__FILE__), __FUNCTION__)
144
145#endif // MB_LOGGER_H
MantisBase entry point.
Definition mantisbase.h:54
Manages SQLite database for application logs.
Definition log_database.h:35
static std::atomic< bool > isDbInitialized
Definition logger.h:101
void critical(const std::string &origin, const std::string &message, const std::string &details="", const json &data=json::object()) const
Definition logger.cpp:162
void warn(const std::string &origin, const std::string &message, const std::string &details="", const json &data=json::object()) const
Definition logger.cpp:139
static void setLogLevel(const MbLogLevel &level=MbLogLevel::INFO)
Definition logger.cpp:10
void debug(const std::string &origin, const std::string &message, const std::string &details="", const json &data=json::object()) const
Definition logger.cpp:116
MbLogger(const MantisBase &app)
Definition logger.cpp:40
void info(const std::string &origin, const std::string &message, const std::string &details="", const json &data=json::object()) const
Definition logger.cpp:93
void trace(const std::string &origin, const std::string &message, const std::string &details="", const json &data=json::object()) const
Definition logger.cpp:70
MbLogDatabase & logsDb() const
Get the log database instance (for API access).
Definition logger.cpp:54
void initDb(const std::string &data_dir="") const
Initialize database connection.
Definition logger.cpp:58
#define MANTISBASE_API
Definition export.h:31
SQLite database manager for application logs.
std::string getFile(const std::string &path)
Definition logger.h:138
Definition api_keys.h:35
nlohmann::json json
Shorten JSON namespace.
Definition api_keys.h:36
MbLogLevel
Log severity levels mirrored in spdlog and the log database.
Definition logger.h:30
@ TRACE
Definition logger.h:31
@ WARN
Definition logger.h:34
@ INFO
Definition logger.h:33
@ CRITICAL
Definition logger.h:35
@ DEBUG
Definition logger.h:32
Core type aliases, handler signatures, and the MbAppMixin DI mixin.
Collection of utility functions that are re-used across different files.