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

Convenience class for setting a single context field with RAII. More...

#include <log_context_scope.h>

Collaboration diagram for kcenon::logger::scoped_context:
Collaboration graph

Public Member Functions

 scoped_context (const std::string &key, const std::string &value)
 Construct with string value.
 
 scoped_context (const std::string &key, int64_t value)
 Construct with integer value.
 
 scoped_context (const std::string &key, int value)
 Construct with int value (converts to int64_t)
 
 scoped_context (const std::string &key, double value)
 Construct with double value.
 
 scoped_context (const std::string &key, bool value)
 Construct with boolean value.
 
 ~scoped_context ()
 Destructor - restores or removes the context field.
 
 scoped_context (const scoped_context &)=delete
 
scoped_contextoperator= (const scoped_context &)=delete
 
 scoped_context (scoped_context &&)=delete
 
scoped_contextoperator= (scoped_context &&)=delete
 

Private Attributes

std::string key_
 
log_value previous_value_
 
bool had_previous_
 

Detailed Description

Convenience class for setting a single context field with RAII.

Provides a simpler interface for setting a single context field that will be automatically removed when the scope exits.

Examples
structured_logging_example.cpp.

Definition at line 347 of file log_context_scope.h.

Constructor & Destructor Documentation

◆ scoped_context() [1/7]

kcenon::logger::scoped_context::scoped_context ( const std::string & key,
const std::string & value )
inline

Construct with string value.

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

Definition at line 352 of file log_context_scope.h.

353 : key_(key), had_previous_(false) {
354 auto prev = log_context_storage::get_field(key);
355 if (prev) {
356 had_previous_ = true;
357 previous_value_ = *prev;
358 }
359 log_context_storage::set(key, value);
360 }
static void set(const std::string &key, const std::string &value)
Set a string context field for the current thread.
static std::optional< log_value > get_field(const std::string &key)
Get a specific field value if it exists.

References kcenon::logger::log_context_storage::get_field(), had_previous_, previous_value_, and kcenon::logger::log_context_storage::set().

Here is the call graph for this function:

◆ scoped_context() [2/7]

kcenon::logger::scoped_context::scoped_context ( const std::string & key,
int64_t value )
inline

Construct with integer value.

Definition at line 365 of file log_context_scope.h.

366 : key_(key), had_previous_(false) {
367 auto prev = log_context_storage::get_field(key);
368 if (prev) {
369 had_previous_ = true;
370 previous_value_ = *prev;
371 }
372 log_context_storage::set(key, value);
373 }

References kcenon::logger::log_context_storage::get_field(), had_previous_, previous_value_, and kcenon::logger::log_context_storage::set().

Here is the call graph for this function:

◆ scoped_context() [3/7]

kcenon::logger::scoped_context::scoped_context ( const std::string & key,
int value )
inline

Construct with int value (converts to int64_t)

Definition at line 378 of file log_context_scope.h.

379 : scoped_context(key, static_cast<int64_t>(value)) {}
scoped_context(const std::string &key, const std::string &value)
Construct with string value.

◆ scoped_context() [4/7]

kcenon::logger::scoped_context::scoped_context ( const std::string & key,
double value )
inline

Construct with double value.

Definition at line 384 of file log_context_scope.h.

385 : key_(key), had_previous_(false) {
386 auto prev = log_context_storage::get_field(key);
387 if (prev) {
388 had_previous_ = true;
389 previous_value_ = *prev;
390 }
391 log_context_storage::set(key, value);
392 }

References kcenon::logger::log_context_storage::get_field(), had_previous_, previous_value_, and kcenon::logger::log_context_storage::set().

Here is the call graph for this function:

◆ scoped_context() [5/7]

kcenon::logger::scoped_context::scoped_context ( const std::string & key,
bool value )
inline

Construct with boolean value.

Definition at line 397 of file log_context_scope.h.

398 : key_(key), had_previous_(false) {
399 auto prev = log_context_storage::get_field(key);
400 if (prev) {
401 had_previous_ = true;
402 previous_value_ = *prev;
403 }
404 log_context_storage::set(key, value);
405 }

References kcenon::logger::log_context_storage::get_field(), had_previous_, previous_value_, and kcenon::logger::log_context_storage::set().

Here is the call graph for this function:

◆ ~scoped_context()

kcenon::logger::scoped_context::~scoped_context ( )
inline

Destructor - restores or removes the context field.

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

Definition at line 410 of file log_context_scope.h.

410 {
411 if (had_previous_) {
413 } else {
415 }
416 }
static void remove(const std::string &key)
Remove a context field for the current thread.

References had_previous_, key_, previous_value_, kcenon::logger::log_context_storage::remove(), and kcenon::logger::log_context_storage::set().

Here is the call graph for this function:

◆ scoped_context() [6/7]

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

◆ scoped_context() [7/7]

kcenon::logger::scoped_context::scoped_context ( scoped_context && )
delete

Member Function Documentation

◆ operator=() [1/2]

◆ operator=() [2/2]

scoped_context & kcenon::logger::scoped_context::operator= ( scoped_context && )
delete

Member Data Documentation

◆ had_previous_

◆ key_

std::string kcenon::logger::scoped_context::key_
private

◆ previous_value_


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