Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
scoped_context_guard.cpp
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
7
8#include <algorithm>
9
10namespace kcenon::logger {
11
13 : logger_(&log), saved_context_(log.context()) {
14 // Save current context state for restoration
15}
16
18 std::string_view key,
19 context_value value,
20 context_category category)
22 set(key, value, category);
23}
24
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}
47
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}
54
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}
80
82 context_value value,
83 context_category category) {
84 if (logger_) {
85 logger_->context().set(key, value, category);
86 track_key(key);
87 }
88 return *this;
89}
90
92 std::string_view trace_id,
93 std::string_view span_id,
94 std::optional<std::string_view> parent_span_id) {
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}
105
107 std::string_view request_id,
108 std::optional<std::string_view> correlation_id) {
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}
118
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}
138
139void scoped_context_guard::track_key(std::string_view key) {
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}
146
147} // namespace kcenon::logger
unified_log_context & context()
Definition logger.cpp:769
RAII guard for automatic context management.
scoped_context_guard & operator=(const scoped_context_guard &)=delete
scoped_context_guard & set(std::string_view key, context_value value, context_category category=context_category::custom)
Set a context value.
~scoped_context_guard()
Destructor - restores previous 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_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_otel(const otlp::otel_context &ctx)
Set OpenTelemetry context.
scoped_context_guard(logger &log)
Construct guard and save current context.
void track_key(std::string_view key)
Track a key as added or overridden.
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.
std::vector< std::string > keys() const
Get all keys in the context.
unified_log_context & set_otel(const otlp::otel_context &ctx)
Set OpenTelemetry context.
unified_log_context & set_request(std::string_view request_id, std::optional< std::string_view > correlation_id=std::nullopt)
Set request 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.
unified_log_context & set(std::string_view key, context_value value, context_category category=context_category::custom)
Set a context value.
High-performance, thread-safe logging system with asynchronous capabilities.
std::variant< std::monostate, bool, int64_t, double, std::string > context_value
Value type for unified context storage.
context_category
Categories for organizing context entries.
RAII guard for automatic context restoration.
OpenTelemetry context for trace correlation.