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

Thread-local storage for structured logging context fields. More...

#include <log_context_scope.h>

Collaboration diagram for kcenon::logger::log_context_storage:
Collaboration graph

Static Public Member Functions

static void set (const std::string &key, const std::string &value)
 Set a string context field for the current thread.
 
static void set (const std::string &key, const char *value)
 Set a C-string context field for the current thread.
 
static void set (const std::string &key, int64_t value)
 Set an integer context field for the current thread.
 
static void set (const std::string &key, double value)
 Set a double context field for the current thread.
 
static void set (const std::string &key, bool value)
 Set a boolean context field for the current thread.
 
static void set (const std::string &key, const log_value &value)
 Set a log_value context field for the current thread.
 
static void set_all (const log_fields &fields)
 Set multiple context fields at once.
 
static void remove (const std::string &key)
 Remove a context field for the current thread.
 
static log_fields get ()
 Get all context fields for the current thread.
 
static void clear ()
 Clear all context fields for the current thread.
 
static bool has_context ()
 Check if any context fields are set for the current thread.
 
static std::optional< log_valueget_field (const std::string &key)
 Get a specific field value if it exists.
 

Static Private Member Functions

static log_fieldsget_storage ()
 
static bool & get_has_context ()
 

Detailed Description

Thread-local storage for structured logging context fields.

Provides thread-safe storage and retrieval of context fields using thread-local storage. This allows automatic context propagation within a thread without affecting other threads.

Thread-local context takes precedence over logger-level context when both are set. Fields are merged with thread-local fields overriding logger-level fields with the same key.

Definition at line 75 of file log_context_scope.h.

Member Function Documentation

◆ clear()

static void kcenon::logger::log_context_storage::clear ( )
inlinestatic

Clear all context fields for the current thread.

Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/core/log_context_scope.h.

Definition at line 175 of file log_context_scope.h.

175 {
176 get_storage().clear();
177 get_has_context() = false;
178 }

References get_has_context(), and get_storage().

Here is the call graph for this function:

◆ get()

static log_fields kcenon::logger::log_context_storage::get ( )
inlinestaticnodiscard

Get all context fields for the current thread.

Returns
Copy of context fields, empty if not set
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/core/log_context_scope.h.

Definition at line 165 of file log_context_scope.h.

165 {
166 if (get_has_context()) {
167 return get_storage();
168 }
169 return {};
170 }

References get_has_context(), and get_storage().

Here is the call graph for this function:

◆ get_field()

static std::optional< log_value > kcenon::logger::log_context_storage::get_field ( const std::string & key)
inlinestaticnodiscard

Get a specific field value if it exists.

Parameters
keyField name
Returns
Optional field value
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/core/log_context_scope.h.

Definition at line 193 of file log_context_scope.h.

193 {
194 if (!get_has_context()) {
195 return std::nullopt;
196 }
197 auto& storage = get_storage();
198 auto it = storage.find(key);
199 if (it != storage.end()) {
200 return it->second;
201 }
202 return std::nullopt;
203 }

References get_has_context(), and get_storage().

Referenced by kcenon::logger::scoped_context::scoped_context(), kcenon::logger::scoped_context::scoped_context(), kcenon::logger::scoped_context::scoped_context(), and kcenon::logger::scoped_context::scoped_context().

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

◆ get_has_context()

static bool & kcenon::logger::log_context_storage::get_has_context ( )
inlinestaticprivate
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/core/log_context_scope.h.

Definition at line 211 of file log_context_scope.h.

211 {
212 thread_local bool has_ctx = false;
213 return has_ctx;
214 }

Referenced by clear(), get(), get_field(), has_context(), remove(), set(), set(), set(), set(), set(), set(), and set_all().

Here is the caller graph for this function:

◆ get_storage()

static log_fields & kcenon::logger::log_context_storage::get_storage ( )
inlinestaticprivate
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/core/log_context_scope.h.

Definition at line 206 of file log_context_scope.h.

206 {
207 thread_local log_fields storage;
208 return storage;
209 }
std::unordered_map< std::string, log_value > log_fields
Type alias for structured fields map.
Definition log_entry.h:75

Referenced by clear(), get(), get_field(), remove(), set(), set(), set(), set(), set(), set(), and set_all().

Here is the caller graph for this function:

◆ has_context()

static bool kcenon::logger::log_context_storage::has_context ( )
inlinestaticnodiscard

Check if any context fields are set for the current thread.

Returns
true if context is set
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/core/log_context_scope.h.

Definition at line 184 of file log_context_scope.h.

184 {
185 return get_has_context();
186 }

References get_has_context().

Here is the call graph for this function:

◆ remove()

static void kcenon::logger::log_context_storage::remove ( const std::string & key)
inlinestatic

Remove a context field for the current thread.

Parameters
keyField name to remove
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/core/log_context_scope.h.

Definition at line 154 of file log_context_scope.h.

154 {
155 get_storage().erase(key);
156 if (get_storage().empty()) {
157 get_has_context() = false;
158 }
159 }

References get_has_context(), and get_storage().

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

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

◆ set() [1/6]

static void kcenon::logger::log_context_storage::set ( const std::string & key,
bool value )
inlinestatic

Set a boolean context field for the current thread.

Parameters
keyField name
valueField value

Definition at line 122 of file log_context_scope.h.

122 {
123 get_storage()[key] = value;
124 get_has_context() = true;
125 }

References get_has_context(), and get_storage().

Here is the call graph for this function:

◆ set() [2/6]

static void kcenon::logger::log_context_storage::set ( const std::string & key,
const char * value )
inlinestatic

Set a C-string context field for the current thread.

Parameters
keyField name
valueField value (will be converted to std::string)

Definition at line 92 of file log_context_scope.h.

92 {
93 get_storage()[key] = std::string(value);
94 get_has_context() = true;
95 }

References get_has_context(), and get_storage().

Here is the call graph for this function:

◆ set() [3/6]

static void kcenon::logger::log_context_storage::set ( const std::string & key,
const log_value & value )
inlinestatic

Set a log_value context field for the current thread.

Parameters
keyField name
valueField value

Definition at line 132 of file log_context_scope.h.

132 {
133 get_storage()[key] = value;
134 get_has_context() = true;
135 }

References get_has_context(), and get_storage().

Here is the call graph for this function:

◆ set() [4/6]

static void kcenon::logger::log_context_storage::set ( const std::string & key,
const std::string & value )
inlinestatic

Set a string context field for the current thread.

Parameters
keyField name
valueField value
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/core/log_context_scope.h.

Definition at line 82 of file log_context_scope.h.

82 {
83 get_storage()[key] = value;
84 get_has_context() = true;
85 }

References get_has_context(), and get_storage().

Referenced by kcenon::logger::log_context_scope::log_context_scope(), kcenon::logger::scoped_context::scoped_context(), kcenon::logger::scoped_context::scoped_context(), kcenon::logger::scoped_context::scoped_context(), kcenon::logger::scoped_context::scoped_context(), and kcenon::logger::scoped_context::~scoped_context().

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

◆ set() [5/6]

static void kcenon::logger::log_context_storage::set ( const std::string & key,
double value )
inlinestatic

Set a double context field for the current thread.

Parameters
keyField name
valueField value

Definition at line 112 of file log_context_scope.h.

112 {
113 get_storage()[key] = value;
114 get_has_context() = true;
115 }

References get_has_context(), and get_storage().

Here is the call graph for this function:

◆ set() [6/6]

static void kcenon::logger::log_context_storage::set ( const std::string & key,
int64_t value )
inlinestatic

Set an integer context field for the current thread.

Parameters
keyField name
valueField value

Definition at line 102 of file log_context_scope.h.

102 {
103 get_storage()[key] = value;
104 get_has_context() = true;
105 }

References get_has_context(), and get_storage().

Here is the call graph for this function:

◆ set_all()

static void kcenon::logger::log_context_storage::set_all ( const log_fields & fields)
inlinestatic

Set multiple context fields at once.

Parameters
fieldsFields to set
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/core/log_context_scope.h.

Definition at line 141 of file log_context_scope.h.

141 {
142 for (const auto& [key, value] : fields) {
143 get_storage()[key] = value;
144 }
145 if (!fields.empty()) {
146 get_has_context() = true;
147 }
148 }

References get_has_context(), and get_storage().

Here is the call graph for this function:

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