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

Unified interface for managing all types of logging context. More...

#include <unified_log_context.h>

Collaboration diagram for kcenon::logger::unified_log_context:
Collaboration graph

Classes

struct  entry
 Internal entry structure. More...
 

Public Member Functions

 unified_log_context ()=default
 Default constructor.
 
 unified_log_context (const unified_log_context &other)
 Copy constructor.
 
 unified_log_context (unified_log_context &&other) noexcept
 Move constructor.
 
unified_log_contextoperator= (const unified_log_context &other)
 Copy assignment operator.
 
unified_log_contextoperator= (unified_log_context &&other) noexcept
 Move assignment operator.
 
 ~unified_log_context ()=default
 Destructor.
 
unified_log_contextset (std::string_view key, context_value value, context_category category=context_category::custom)
 Set a context value.
 
unified_log_contextset_trace (std::string_view trace_id, std::string_view span_id, std::optional< std::string_view > parent_span_id=std::nullopt)
 Set trace context.
 
unified_log_contextset_request (std::string_view request_id, std::optional< std::string_view > correlation_id=std::nullopt)
 Set request context.
 
unified_log_contextset_otel (const otlp::otel_context &ctx)
 Set OpenTelemetry context.
 
std::optional< context_valueget (std::string_view key) const
 Get a context value by key.
 
template<typename T >
std::optional< T > get_as (std::string_view key) const
 
std::string get_string (std::string_view key, std::string_view default_value="") const
 Get a context value as string.
 
std::optional< context_categoryget_category (std::string_view key) const
 Get the category of a context entry.
 
bool has (std::string_view key) const
 Check if a key exists in the context.
 
bool empty () const
 Check if the context is empty.
 
size_t size () const
 Get the number of entries.
 
std::vector< std::string > keys () const
 Get all keys in the context.
 
std::vector< std::string > keys (context_category category) const
 Get keys for a specific category.
 
void remove (std::string_view key)
 Remove a specific key from the context.
 
void clear ()
 Clear all entries from the context.
 
void clear (context_category category)
 Clear entries of a specific category.
 
log_fields to_fields () const
 Export context to log_fields format.
 
unified_log_contextmerge (const unified_log_context &other, bool overwrite=true)
 Merge another context into this one.
 

Private Attributes

std::shared_mutex mutex_
 
std::unordered_map< std::string, entrydata_
 

Detailed Description

Unified interface for managing all types of logging context.

This class provides a single point of access for all logging context operations. It consolidates what was previously spread across multiple storage mechanisms into one coherent interface.

Thread Safety: All methods are thread-safe. Read operations use shared locks, write operations use exclusive locks.

Definition at line 117 of file unified_log_context.h.

Constructor & Destructor Documentation

◆ unified_log_context() [1/3]

kcenon::logger::unified_log_context::unified_log_context ( )
default

Default constructor.

◆ unified_log_context() [2/3]

kcenon::logger::unified_log_context::unified_log_context ( const unified_log_context & other)

Copy constructor.

Parameters
otherContext to copy from

Definition at line 12 of file unified_log_context.cpp.

12 {
13 std::shared_lock lock(other.mutex_);
14 data_ = other.data_;
15}
std::unordered_map< std::string, entry > data_

References data_, and mutex_.

◆ unified_log_context() [3/3]

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

Move constructor.

Parameters
otherContext to move from

Definition at line 17 of file unified_log_context.cpp.

17 {
18 std::unique_lock lock(other.mutex_);
19 data_ = std::move(other.data_);
20}

◆ ~unified_log_context()

kcenon::logger::unified_log_context::~unified_log_context ( )
default

Destructor.

Member Function Documentation

◆ clear() [1/2]

void kcenon::logger::unified_log_context::clear ( )

Clear all entries from the context.

Definition at line 174 of file unified_log_context.cpp.

174 {
175 std::unique_lock lock(mutex_);
176 data_.clear();
177}

References data_, and mutex_.

◆ clear() [2/2]

void kcenon::logger::unified_log_context::clear ( context_category category)

Clear entries of a specific category.

Parameters
categoryCategory to clear

Definition at line 179 of file unified_log_context.cpp.

179 {
180 std::unique_lock lock(mutex_);
181 for (auto it = data_.begin(); it != data_.end();) {
182 if (it->second.category == category) {
183 it = data_.erase(it);
184 } else {
185 ++it;
186 }
187 }
188}

References data_, and mutex_.

◆ empty()

bool kcenon::logger::unified_log_context::empty ( ) const
nodiscard

Check if the context is empty.

Returns
true if no entries exist

Definition at line 134 of file unified_log_context.cpp.

134 {
135 std::shared_lock lock(mutex_);
136 return data_.empty();
137}

References data_, and mutex_.

◆ get()

std::optional< context_value > kcenon::logger::unified_log_context::get ( std::string_view key) const
nodiscard

Get a context value by key.

Parameters
keyField name
Returns
Optional context value, empty if not found

Definition at line 95 of file unified_log_context.cpp.

95 {
96 std::shared_lock lock(mutex_);
97 auto it = data_.find(std::string(key));
98 if (it != data_.end()) {
99 return it->second.value;
100 }
101 return std::nullopt;
102}

References data_, and mutex_.

Referenced by get_string(), and kcenon::logger::scoped_context_guard::~scoped_context_guard().

Here is the caller graph for this function:

◆ get_as()

template<typename T >
std::optional< T > kcenon::logger::unified_log_context::get_as ( std::string_view key) const
inlinenodiscard

Definition at line 235 of file unified_log_context.h.

235 {
236 auto value = get(key);
237 if (!value) {
238 return std::nullopt;
239 }
240 if (auto* ptr = std::get_if<T>(&*value)) {
241 return *ptr;
242 }
243 return std::nullopt;
244 }
std::optional< context_value > get(std::string_view key) const
Get a context value by key.

◆ get_category()

std::optional< context_category > kcenon::logger::unified_log_context::get_category ( std::string_view key) const
nodiscard

Get the category of a context entry.

Parameters
keyField name
Returns
Optional category, empty if key not found

Definition at line 116 of file unified_log_context.cpp.

116 {
117 std::shared_lock lock(mutex_);
118 auto it = data_.find(std::string(key));
119 if (it != data_.end()) {
120 return it->second.category;
121 }
122 return std::nullopt;
123}

References data_, and mutex_.

Referenced by kcenon::logger::scoped_context_guard::~scoped_context_guard().

Here is the caller graph for this function:

◆ get_string()

std::string kcenon::logger::unified_log_context::get_string ( std::string_view key,
std::string_view default_value = "" ) const
nodiscard

Get a context value as string.

Parameters
keyField name
default_valueValue to return if not found
Returns
String value or default

Returns the value if it's a string, or the default value if not found or if the value is not a string type.

Definition at line 104 of file unified_log_context.cpp.

105 {
106 auto value = get(key);
107 if (!value) {
108 return std::string(default_value);
109 }
110 if (auto* str = std::get_if<std::string>(&*value)) {
111 return *str;
112 }
113 return std::string(default_value);
114}

References get().

Here is the call graph for this function:

◆ has()

bool kcenon::logger::unified_log_context::has ( std::string_view key) const
nodiscard

Check if a key exists in the context.

Parameters
keyField name
Returns
true if key exists

Definition at line 129 of file unified_log_context.cpp.

129 {
130 std::shared_lock lock(mutex_);
131 return data_.find(std::string(key)) != data_.end();
132}

References data_, and mutex_.

◆ keys() [1/2]

std::vector< std::string > kcenon::logger::unified_log_context::keys ( ) const
nodiscard

Get all keys in the context.

Returns
Vector of key names

Definition at line 144 of file unified_log_context.cpp.

144 {
145 std::shared_lock lock(mutex_);
146 std::vector<std::string> result;
147 result.reserve(data_.size());
148 for (const auto& [key, _] : data_) {
149 result.push_back(key);
150 }
151 return result;
152}

References data_, and mutex_.

Referenced by kcenon::logger::scoped_context_guard::~scoped_context_guard().

Here is the caller graph for this function:

◆ keys() [2/2]

std::vector< std::string > kcenon::logger::unified_log_context::keys ( context_category category) const
nodiscard

Get keys for a specific category.

Parameters
categoryCategory to filter by
Returns
Vector of key names in the specified category

Definition at line 154 of file unified_log_context.cpp.

154 {
155 std::shared_lock lock(mutex_);
156 std::vector<std::string> result;
157 for (const auto& [key, entry] : data_) {
158 if (entry.category == category) {
159 result.push_back(key);
160 }
161 }
162 return result;
163}

References kcenon::logger::unified_log_context::entry::category, data_, and mutex_.

◆ merge()

unified_log_context & kcenon::logger::unified_log_context::merge ( const unified_log_context & other,
bool overwrite = true )

Merge another context into this one.

Parameters
otherContext to merge from
overwriteIf true, other's values overwrite existing keys
Returns
Reference to this context for chaining

Copies all entries from other into this context. If overwrite is true (default), existing keys are replaced. If overwrite is false, existing keys are preserved.

Definition at line 218 of file unified_log_context.cpp.

218 {
219 std::scoped_lock lock(mutex_, other.mutex_);
220 for (const auto& [key, entry] : other.data_) {
221 if (overwrite || data_.find(key) == data_.end()) {
222 data_[key] = entry;
223 }
224 }
225 return *this;
226}

References data_, and mutex_.

◆ operator=() [1/2]

unified_log_context & kcenon::logger::unified_log_context::operator= ( const unified_log_context & other)

Copy assignment operator.

Parameters
otherContext to copy from
Returns
Reference to this context

Definition at line 22 of file unified_log_context.cpp.

22 {
23 if (this != &other) {
24 std::scoped_lock lock(mutex_, other.mutex_);
25 data_ = other.data_;
26 }
27 return *this;
28}

References data_, and mutex_.

◆ operator=() [2/2]

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

Move assignment operator.

Parameters
otherContext to move from
Returns
Reference to this context

Definition at line 30 of file unified_log_context.cpp.

30 {
31 if (this != &other) {
32 std::scoped_lock lock(mutex_, other.mutex_);
33 data_ = std::move(other.data_);
34 }
35 return *this;
36}

◆ remove()

void kcenon::logger::unified_log_context::remove ( std::string_view key)

Remove a specific key from the context.

Parameters
keyField name to remove

Definition at line 169 of file unified_log_context.cpp.

169 {
170 std::unique_lock lock(mutex_);
171 data_.erase(std::string(key));
172}

References data_, and mutex_.

Referenced by kcenon::logger::log_context_scope::remove_logger_context().

Here is the caller graph for this function:

◆ set()

unified_log_context & kcenon::logger::unified_log_context::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 context for chaining

Sets a key-value pair in the context. If the key already exists, its value and category are updated.

Definition at line 42 of file unified_log_context.cpp.

44 {
45 std::unique_lock lock(mutex_);
46 data_[std::string(key)] = entry{std::move(value), category};
47 return *this;
48}

References data_, and mutex_.

Referenced by kcenon::logger::log_context_scope::log_context_scope(), and kcenon::logger::scoped_context_guard::set().

Here is the caller graph for this function:

◆ set_otel()

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

Set OpenTelemetry context.

Parameters
ctxOpenTelemetry context structure
Returns
Reference to this context for chaining

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

Definition at line 74 of file unified_log_context.cpp.

74 {
75 std::unique_lock lock(mutex_);
76 if (!ctx.trace_id.empty()) {
77 data_["otel_trace_id"] = entry{ctx.trace_id, context_category::otel};
78 }
79 if (!ctx.span_id.empty()) {
80 data_["otel_span_id"] = entry{ctx.span_id, context_category::otel};
81 }
82 if (!ctx.trace_flags.empty()) {
83 data_["otel_trace_flags"] = entry{ctx.trace_flags, context_category::otel};
84 }
85 if (!ctx.trace_state.empty()) {
86 data_["otel_trace_state"] = entry{ctx.trace_state, context_category::otel};
87 }
88 return *this;
89}
@ otel
OpenTelemetry specific fields.

References data_, mutex_, kcenon::logger::otel, kcenon::logger::otlp::otel_context::span_id, kcenon::logger::otlp::otel_context::trace_flags, kcenon::logger::otlp::otel_context::trace_id, and kcenon::logger::otlp::otel_context::trace_state.

Referenced by kcenon::logger::scoped_context_guard::set_otel().

Here is the caller graph for this function:

◆ set_request()

unified_log_context & kcenon::logger::unified_log_context::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 context for chaining

Sets request_id and optionally correlation_id with context_category::request. These are used for request tracking.

Definition at line 63 of file unified_log_context.cpp.

65 {
66 std::unique_lock lock(mutex_);
67 data_["request_id"] = entry{std::string(request_id), context_category::request};
68 if (correlation_id) {
69 data_["correlation_id"] = entry{std::string(*correlation_id), context_category::request};
70 }
71 return *this;
72}
@ request
Request metadata (request_id, correlation_id)

References data_, mutex_, and kcenon::logger::request.

Referenced by kcenon::logger::scoped_context_guard::set_request().

Here is the caller graph for this function:

◆ set_trace()

unified_log_context & kcenon::logger::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.

Parameters
trace_idTrace identifier (32 hex chars)
span_idSpan identifier (16 hex chars)
parent_span_idOptional parent span identifier
Returns
Reference to this context for chaining

Sets trace_id, span_id, and optionally parent_span_id with context_category::trace. These are used for distributed tracing.

Definition at line 50 of file unified_log_context.cpp.

53 {
54 std::unique_lock lock(mutex_);
55 data_["trace_id"] = entry{std::string(trace_id), context_category::trace};
56 data_["span_id"] = entry{std::string(span_id), context_category::trace};
57 if (parent_span_id) {
58 data_["parent_span_id"] = entry{std::string(*parent_span_id), context_category::trace};
59 }
60 return *this;
61}
@ trace
Distributed tracing (trace_id, span_id, parent_span_id)

References data_, mutex_, and kcenon::logger::trace.

Referenced by kcenon::logger::scoped_context_guard::set_trace().

Here is the caller graph for this function:

◆ size()

size_t kcenon::logger::unified_log_context::size ( ) const
nodiscard

Get the number of entries.

Returns
Number of context entries

Definition at line 139 of file unified_log_context.cpp.

139 {
140 std::shared_lock lock(mutex_);
141 return data_.size();
142}

References data_, and mutex_.

◆ to_fields()

log_fields kcenon::logger::unified_log_context::to_fields ( ) const
nodiscard

Export context to log_fields format.

Returns
Map of string keys to log_value variants

Converts all context entries to the log_fields format used by the structured logging system. Values are converted as follows:

  • std::monostate -> (key is skipped)
  • bool -> bool
  • int64_t -> int64_t
  • double -> double
  • std::string -> std::string

Definition at line 194 of file unified_log_context.cpp.

194 {
195 std::shared_lock lock(mutex_);
196 log_fields result;
197 for (const auto& [key, entry] : data_) {
198 std::visit(
199 [&result, &key](const auto& val) {
200 using T = std::decay_t<decltype(val)>;
201 if constexpr (std::is_same_v<T, std::monostate>) {
202 // Skip null values
203 } else if constexpr (std::is_same_v<T, bool>) {
204 result[key] = val;
205 } else if constexpr (std::is_same_v<T, int64_t>) {
206 result[key] = val;
207 } else if constexpr (std::is_same_v<T, double>) {
208 result[key] = val;
209 } else if constexpr (std::is_same_v<T, std::string>) {
210 result[key] = val;
211 }
212 },
213 entry.value);
214 }
215 return result;
216}
std::unordered_map< std::string, log_value > log_fields
Type alias for structured fields map.
Definition log_entry.h:75

References data_, mutex_, and kcenon::logger::unified_log_context::entry::value.

Member Data Documentation

◆ data_

std::unordered_map<std::string, entry> kcenon::logger::unified_log_context::data_
private

◆ mutex_

std::shared_mutex kcenon::logger::unified_log_context::mutex_
mutableprivate

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