MantisBase v0.4.9
Loading...
Searching...
No Matches
log_database.h
Go to the documentation of this file.
1
8
9#ifndef MB_LOG_DATABASE_H
10#define MB_LOG_DATABASE_H
11
12#include <string>
13#include <memory>
14#include <thread>
15#include <atomic>
16#include <condition_variable>
17#include <mutex>
18#include <nlohmann/json.hpp>
20
21namespace soci {
22 class session;
23}
24
25namespace mb {
26 class MantisBase;
27 using json = nlohmann::json;
28
36 public:
40 explicit MbLogDatabase(const MantisBase& app);
41
46
51 bool init(const std::string& data_dir = "");
52
62 bool insertLog(const std::string& level, const std::string& origin, const std::string& message,
63 const std::string& details, const json& data = json::object());
64
75 json getLogs(const std::string& after = "", int limit = 50,
76 const std::string& level_filter = "",
77 const std::string& search_filter = "",
78 const std::string& start_date = "",
79 const std::string& end_date = "");
80 private:
84 void shutdown();
85
89 void createTable() const;
90
94 void cleanupThread();
95
100 void deleteOldLogs(int days);
101
102 [[nodiscard]] int configuredLogRetentionDays() const;
103
104 static std::string buildMinLogWhereCondition(const std::string& level);
105
106 std::string m_dataDir;
107 std::unique_ptr<soci::session> m_session;
108 std::thread m_cleanupThread;
109 std::atomic<bool> m_running;
110 std::condition_variable m_cv;
111 std::mutex m_dbMutexLock;
112 const MantisBase& mApp;
113
114 inline static const std::vector<std::string> m_logLevels{ "critical", "warn", "info", "debug", "trace"};
115 };
116}
117
118#endif // MB_LOG_DATABASE_H
119
MantisBase entry point.
Definition mantisbase.h:54
MbLogDatabase(const MantisBase &app)
Construct MbLogDatabase instance.
Definition log_database.cpp:14
json getLogs(const std::string &after="", int limit=50, const std::string &level_filter="", const std::string &search_filter="", const std::string &start_date="", const std::string &end_date="")
Get logs with cursor-based pagination and filtering (ordered by id ASC).
Definition log_database.cpp:131
bool init(const std::string &data_dir="")
Initialize database: create table and start cleanup thread.
Definition log_database.cpp:24
bool insertLog(const std::string &level, const std::string &origin, const std::string &message, const std::string &details, const json &data=json::object())
Insert a log entry into the database.
Definition log_database.cpp:82
#define MANTISBASE_API
Definition export.h:31
Definition api_keys.h:35
nlohmann::json json
Shorten JSON namespace.
Definition api_keys.h:36
Definition log_database.h:21
Core type aliases, handler signatures, and the MbAppMixin DI mixin.