Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
unified_log_context.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
39
40#include <optional>
41#include <shared_mutex>
42#include <string>
43#include <string_view>
44#include <unordered_map>
45#include <variant>
46#include <vector>
47
48namespace kcenon::logger {
49
60using context_value = std::variant<std::monostate, bool, int64_t, double, std::string>;
61
72enum class context_category : uint8_t {
73 custom = 0,
74 trace = 1,
75 request = 2,
76 otel = 3
77};
78
118public:
123
129
135
141 unified_log_context& operator=(const unified_log_context& other);
142
148 unified_log_context& operator=(unified_log_context&& other) noexcept;
149
154
155 // =========================================================================
156 // Setters
157 // =========================================================================
158
169 unified_log_context& set(std::string_view key,
170 context_value value,
171 context_category category = context_category::custom);
172
183 unified_log_context& set_trace(std::string_view trace_id,
184 std::string_view span_id,
185 std::optional<std::string_view> parent_span_id = std::nullopt);
186
196 unified_log_context& set_request(std::string_view request_id,
197 std::optional<std::string_view> correlation_id = std::nullopt);
198
207 unified_log_context& set_otel(const otlp::otel_context& ctx);
208
209 // =========================================================================
210 // Getters
211 // =========================================================================
212
218 [[nodiscard]] std::optional<context_value> get(std::string_view key) const;
219
234 template <typename T>
235 [[nodiscard]] std::optional<T> get_as(std::string_view key) const {
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 }
245
255 [[nodiscard]] std::string get_string(std::string_view key,
256 std::string_view default_value = "") const;
257
263 [[nodiscard]] std::optional<context_category> get_category(std::string_view key) const;
264
265 // =========================================================================
266 // Query methods
267 // =========================================================================
268
274 [[nodiscard]] bool has(std::string_view key) const;
275
280 [[nodiscard]] bool empty() const;
281
286 [[nodiscard]] size_t size() const;
287
292 [[nodiscard]] std::vector<std::string> keys() const;
293
299 [[nodiscard]] std::vector<std::string> keys(context_category category) const;
300
301 // =========================================================================
302 // Removal
303 // =========================================================================
304
309 void remove(std::string_view key);
310
314 void clear();
315
320 void clear(context_category category);
321
322 // =========================================================================
323 // Export
324 // =========================================================================
325
338 [[nodiscard]] log_fields to_fields() const;
339
350 unified_log_context& merge(const unified_log_context& other, bool overwrite = true);
351
352private:
360
361 mutable std::shared_mutex mutex_;
362 std::unordered_map<std::string, entry> data_;
363};
364
365} // namespace kcenon::logger
Unified interface for managing all types of logging context.
unified_log_context()=default
Default constructor.
std::unordered_map< std::string, entry > data_
std::optional< T > get_as(std::string_view key) const
~unified_log_context()=default
Destructor.
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.
Definition log_entry.h:75
std::variant< std::monostate, bool, int64_t, double, std::string > context_value
Value type for unified context storage.
@ size
Rotate based on file size only.
context_category
Categories for organizing context entries.
@ trace
Distributed tracing (trace_id, span_id, parent_span_id)
@ request
Request metadata (request_id, correlation_id)
@ otel
OpenTelemetry specific fields.
@ custom
User-defined custom fields.
OpenTelemetry context structure for trace correlation kcenon.
OpenTelemetry context for trace correlation.