2#ifndef LOGIT_CPP_HEADER_LOGIT_CPP_LOGIT_LOGGERS_MEMORYLOGGER_HPP_INCLUDED
3#define LOGIT_CPP_HEADER_LOGIT_CPP_LOGIT_LOGGERS_MEMORYLOGGER_HPP_INCLUDED
46 MemoryLogger(std::size_t max_records, std::size_t max_bytes, int64_t max_age_ms) :
47 m_config{max_records, max_bytes, max_age_ms} {}
50 void log(
const LogRecord& record,
const std::string& message)
override {
66 std::lock_guard<std::mutex> lock(
m_mutex);
124 m_log_level.store(
static_cast<int>(level), std::memory_order_relaxed);
135 std::lock_guard<std::mutex> lock(
m_mutex);
138 std::vector<std::string> snapshot;
141 snapshot.push_back(entry.message);
149 std::lock_guard<std::mutex> lock(
m_mutex);
172 std::lock_guard<std::mutex> lock(
m_mutex);
189 std::size_t limit = 0)
const override {
190#if __cplusplus >= 201703L
191 auto result = read_range_result(from_ms, to_ms, limit);
192 return result.value ? *result.value : std::vector<LogRecordSnapshot>();
194 std::vector<LogRecordSnapshot> out;
195 if (to_ms <= from_ms) {
199 std::lock_guard<std::mutex> lock(
m_mutex);
203 if (entry.timestamp_ms >= from_ms && entry.timestamp_ms < to_ms) {
205 if (limit > 0 && out.size() >= limit) {
214#if __cplusplus >= 201703L
216 LogReadResult<std::vector<LogRecordSnapshot>> read_range_result(
219 std::size_t limit = 0)
const override {
220 LogReadResult<std::vector<LogRecordSnapshot>> result;
221 result.value.emplace();
222 if (to_ms <= from_ms) {
226 std::lock_guard<std::mutex> lock(
m_mutex);
230 if (entry.timestamp_ms >= from_ms && entry.timestamp_ms < to_ms) {
232 if (limit > 0 && result.value->size() >= limit) {
244 int64_t period_ms = 0,
246#if __cplusplus >= 201703L
247 auto result = read_recent_result(limit, period_ms, order);
248 return result.value ? *result.value : std::vector<LogRecordSnapshot>();
250 std::vector<LogRecordSnapshot> out;
251 std::lock_guard<std::mutex> lock(
m_mutex);
255 const int64_t from_ms = (period_ms > 0) ? (now_ms - period_ms) : 0;
258 if (period_ms <= 0 || it->timestamp_ms >= from_ms) {
260 if (limit > 0 && out.size() >= limit) {
267 std::reverse(out.begin(), out.end());
273#if __cplusplus >= 201703L
275 LogReadResult<std::vector<LogRecordSnapshot>> read_recent_result(
277 int64_t period_ms = 0,
279 LogReadResult<std::vector<LogRecordSnapshot>> result;
280 result.value.emplace();
281 std::lock_guard<std::mutex> lock(
m_mutex);
285 const int64_t from_ms = (period_ms > 0) ? (now_ms - period_ms) : 0;
288 if (period_ms <= 0 || it->timestamp_ms >= from_ms) {
290 if (limit > 0 && result.value->size() >= limit) {
297 std::reverse(result.value->begin(), result.value->end());
316 std::vector<Callback> callbacks_copy;
321 callbacks_copy.push_back(kv.second);
324 for (
const auto& cb : callbacks_copy) {
383 return now > last ? (now - last) : 0;
Optional read-only interface for log backends that support querying stored records.
Optional live-subscription interface for log backends that can push newly written records.
Defines the interface for loggers used in the logging system.
Optional interface for backends that expose stored records.
Optional interface for backends that can notify callers when a record is successfully written.
std::function< void(const LogRecordSnapshot &)> Callback
Optional buffered snapshot access returns empty results by default.
Stores the latest formatted log messages in memory.
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::atomic< int > m_log_level
MemoryLogger(std::size_t max_records, std::size_t max_bytes, int64_t max_age_ms)
Construct with explicit count/size/age limits.
void set_log_level(LogLevel level) override
Set the minimum accepted log level.
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.
std::atomic< int64_t > m_last_log_ts
double get_float_param(const LoggerParam ¶m) const override
Retrieve legacy floating-point metadata.
std::atomic< int64_t > m_last_log_mono_ts
std::atomic< uint64_t > m_next_callback_id
void log(const LogRecord &record, const std::string &message) override
Store a formatted message in the buffer and evict old entries.
static std::size_t m_entry_bytes(const BufferedLogEntry &entry)
void notify_callbacks(const LogRecordSnapshot &snapshot) const
std::map< uint64_t, Callback > m_callbacks
std::deque< BufferedLogEntry > m_entries
void m_pop_front_locked() const
std::size_t m_total_bytes
static LogRecordSnapshot to_snapshot(const BufferedLogEntry &e)
std::vector< std::string > get_buffered_strings() const override
Return buffered formatted strings in chronological order.
std::string get_string_param(const LoggerParam ¶m) const override
Retrieve legacy string-based metadata.
std::vector< BufferedLogEntry > get_buffered_entries() const override
Return buffered structured entries in chronological order.
LogLevel get_log_level() const override
Get the minimum accepted log level.
void m_evict_expired_locked(int64_t now_ms) const
LogClearResult clear_logs(const LogClearOptions &options=LogClearOptions()) override
Clears buffered entries.
int64_t get_int_param(const LoggerParam ¶m) const override
Retrieve legacy integer-based metadata.
int64_t get_time_since_last_log() const
MemoryLogger()=default
Construct with default retention settings.
int64_t get_last_log_ts() const
bool remove_log_callback(uint64_t callback_id) override
Unregisters a previously added callback.
void m_enforce_limits_locked(int64_t now_ms) const
uint64_t add_log_callback(Callback callback) override
Registers a callback to be invoked after each successfully written record.
void wait() override
Memory logger is synchronous, so no flush step is needed.
MemoryLogger(const Config &config)
Construct with explicit configuration.
std::mutex m_callbacks_mutex
#define LOGIT_CURRENT_TIMESTAMP_MS()
Macro to get the current timestamp in milliseconds.
#define LOGIT_MONOTONIC_MS()
The primary namespace for the LogIt++ library.
@ LOG_LVL_TRACE
Trace level logging.
LoggerParam
Enumeration for different logger parameters that can be retrieved.
@ TimeSinceLastLog
The time elapsed since the last log in seconds.
@ LastLogTimestamp
The timestamp of the last log.
LogReadOrder
Ordering for query results returned by ILogReader.
Structured representation of a buffered log snapshot entry.
LogLevel level
Severity of the log entry.
std::string file
Source file path or leaf name.
int64_t timestamp_ms
Wall-clock timestamp in milliseconds.
int line
Source line number.
std::string function
Function name captured at the call site.
std::string message
Formatted message text stored in the buffer.
Selects which data categories a logger cleanup should remove.
Result returned by logger cleanup operations.
std::string message
Optional diagnostic or unsupported reason.
std::size_t cleared_records
Number of primary records/files cleared when available.
LogClearStatus status
Detailed cleanup status.
bool ok
True when the backend completed cleanup.
Common owning snapshot of a stored log record.
std::string message
Formatted message or payload preview.
int64_t timestamp_ms
Log timestamp in milliseconds.
int line
Source line number.
std::string file
Source file path.
std::string function
Source function name.
LogLevel level
Log severity level.
Stores log metadata and content.
const std::string function
Function name.
const int line
Line number in the source file.
const LogLevel log_level
Log level (severity).
const bool raw_mode
Flag indicating if formatter and level filters should be bypassed.
const int64_t timestamp_ms
Timestamp in milliseconds.
const std::string file
Source file name.
Retention limits for the in-memory buffer.
std::size_t max_bytes
Maximum buffered formatted-message bytes (0 = unlimited).
int64_t max_age_ms
Maximum age of buffered entries (0 = unlimited).
std::size_t max_records
Maximum number of buffered entries (0 = unlimited).
Config(std::size_t max_records, std::size_t max_bytes, int64_t max_age_ms)