82 static void set(
const std::string& key,
const std::string& value) {
92 static void set(
const std::string& key,
const char* value) {
102 static void set(
const std::string& key, int64_t value) {
112 static void set(
const std::string& key,
double value) {
122 static void set(
const std::string& key,
bool value) {
142 for (
const auto& [key, value] : fields) {
145 if (!fields.empty()) {
154 static void remove(
const std::string& key) {
193 [[nodiscard]]
static std::optional<log_value>
get_field(
const std::string& key) {
198 auto it = storage.find(key);
199 if (it != storage.end()) {
212 thread_local bool has_ctx =
false;
264 for (
const auto& [key, value] : fields) {
266 if (previous_context_.find(key) == previous_context_.end()) {
267 added_keys_.push_back(key);
269 log_context_storage::set(key, value);
291 for (
const auto& key : added_keys_) {
292 log_context_storage::remove(key);
297 for (
const auto& [key, value] : previous_context_) {
299 if (std::find(added_keys_.begin(), added_keys_.end(), key) == added_keys_.end()) {
300 log_context_storage::set(key, value);
306 if (logger_ !=
nullptr) {
307 for (
const auto& key : added_keys_) {
308 remove_logger_context(key);
320 void remove_logger_context(
const std::string& key);
RAII guard for structured logging context.
log_context_scope(const log_fields &fields)
Construct scope with initial fields.
std::vector< std::string > added_keys_
log_context_scope(log_context_scope &&)=delete
log_context_scope & operator=(log_context_scope &&)=delete
log_context_scope(const log_context_scope &)=delete
~log_context_scope()
Destructor - restores previous context.
log_context_scope & operator=(const log_context_scope &)=delete
log_fields previous_context_
Thread-local storage for structured logging context fields.
static void set(const std::string &key, bool value)
Set a boolean context field for the current thread.
static void set_all(const log_fields &fields)
Set multiple context fields at once.
static bool & get_has_context()
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, double value)
Set a double context field for the current thread.
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 std::optional< log_value > get_field(const std::string &key)
Get a specific field value if it exists.
static log_fields & get_storage()
static bool has_context()
Check if any context fields are set 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 clear()
Clear all context fields 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, const log_value &value)
Set a log_value context field for the current thread.
Convenience class for setting a single context field with RAII.
scoped_context(const std::string &key, int64_t value)
Construct with integer value.
scoped_context(const scoped_context &)=delete
~scoped_context()
Destructor - restores or removes the context field.
scoped_context(const std::string &key, const std::string &value)
Construct with string value.
scoped_context & operator=(scoped_context &&)=delete
scoped_context(const std::string &key, bool value)
Construct with boolean value.
scoped_context(const std::string &key, int value)
Construct with int value (converts to int64_t)
scoped_context(scoped_context &&)=delete
scoped_context & operator=(const scoped_context &)=delete
log_value previous_value_
scoped_context(const std::string &key, double value)
Construct with double value.
Data structures for representing log entries and source locations kcenon.
DLL export/import macros for logger_system shared library support.
#define LOGGER_SYSTEM_API
std::unordered_map< std::string, log_value > log_fields
Type alias for structured fields map.
std::variant< std::string, int64_t, double, bool > log_value
Value type for structured logging fields.