21std::string make_db_path() {
22 std::ostringstream os;
27void cleanup_db(
const std::string& path) {
28 std::remove(path.c_str());
29 std::remove((path +
"-lck").c_str());
35 const std::string db_path = make_db_path();
38 std::cout <<
"MdbxLogger example starting, db=" << db_path << std::endl;
44 mdbx_config.
path = db_path;
45 mdbx_config.
app_name =
"example-mdbx";
46 mdbx_config.
async =
true;
53 mdbx_config.
on_error = [](
const std::string& msg) {
54 std::cerr <<
"[MdbxLogger error callback] " << msg << std::endl;
86 for (
int i = 0; i < 50; ++i) {
87 big +=
"chunk-" + std::to_string(i) +
" ";
93 LOGIT_FATAL(
"Critical: unable to recover transaction state");
103 auto session_opt = mdbx->read_session(mdbx->session_id());
105 std::cout <<
"\n--- Session ---" << std::endl;
106 std::cout <<
" app_name: " << session_opt->app_name << std::endl;
107 std::cout <<
" process_id: " << session_opt->process_id << std::endl;
108 std::cout <<
" started_ms: " << session_opt->start_time_ms << std::endl;
109 std::cout <<
" schema_ver: " << session_opt->schema_version << std::endl;
117 now_ms - 24LL * 60 * 60 * 1000,
121 std::cout <<
"\n--- All records (" << all_records.size() <<
") ---" << std::endl;
122 for (
const auto& r : all_records) {
124 << r.timestamp_ms <<
" seq=" << r.sequence
125 <<
" msg=\"" << r.message <<
"\"";
126 if (r.payload_id != 0) {
127 std::cout <<
" [payload_id=" << r.payload_id <<
"]";
129 std::cout << std::endl;
133 std::cout <<
"\n--- Records with level >= WARN ---" << std::endl;
134 for (
const auto& r : all_records) {
137 << r.timestamp_ms <<
" " << r.message << std::endl;
142 std::cout <<
"\n--- Spilled payloads ---" << std::endl;
143 for (
const auto& r : all_records) {
144 if (r.payload_id != 0) {
145 auto data_opt = mdbx->read_payload_data(r.payload_id);
147 std::cout <<
" payload_id=" << r.payload_id
148 <<
" size=" << data_opt->size()
149 <<
" preview=\"" << r.message <<
"\""
152 std::cout <<
" payload_id=" << r.payload_id <<
" FAILED to read/decompress"
160 auto now_local = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
161 std::tm* tm_now = std::localtime(&now_local);
162 std::tm tm_midnight = *tm_now;
163 tm_midnight.tm_hour = 0;
164 tm_midnight.tm_min = 0;
165 tm_midnight.tm_sec = 0;
166 std::time_t midnight_t = std::mktime(&tm_midnight);
168 auto today_ms = std::chrono::milliseconds(
static_cast<int64_t
>(midnight_t) * 1000);
169 auto tomorrow_ms = today_ms + std::chrono::hours(24);
177 std::cout <<
"\n--- Records for today (" << day_records.size() <<
") ---" << std::endl;
178 for (
const auto& r : day_records) {
180 << r.timestamp_ms <<
" " << r.message << std::endl;
182 }
catch (
const std::exception& e) {
183 std::cerr <<
"Date query example skipped: " << e.what() << std::endl;
188 std::cout <<
"\n--- LOGIT_READ_RECENT_ASC(100) (" << recent.size() <<
") ---" << std::endl;
189 for (
const auto& r : recent) {
191 << r.timestamp_ms <<
" " << r.message << std::endl;
196 std::vector<logit::LogRecordSnapshot> live_updates;
200 live_updates.push_back(v);
207 std::cout <<
"\n--- Live updates received (" << live_updates.size()
208 <<
") ---" << std::endl;
209 for (
const auto& r : live_updates) {
211 << r.message << std::endl;
215 std::cout <<
"Callback removed" << std::endl;
220 std::cout <<
"Live updates after unsubscribe: " << live_updates.size()
224 std::cout <<
"\n--- Statistics ---" << std::endl;
225 std::cout <<
" dropped: " << mdbx->dropped_count() << std::endl;
226 std::cout <<
" failed_writes: " << mdbx->failed_export_count() << std::endl;
228 std::cerr <<
"MDBX logger not found at index " << mdbx_index << std::endl;
241 auto session_opt = mdbx->read_session(mdbx->session_id());
243 std::cout <<
" session end_ms: " << session_opt->end_time_ms << std::endl;
248 std::cout <<
"\nMdbxLogger example completed." << std::endl;
257 std::cout <<
"MdbxLogger example requires LOGIT_WITH_MDBX=ON." << std::endl;
static Logger & get_instance()
Retrieves singleton instance of Logger.
std::size_t logger_count() const
Returns the number of registered logger strategies.
Stores formatted logs in MDBX tables with optional async batching.
#define LOGIT_CURRENT_TIMESTAMP_MS()
Macro to get the current timestamp in milliseconds.
#define LOGIT_ADD_CONSOLE_DEFAULT()
Macro for adding the default console logger.
#define LOGIT_WAIT()
Macro for waiting for all asynchronous loggers to finish processing.
#define LOGIT_WITH_LOGGER_AS(logger_index, logger_type, var_name)
Executes a code block when the logger backend has the requested type.
#define LOGIT_SHUTDOWN()
Macro for shutting down logger system.
#define LOGIT_ADD_LOG_CALLBACK(logger_index, callback)
Registers a callback to receive newly written log records.
#define LOGIT_READ_RECENT_ASC(logger_index, limit, period_ms)
Reads recent records in ascending order (oldest first).
#define LOGIT_REMOVE_LOG_CALLBACK(logger_index, callback_id)
Unregisters a previously added log callback.
#define LOGIT_SCOPE_INFO(phase)
#define LOGIT_ADD_LOGGER(logger_type, logger_args, formatter_type, formatter_args)
Macro for adding a logger with a specific formatter.
#define LOGIT_READ_RANGE(logger_index, from_ms, to_ms, limit)
Reads a time-range of records from a backend that supports ILogReader.
Unified umbrella header for the LogIt++ library.
std::string to_string(LogLevel level, int mode=0)
Convert LogLevel to a std::string representation.
@ LOG_LVL_WARN
Warning level logging.
std::string get_exec_dir()
Retrieves the directory of the executable file.
Common owning snapshot of a stored log record.
Configuration for the MDBX logger backend.
bool store_large_payloads_separately
Store large messages in log_payloads.
std::size_t max_batch_size
Maximum records per write transaction.
std::size_t max_queue_size
Maximum async queue size; 0 means unlimited.
std::function< void(const std::string &)> on_error
Optional callback invoked on initialization and write errors instead of stderr.
std::string path
MDBX environment path.
bool async
Queue records and write them from a worker thread.
std::size_t large_payload_threshold
Spill messages larger than this.
std::string app_name
Optional application name stored in session metadata.