Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
scoped_context_guard.h
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
5#pragma once
6
32#include "unified_log_context.h"
33
35
36#include <optional>
37#include <string>
38#include <string_view>
39#include <vector>
40
41namespace kcenon::logger {
42
43// Forward declaration
44class logger;
45
102public:
110 explicit scoped_context_guard(logger& log);
111
123 std::string_view key,
124 context_value value,
125 context_category category = context_category::custom);
126
135
136 // Non-copyable
139
140 // Movable
142 scoped_context_guard& operator=(scoped_context_guard&& other) noexcept;
143
144 // =========================================================================
145 // Setters (chainable)
146 // =========================================================================
147
159 scoped_context_guard& set(std::string_view key,
160 context_value value,
161 context_category category = context_category::custom);
162
174 scoped_context_guard& set_trace(std::string_view trace_id,
175 std::string_view span_id,
176 std::optional<std::string_view> parent_span_id = std::nullopt);
177
188 scoped_context_guard& set_request(std::string_view request_id,
189 std::optional<std::string_view> correlation_id = std::nullopt);
190
199 scoped_context_guard& set_otel(const otlp::otel_context& ctx);
200
201private:
206 void track_key(std::string_view key);
207
208 logger* logger_; // Logger whose context we're managing
209 unified_log_context saved_context_; // Saved context state
210 std::vector<std::string> tracked_keys_; // Keys added/modified by this guard
211};
212
213} // namespace kcenon::logger
RAII guard for automatic context management.
scoped_context_guard & operator=(const scoped_context_guard &)=delete
scoped_context_guard(const scoped_context_guard &)=delete
Unified interface for managing all types of logging context.
DLL export/import macros for logger_system shared library support.
#define LOGGER_SYSTEM_API
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.
OpenTelemetry context for trace correlation.
Unified interface for managing all types of logging context.