|
Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
|
RAII guard for automatic context management. More...
#include <scoped_context_guard.h>

Public Member Functions | |
| scoped_context_guard (logger &log) | |
| Construct guard and save current context. | |
| scoped_context_guard (logger &log, std::string_view key, context_value value, context_category category=context_category::custom) | |
| Construct guard and set a single context field. | |
| ~scoped_context_guard () | |
| Destructor - restores previous context. | |
| scoped_context_guard (const scoped_context_guard &)=delete | |
| scoped_context_guard & | operator= (const scoped_context_guard &)=delete |
| scoped_context_guard (scoped_context_guard &&other) noexcept | |
| scoped_context_guard & | operator= (scoped_context_guard &&other) noexcept |
| scoped_context_guard & | set (std::string_view key, context_value value, context_category category=context_category::custom) |
| Set a context value. | |
| scoped_context_guard & | set_trace (std::string_view trace_id, std::string_view span_id, std::optional< std::string_view > parent_span_id=std::nullopt) |
| Set trace context. | |
| scoped_context_guard & | set_request (std::string_view request_id, std::optional< std::string_view > correlation_id=std::nullopt) |
| Set request context. | |
| scoped_context_guard & | set_otel (const otlp::otel_context &ctx) |
| Set OpenTelemetry context. | |
Private Member Functions | |
| void | track_key (std::string_view key) |
| Track a key as added or overridden. | |
Private Attributes | |
| logger * | logger_ |
| unified_log_context | saved_context_ |
| std::vector< std::string > | tracked_keys_ |
RAII guard for automatic context management.
Provides exception-safe context management by saving the current context state on construction and restoring it on destruction. Supports chainable setters for convenient context configuration.
The guard tracks which keys were added during its lifetime and removes only those keys on destruction, while restoring any values that were overridden.
Thread Safety: Each instance is tied to a specific logger instance and should only be used from a single thread. However, multiple threads can use separate guards with the same logger safely.
Definition at line 101 of file scoped_context_guard.h.
|
explicit |
Construct guard and save current context.
| log | Logger whose context to manage |
Saves the current state of the logger's context. On destruction, the context will be restored to this state.
Definition at line 12 of file scoped_context_guard.cpp.
| kcenon::logger::scoped_context_guard::scoped_context_guard | ( | logger & | log, |
| std::string_view | key, | ||
| context_value | value, | ||
| context_category | category = context_category::custom ) |
Construct guard and set a single context field.
| log | Logger whose context to manage |
| key | Field name to set |
| value | Field value to set |
| category | Context category (default: custom) |
Convenience constructor for setting a single field. Equivalent to constructing with logger and then calling set().
Definition at line 17 of file scoped_context_guard.cpp.
References set().

| kcenon::logger::scoped_context_guard::~scoped_context_guard | ( | ) |
Destructor - restores previous context.
Removes all keys that were added by this guard and restores any values that were overridden. This ensures the logger's context is returned to its state before the guard was constructed.
Definition at line 25 of file scoped_context_guard.cpp.
References kcenon::logger::logger::context(), kcenon::logger::unified_log_context::get(), kcenon::logger::unified_log_context::get_category(), kcenon::logger::unified_log_context::keys(), logger_, saved_context_, and tracked_keys_.

|
delete |
|
noexcept |
Definition at line 48 of file scoped_context_guard.cpp.
|
delete |
|
noexcept |
Definition at line 55 of file scoped_context_guard.cpp.
| scoped_context_guard & kcenon::logger::scoped_context_guard::set | ( | std::string_view | key, |
| context_value | value, | ||
| context_category | category = context_category::custom ) |
Set a context value.
| key | Field name |
| value | Field value |
| category | Context category (default: custom) |
Sets a key-value pair in the logger's context. If the key already exists, its value is overridden and the previous value is saved for restoration.
Definition at line 81 of file scoped_context_guard.cpp.
References kcenon::logger::logger::context(), logger_, kcenon::logger::unified_log_context::set(), and track_key().
Referenced by scoped_context_guard().


| scoped_context_guard & kcenon::logger::scoped_context_guard::set_otel | ( | const otlp::otel_context & | ctx | ) |
Set OpenTelemetry context.
| ctx | OpenTelemetry context structure |
Imports all fields from an otel_context structure with context_category::otel.
Definition at line 119 of file scoped_context_guard.cpp.
References kcenon::logger::logger::context(), logger_, kcenon::logger::unified_log_context::set_otel(), kcenon::logger::otlp::otel_context::span_id, kcenon::logger::otlp::otel_context::trace_flags, kcenon::logger::otlp::otel_context::trace_id, kcenon::logger::otlp::otel_context::trace_state, and track_key().

| scoped_context_guard & kcenon::logger::scoped_context_guard::set_request | ( | std::string_view | request_id, |
| std::optional< std::string_view > | correlation_id = std::nullopt ) |
Set request context.
| request_id | Request identifier |
| correlation_id | Optional correlation identifier |
Convenience method for setting request tracking context. Sets request_id and optionally correlation_id with context_category::request.
Definition at line 106 of file scoped_context_guard.cpp.
References kcenon::logger::logger::context(), logger_, kcenon::logger::unified_log_context::set_request(), and track_key().

| scoped_context_guard & kcenon::logger::scoped_context_guard::set_trace | ( | std::string_view | trace_id, |
| std::string_view | span_id, | ||
| std::optional< std::string_view > | parent_span_id = std::nullopt ) |
Set trace context.
| trace_id | Trace identifier |
| span_id | Span identifier |
| parent_span_id | Optional parent span identifier |
Convenience method for setting distributed tracing context. Sets trace_id, span_id, and optionally parent_span_id with context_category::trace.
Definition at line 91 of file scoped_context_guard.cpp.
References kcenon::logger::logger::context(), logger_, kcenon::logger::unified_log_context::set_trace(), and track_key().

|
private |
Track a key as added or overridden.
| key | Key that was set |
Definition at line 139 of file scoped_context_guard.cpp.
References tracked_keys_.
Referenced by set(), set_otel(), set_request(), and set_trace().

|
private |
Definition at line 208 of file scoped_context_guard.h.
Referenced by set(), set_otel(), set_request(), set_trace(), and ~scoped_context_guard().
|
private |
Definition at line 209 of file scoped_context_guard.h.
Referenced by ~scoped_context_guard().
|
private |
Definition at line 210 of file scoped_context_guard.h.
Referenced by track_key(), and ~scoped_context_guard().