15#include <kcenon/common/interfaces/logger_interface.h>
23using log_level = common::interfaces::log_level;
50 case log_level::critical:
return "CRITICAL";
51 case log_level::error:
return "ERROR";
52 case log_level::warning:
return "WARNING";
53 case log_level::info:
return "INFO";
54 case log_level::debug:
return "DEBUG";
55 case log_level::trace:
return "TRACE";
56 default:
return "UNKNOWN";
65 std::string
format_timestamp(
const std::chrono::system_clock::time_point& timestamp)
const {
66 auto time_t = std::chrono::system_clock::to_time_t(timestamp);
67 std::ostringstream oss;
68 oss << std::put_time(std::gmtime(&time_t),
"%Y-%m-%dT%H:%M:%SZ");
77 std::ostringstream oss;
78 oss << std::this_thread::get_id();
90 std::ostringstream oss;
97 oss <<
"[" << std::string_view(*entry.
thread_id) <<
"] ";
102 oss << std::string_view(entry.
message);
105 oss <<
" [" << std::string_view(entry.
location->file)
107 <<
":" << std::string_view(entry.
location->function) <<
"]";
122class json_formatter :
public base_formatter {
125 std::ostringstream oss;
134 oss <<
",\"location\":{";
136 oss <<
"\"line\":" << entry.
location->line <<
",";
155 template<
typename StringType>
157 std::ostringstream escaped;
158 for (
unsigned char c : str) {
159 if (c ==
'"') escaped <<
"\\\"";
160 else if (c ==
'\\') escaped <<
"\\\\";
161 else if (c ==
'\n') escaped <<
"\\n";
162 else if (c ==
'\r') escaped <<
"\\r";
163 else if (c ==
'\t') escaped <<
"\\t";
164 else if (c ==
'\b') escaped <<
"\\b";
165 else if (c ==
'\f') escaped <<
"\\f";
167 else if (c >= 0x00 && c <= 0x1F) {
169 << std::hex << std::setw(4) << std::setfill(
'0')
170 <<
static_cast<int>(c);
174 return escaped.str();
185 std::ostringstream oss;
std::string to_string() const
Convert to std::string.
Data structures for representing log entries and source locations kcenon.
common::interfaces::log_level log_level
Represents a single log entry with all associated metadata.
std::optional< source_location > location
Optional source code location information.
std::optional< small_string_64 > thread_id
Optional thread identifier.
log_level level
Severity level of the log message.
std::optional< small_string_128 > category
Optional category for log filtering and routing.
small_string_256 message
The actual log message.
std::chrono::system_clock::time_point timestamp
Timestamp when the log entry was created.