Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
log_entry.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
41#include <string>
42#include <chrono>
43#include <optional>
44#include <unordered_map>
45#include <variant>
46#include <cstdint>
48
49// Use common_system's standard interface
50#include <kcenon/common/interfaces/logger_interface.h>
51
52// OpenTelemetry context support
54
55namespace kcenon::logger {
56
57// Type alias for log_level
58using log_level = common::interfaces::log_level;
59
69using log_value = std::variant<std::string, int64_t, double, bool>;
70
75using log_fields = std::unordered_map<std::string, log_value>;
76
99
104 int line;
105
111
118 source_location(const std::string& f = "", int l = 0, const std::string& func = "")
119 : file(f), line(l), function(func) {}
120
130 source_location(const char* f = "", int l = 0, const char* func = "")
131 : file(f), line(l), function(func) {}
132};
133
155struct log_entry {
156 // Required fields
157
162 log_level level;
163
170
175 std::chrono::system_clock::time_point timestamp;
176
177 // Optional fields
178
183 std::optional<source_location> location;
184
190 std::optional<small_string_64> thread_id;
191
197 std::optional<small_string_128> category;
198
204 std::optional<otlp::otel_context> otel_ctx;
205
213 std::optional<log_fields> fields;
214
231 log_entry(log_level lvl,
232 const std::string& msg,
233 std::chrono::system_clock::time_point ts = std::chrono::system_clock::now())
234 : level(lvl), message(msg), timestamp(ts) {}
235
261 log_entry(log_level lvl,
262 const std::string& msg,
263 const std::string& file,
264 int line,
265 const std::string& function,
266 std::chrono::system_clock::time_point ts = std::chrono::system_clock::now())
267 : level(lvl), message(msg), timestamp(ts),
268 location(source_location{file, line, function}) {}
269
275 log_entry(log_entry&&) noexcept = default;
276
282 log_entry& operator=(log_entry&&) noexcept = default;
283
289 log_entry(const log_entry&) = delete;
290
296 log_entry& operator=(const log_entry&) = delete;
297};
298
299} // namespace kcenon::logger
common::interfaces::log_level log_level
std::unordered_map< std::string, log_value > log_fields
Type alias for structured fields map.
Definition log_entry.h:75
std::variant< std::string, int64_t, double, bool > log_value
Value type for structured logging fields.
Definition log_entry.h:69
OpenTelemetry context structure for trace correlation kcenon.
Small String Optimization (SSO) for short log messages.
Represents a single log entry with all associated metadata.
Definition log_entry.h:155
std::optional< source_location > location
Optional source code location information.
Definition log_entry.h:183
log_entry(log_level lvl, const std::string &msg, const std::string &file, int line, const std::string &function, std::chrono::system_clock::time_point ts=std::chrono::system_clock::now())
Definition log_entry.h:261
std::optional< log_fields > fields
Optional structured fields for key-value logging.
Definition log_entry.h:213
std::optional< small_string_64 > thread_id
Optional thread identifier.
Definition log_entry.h:190
std::optional< otlp::otel_context > otel_ctx
Optional OpenTelemetry context for trace correlation.
Definition log_entry.h:204
log_level level
Severity level of the log message.
Definition log_entry.h:162
log_entry(log_entry &&) noexcept=default
Move constructor.
log_entry(log_level lvl, const std::string &msg, std::chrono::system_clock::time_point ts=std::chrono::system_clock::now())
Definition log_entry.h:231
std::optional< small_string_128 > category
Optional category for log filtering and routing.
Definition log_entry.h:197
small_string_256 message
The actual log message.
Definition log_entry.h:169
std::chrono::system_clock::time_point timestamp
Timestamp when the log entry was created.
Definition log_entry.h:175
Source code location information for debugging.
Definition log_entry.h:93
small_string_128 function
Function or method name.
Definition log_entry.h:110
small_string_256 file
Source file path.
Definition log_entry.h:98
int line
Line number in the source file.
Definition log_entry.h:104
source_location(const char *f="", int l=0, const char *func="")
Construct source location from C-strings.
Definition log_entry.h:130
source_location(const std::string &f="", int l=0, const std::string &func="")
Construct source location from std::string.
Definition log_entry.h:118