LogIt++
Header-only, macro-first C++ logging library
Loading...
Searching...
No Matches
logit::MemoryLogger Class Reference

Stores the latest formatted log messages in memory. More...

#include <MemoryLogger.hpp>

Inheritance diagram for logit::MemoryLogger:
logit::ILogger logit::ILogReader logit::ILogSubscriber

Classes

struct  Config
 Retention limits for the in-memory buffer. More...
 

Public Member Functions

 MemoryLogger ()=default
 Construct with default retention settings.
 
 MemoryLogger (const Config &config)
 Construct with explicit configuration.
 
 MemoryLogger (std::size_t max_records, std::size_t max_bytes, int64_t max_age_ms)
 Construct with explicit count/size/age limits.
 
void log (const LogRecord &record, const std::string &message) override
 Store a formatted message in the buffer and evict old entries.
 
std::string get_string_param (const LoggerParam &param) const override
 Retrieve legacy string-based metadata.
 
int64_t get_int_param (const LoggerParam &param) const override
 Retrieve legacy integer-based metadata.
 
double get_float_param (const LoggerParam &param) const override
 Retrieve legacy floating-point metadata.
 
void set_log_level (LogLevel level) override
 Set the minimum accepted log level.
 
LogLevel get_log_level () const override
 Get the minimum accepted log level.
 
std::vector< std::string > get_buffered_strings () const override
 Return buffered formatted strings in chronological order.
 
std::vector< BufferedLogEntry > get_buffered_entries () const override
 Return buffered structured entries in chronological order.
 
void wait () override
 Memory logger is synchronous, so no flush step is needed.
 
uint64_t add_log_callback (Callback callback) override
 Registers a callback to be invoked after each successfully written record.
 
bool remove_log_callback (uint64_t callback_id) override
 Unregisters a previously added callback.
 
LogClearResult clear_logs (const LogClearOptions &options=LogClearOptions()) override
 Clears buffered entries.
 
std::vector< LogRecordSnapshot > read_range (int64_t from_ms, int64_t to_ms, std::size_t limit=0) const override
 Reads records in [from_ms, to_ms) ordered by timestamp.
 
std::vector< LogRecordSnapshot > read_recent (std::size_t limit, int64_t period_ms=0, LogReadOrder order=LogReadOrder::Ascending) const override
 Reads the most recent records.
 
- Public Member Functions inherited from logit::ILogger
virtual ~ILogger ()=default
 
virtual std::vector< LogFileInfo > list_log_files () const
 Lists persisted log files owned by this backend.
 
virtual LogFileReadResult read_log_file (const std::string &path) const
 Reads one persisted log file owned by this backend.
 
virtual std::vector< LogFileReadResult > read_log_files (const std::vector< std::string > &paths) const
 Reads several persisted log files owned by this backend.
 
virtual void shutdown ()
 Stops logger-owned asynchronous resources after draining pending work.
 
- Public Member Functions inherited from logit::ILogReader
virtual ~ILogReader ()=default
 
- Public Member Functions inherited from logit::ILogSubscriber
virtual ~ILogSubscriber ()=default
 

Private Member Functions

void notify_callbacks (const LogRecordSnapshot &snapshot) const
 
void m_pop_front_locked () const
 
void m_evict_expired_locked (int64_t now_ms) const
 
void m_enforce_limits_locked (int64_t now_ms) const
 
int64_t get_last_log_ts () const
 
int64_t get_time_since_last_log () const
 

Static Private Member Functions

static LogRecordSnapshot to_snapshot (const BufferedLogEntry &e)
 
static std::size_t m_entry_bytes (const BufferedLogEntry &entry)
 

Private Attributes

Config m_config
 
std::mutex m_mutex
 
std::deque< BufferedLogEntry > m_entries
 
std::size_t m_total_bytes = 0
 
std::atomic< int64_t > m_last_log_ts = ATOMIC_VAR_INIT(0)
 
std::atomic< int64_t > m_last_log_mono_ts = ATOMIC_VAR_INIT(0)
 
std::atomic< int > m_log_level = ATOMIC_VAR_INIT(static_cast<int>(LogLevel::LOG_LVL_TRACE))
 
std::mutex m_callbacks_mutex
 
std::map< uint64_t, Callback > m_callbacks
 
std::atomic< uint64_t > m_next_callback_id {1}
 

Additional Inherited Members

- Public Types inherited from logit::ILogSubscriber
using Callback = std::function<void(const LogRecordSnapshot&)>
 

Detailed Description

Stores the latest formatted log messages in memory.

Snapshots are returned oldest-to-newest. Read operations avoid Logger-level execution serialization, but still synchronize on this backend's own mutex while copying the current buffer.

Definition at line 22 of file MemoryLogger.hpp.

Constructor & Destructor Documentation

◆ MemoryLogger() [1/3]

logit::MemoryLogger::MemoryLogger ( )
default

Construct with default retention settings.

◆ MemoryLogger() [2/3]

logit::MemoryLogger::MemoryLogger ( const Config & config)
inlineexplicit

Construct with explicit configuration.

Definition at line 43 of file MemoryLogger.hpp.

◆ MemoryLogger() [3/3]

logit::MemoryLogger::MemoryLogger ( std::size_t max_records,
std::size_t max_bytes,
int64_t max_age_ms )
inline

Construct with explicit count/size/age limits.

Definition at line 46 of file MemoryLogger.hpp.

Member Function Documentation

◆ add_log_callback()

uint64_t logit::MemoryLogger::add_log_callback ( Callback callback)
inlineoverridevirtual

Registers a callback to be invoked after each successfully written record.

Parameters
callbackFunction called with the written LogRecordSnapshot.
Returns
Stable callback id that can be passed to remove_log_callback.

Implements logit::ILogSubscriber.

Definition at line 157 of file MemoryLogger.hpp.

◆ clear_logs()

LogClearResult logit::MemoryLogger::clear_logs ( const LogClearOptions & options = LogClearOptions())
inlineoverridevirtual

Clears buffered entries.

Implements logit::ILogger.

Definition at line 170 of file MemoryLogger.hpp.

◆ get_buffered_entries()

std::vector< BufferedLogEntry > logit::MemoryLogger::get_buffered_entries ( ) const
inlineoverridevirtual

Return buffered structured entries in chronological order.

Age-based cleanup runs before copying the snapshot.

Implements logit::ILogger.

Definition at line 148 of file MemoryLogger.hpp.

◆ get_buffered_strings()

std::vector< std::string > logit::MemoryLogger::get_buffered_strings ( ) const
inlineoverridevirtual

Return buffered formatted strings in chronological order.

Age-based cleanup runs before copying the snapshot.

Implements logit::ILogger.

Definition at line 134 of file MemoryLogger.hpp.

◆ get_float_param()

double logit::MemoryLogger::get_float_param ( const LoggerParam & param) const
inlineoverridevirtual

Retrieve legacy floating-point metadata.

Implements logit::ILogger.

Definition at line 110 of file MemoryLogger.hpp.

◆ get_int_param()

int64_t logit::MemoryLogger::get_int_param ( const LoggerParam & param) const
inlineoverridevirtual

Retrieve legacy integer-based metadata.

Implements logit::ILogger.

Definition at line 97 of file MemoryLogger.hpp.

◆ get_last_log_ts()

int64_t logit::MemoryLogger::get_last_log_ts ( ) const
inlineprivate

Definition at line 373 of file MemoryLogger.hpp.

◆ get_log_level()

LogLevel logit::MemoryLogger::get_log_level ( ) const
inlineoverridevirtual

Get the minimum accepted log level.

Implements logit::ILogger.

Definition at line 128 of file MemoryLogger.hpp.

◆ get_string_param()

std::string logit::MemoryLogger::get_string_param ( const LoggerParam & param) const
inlineoverridevirtual

Retrieve legacy string-based metadata.

Implements logit::ILogger.

Definition at line 84 of file MemoryLogger.hpp.

◆ get_time_since_last_log()

int64_t logit::MemoryLogger::get_time_since_last_log ( ) const
inlineprivate

Definition at line 377 of file MemoryLogger.hpp.

◆ log()

void logit::MemoryLogger::log ( const LogRecord & record,
const std::string & message )
inlineoverridevirtual

Store a formatted message in the buffer and evict old entries.

Implements logit::ILogger.

Definition at line 50 of file MemoryLogger.hpp.

◆ m_enforce_limits_locked()

void logit::MemoryLogger::m_enforce_limits_locked ( int64_t now_ms) const
inlineprivate

Definition at line 356 of file MemoryLogger.hpp.

◆ m_entry_bytes()

static std::size_t logit::MemoryLogger::m_entry_bytes ( const BufferedLogEntry & entry)
inlinestaticprivate

Definition at line 333 of file MemoryLogger.hpp.

◆ m_evict_expired_locked()

void logit::MemoryLogger::m_evict_expired_locked ( int64_t now_ms) const
inlineprivate

Definition at line 345 of file MemoryLogger.hpp.

◆ m_pop_front_locked()

void logit::MemoryLogger::m_pop_front_locked ( ) const
inlineprivate

Definition at line 337 of file MemoryLogger.hpp.

◆ notify_callbacks()

void logit::MemoryLogger::notify_callbacks ( const LogRecordSnapshot & snapshot) const
inlineprivate

Definition at line 315 of file MemoryLogger.hpp.

◆ read_range()

std::vector< LogRecordSnapshot > logit::MemoryLogger::read_range ( int64_t from_ms,
int64_t to_ms,
std::size_t limit = 0 ) const
inlineoverridevirtual

Reads records in [from_ms, to_ms) ordered by timestamp.

Implements logit::ILogReader.

Definition at line 186 of file MemoryLogger.hpp.

◆ read_recent()

std::vector< LogRecordSnapshot > logit::MemoryLogger::read_recent ( std::size_t limit,
int64_t period_ms = 0,
LogReadOrder order = LogReadOrder::Ascending ) const
inlineoverridevirtual

Reads the most recent records.

Implements logit::ILogReader.

Definition at line 242 of file MemoryLogger.hpp.

◆ remove_log_callback()

bool logit::MemoryLogger::remove_log_callback ( uint64_t callback_id)
inlineoverridevirtual

Unregisters a previously added callback.

Removal prevents future dispatch snapshots from including the callback, but cannot cancel an invocation that has already been copied for dispatch.

Parameters
callback_idId returned by add_log_callback.
Returns
True if the callback existed and was removed.

Implements logit::ILogSubscriber.

Definition at line 164 of file MemoryLogger.hpp.

◆ set_log_level()

void logit::MemoryLogger::set_log_level ( LogLevel level)
inlineoverridevirtual

Set the minimum accepted log level.

Implements logit::ILogger.

Definition at line 123 of file MemoryLogger.hpp.

◆ to_snapshot()

static LogRecordSnapshot logit::MemoryLogger::to_snapshot ( const BufferedLogEntry & e)
inlinestaticprivate

Definition at line 304 of file MemoryLogger.hpp.

◆ wait()

void logit::MemoryLogger::wait ( )
inlineoverridevirtual

Memory logger is synchronous, so no flush step is needed.

Implements logit::ILogger.

Definition at line 155 of file MemoryLogger.hpp.

Member Data Documentation

◆ m_callbacks

std::map<uint64_t, Callback> logit::MemoryLogger::m_callbacks
private

Definition at line 394 of file MemoryLogger.hpp.

◆ m_callbacks_mutex

std::mutex logit::MemoryLogger::m_callbacks_mutex
mutableprivate

Definition at line 393 of file MemoryLogger.hpp.

◆ m_config

Config logit::MemoryLogger::m_config
private

Definition at line 386 of file MemoryLogger.hpp.

◆ m_entries

std::deque<BufferedLogEntry> logit::MemoryLogger::m_entries
mutableprivate

Definition at line 388 of file MemoryLogger.hpp.

◆ m_last_log_mono_ts

std::atomic<int64_t> logit::MemoryLogger::m_last_log_mono_ts = ATOMIC_VAR_INIT(0)
private

Definition at line 391 of file MemoryLogger.hpp.

◆ m_last_log_ts

std::atomic<int64_t> logit::MemoryLogger::m_last_log_ts = ATOMIC_VAR_INIT(0)
private

Definition at line 390 of file MemoryLogger.hpp.

◆ m_log_level

std::atomic<int> logit::MemoryLogger::m_log_level = ATOMIC_VAR_INIT(static_cast<int>(LogLevel::LOG_LVL_TRACE))
private

Definition at line 392 of file MemoryLogger.hpp.

◆ m_mutex

std::mutex logit::MemoryLogger::m_mutex
mutableprivate

Definition at line 387 of file MemoryLogger.hpp.

◆ m_next_callback_id

std::atomic<uint64_t> logit::MemoryLogger::m_next_callback_id {1}
private

Definition at line 395 of file MemoryLogger.hpp.

◆ m_total_bytes

std::size_t logit::MemoryLogger::m_total_bytes = 0
mutableprivate

Definition at line 389 of file MemoryLogger.hpp.


The documentation for this class was generated from the following file: