Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
kcenon::logger::scoped_context_guard Class Reference

RAII guard for automatic context management. More...

#include <scoped_context_guard.h>

Collaboration diagram for kcenon::logger::scoped_context_guard:
Collaboration graph

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_guardoperator= (const scoped_context_guard &)=delete
 
 scoped_context_guard (scoped_context_guard &&other) noexcept
 
scoped_context_guardoperator= (scoped_context_guard &&other) noexcept
 
scoped_context_guardset (std::string_view key, context_value value, context_category category=context_category::custom)
 Set a context value.
 
scoped_context_guardset_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_guardset_request (std::string_view request_id, std::optional< std::string_view > correlation_id=std::nullopt)
 Set request context.
 
scoped_context_guardset_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

loggerlogger_
 
unified_log_context saved_context_
 
std::vector< std::string > tracked_keys_
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ scoped_context_guard() [1/4]

kcenon::logger::scoped_context_guard::scoped_context_guard ( logger & log)
explicit

Construct guard and save current context.

Parameters
logLogger 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.

13 : logger_(&log), saved_context_(log.context()) {
14 // Save current context state for restoration
15}

◆ scoped_context_guard() [2/4]

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.

Parameters
logLogger whose context to manage
keyField name to set
valueField value to set
categoryContext 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.

22 set(key, value, category);
23}
scoped_context_guard & set(std::string_view key, context_value value, context_category category=context_category::custom)
Set a context value.
scoped_context_guard(logger &log)
Construct guard and save current context.

References set().

Here is the call graph for this function:

◆ ~scoped_context_guard()

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.

25 {
26 if (!logger_) {
27 return; // Moved-from state
28 }
29
30 // Get current context
31 auto& ctx = logger_->context();
32
33 // Remove all tracked keys from current context
34 for (const auto& key : tracked_keys_) {
35 ctx.remove(key);
36 }
37
38 // Restore values from saved context
39 for (const auto& key : saved_context_.keys()) {
40 auto value = saved_context_.get(key);
41 auto category = saved_context_.get_category(key);
42 if (value && category) {
43 ctx.set(key, *value, *category);
44 }
45 }
46}
unified_log_context & context()
Definition logger.cpp:769
std::vector< std::string > keys() const
Get all keys in the context.
std::optional< context_value > get(std::string_view key) const
Get a context value by key.
std::optional< context_category > get_category(std::string_view key) const
Get the category of a context entry.

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_.

Here is the call graph for this function:

◆ scoped_context_guard() [3/4]

kcenon::logger::scoped_context_guard::scoped_context_guard ( const scoped_context_guard & )
delete

◆ scoped_context_guard() [4/4]

kcenon::logger::scoped_context_guard::scoped_context_guard ( scoped_context_guard && other)
noexcept

Definition at line 48 of file scoped_context_guard.cpp.

49 : logger_(other.logger_),
50 saved_context_(std::move(other.saved_context_)),
51 tracked_keys_(std::move(other.tracked_keys_)) {
52 other.logger_ = nullptr; // Mark other as moved-from
53}

Member Function Documentation

◆ operator=() [1/2]

scoped_context_guard & kcenon::logger::scoped_context_guard::operator= ( const scoped_context_guard & )
delete

◆ operator=() [2/2]

scoped_context_guard & kcenon::logger::scoped_context_guard::operator= ( scoped_context_guard && other)
noexcept

Definition at line 55 of file scoped_context_guard.cpp.

55 {
56 if (this != &other) {
57 // First, restore our current state (like destructor)
58 if (logger_) {
59 auto& ctx = logger_->context();
60 for (const auto& key : tracked_keys_) {
61 ctx.remove(key);
62 }
63 for (const auto& key : saved_context_.keys()) {
64 auto value = saved_context_.get(key);
65 auto category = saved_context_.get_category(key);
66 if (value && category) {
67 ctx.set(key, *value, *category);
68 }
69 }
70 }
71
72 // Transfer ownership from other
73 logger_ = other.logger_;
74 saved_context_ = std::move(other.saved_context_);
75 tracked_keys_ = std::move(other.tracked_keys_);
76 other.logger_ = nullptr;
77 }
78 return *this;
79}

◆ set()

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.

Parameters
keyField name
valueField value
categoryContext category (default: custom)
Returns
Reference to this guard for chaining

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.

83 {
84 if (logger_) {
85 logger_->context().set(key, value, category);
86 track_key(key);
87 }
88 return *this;
89}
void track_key(std::string_view key)
Track a key as added or overridden.
unified_log_context & set(std::string_view key, context_value value, context_category category=context_category::custom)
Set a context value.

References kcenon::logger::logger::context(), logger_, kcenon::logger::unified_log_context::set(), and track_key().

Referenced by scoped_context_guard().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_otel()

scoped_context_guard & kcenon::logger::scoped_context_guard::set_otel ( const otlp::otel_context & ctx)

Set OpenTelemetry context.

Parameters
ctxOpenTelemetry context structure
Returns
Reference to this guard for chaining

Imports all fields from an otel_context structure with context_category::otel.

Definition at line 119 of file scoped_context_guard.cpp.

119 {
120 if (logger_) {
121 logger_->context().set_otel(ctx);
122 // Track all OTEL context keys
123 if (!ctx.trace_id.empty()) {
124 track_key("otel_trace_id");
125 }
126 if (!ctx.span_id.empty()) {
127 track_key("otel_span_id");
128 }
129 if (!ctx.trace_flags.empty()) {
130 track_key("otel_trace_flags");
131 }
132 if (!ctx.trace_state.empty()) {
133 track_key("otel_trace_state");
134 }
135 }
136 return *this;
137}
unified_log_context & set_otel(const otlp::otel_context &ctx)
Set OpenTelemetry context.

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().

Here is the call graph for this function:

◆ set_request()

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.

Parameters
request_idRequest identifier
correlation_idOptional correlation identifier
Returns
Reference to this guard for chaining

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.

108 {
109 if (logger_) {
110 logger_->context().set_request(request_id, correlation_id);
111 track_key("request_id");
112 if (correlation_id) {
113 track_key("correlation_id");
114 }
115 }
116 return *this;
117}
unified_log_context & set_request(std::string_view request_id, std::optional< std::string_view > correlation_id=std::nullopt)
Set request context.

References kcenon::logger::logger::context(), logger_, kcenon::logger::unified_log_context::set_request(), and track_key().

Here is the call graph for this function:

◆ set_trace()

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.

Parameters
trace_idTrace identifier
span_idSpan identifier
parent_span_idOptional parent span identifier
Returns
Reference to this guard for chaining

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.

94 {
95 if (logger_) {
96 logger_->context().set_trace(trace_id, span_id, parent_span_id);
97 track_key("trace_id");
98 track_key("span_id");
99 if (parent_span_id) {
100 track_key("parent_span_id");
101 }
102 }
103 return *this;
104}
unified_log_context & 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.

References kcenon::logger::logger::context(), logger_, kcenon::logger::unified_log_context::set_trace(), and track_key().

Here is the call graph for this function:

◆ track_key()

void kcenon::logger::scoped_context_guard::track_key ( std::string_view key)
private

Track a key as added or overridden.

Parameters
keyKey that was set

Definition at line 139 of file scoped_context_guard.cpp.

139 {
140 std::string key_str(key);
141 // Only add if not already tracked (avoid duplicates)
142 if (std::find(tracked_keys_.begin(), tracked_keys_.end(), key_str) == tracked_keys_.end()) {
143 tracked_keys_.push_back(std::move(key_str));
144 }
145}

References tracked_keys_.

Referenced by set(), set_otel(), set_request(), and set_trace().

Here is the caller graph for this function:

Member Data Documentation

◆ logger_

logger* kcenon::logger::scoped_context_guard::logger_
private

Definition at line 208 of file scoped_context_guard.h.

Referenced by set(), set_otel(), set_request(), set_trace(), and ~scoped_context_guard().

◆ saved_context_

unified_log_context kcenon::logger::scoped_context_guard::saved_context_
private

Definition at line 209 of file scoped_context_guard.h.

Referenced by ~scoped_context_guard().

◆ tracked_keys_

std::vector<std::string> kcenon::logger::scoped_context_guard::tracked_keys_
private

Definition at line 210 of file scoped_context_guard.h.

Referenced by track_key(), and ~scoped_context_guard().


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