Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
timestamp_formatter.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
10#include <sstream>
11#include <iomanip>
12#include <ctime>
13
14// Use common_system's standard interface
15#include <kcenon/common/interfaces/logger_interface.h>
16
36namespace kcenon::logger {
37
58public:
66 options_ = opts;
67 }
68
81 std::string format(const log_entry& entry) const override {
82 std::ostringstream oss;
83
84 // Timestamp
86 oss << "[" << utils::time_utils::format_timestamp(entry.timestamp) << "] ";
87 }
88
89 // Level (with color)
91 if (options_.use_colors) {
93 }
94 oss << "[" << utils::string_utils::level_to_string(entry.level) << "] ";
95 if (options_.use_colors) {
97 }
98 }
99
100 // Thread ID
101 if (options_.include_thread_id && entry.thread_id) {
102 oss << "[thread:" << entry.thread_id->to_string() << "] ";
103 }
104
105 // Message
106 oss << entry.message.to_string();
107
108 // Source location
110 oss << " [";
111
112 // Extract filename from path
113 std::string file_path = entry.location->file.to_string();
114 if (!file_path.empty()) {
115 std::string filename = utils::string_utils::extract_filename(file_path);
116 oss << filename << ":" << entry.location->line;
117 }
118
119 // Function name
120 std::string func = entry.location->function.to_string();
121 if (!func.empty()) {
122 oss << " in " << func << "()";
123 }
124
125 oss << "]";
126 }
127
128 return oss.str();
129 }
130
137 std::string get_name() const override {
138 return "timestamp_formatter";
139 }
140
141private:
142 // Note: Formatting functions moved to utils::time_utils and utils::string_utils (Phase 3.4)
143 // This reduces code duplication and improves maintainability.
144};
145
146} // namespace kcenon::logger
Abstract interface for log message formatters.
std::string to_string() const
Convert to std::string.
Default formatter with human-readable timestamp format.
std::string format(const log_entry &entry) const override
Format a log entry to human-readable string.
timestamp_formatter(const format_options &opts=format_options{})
Constructor with optional format options.
std::string get_name() const override
Get formatter name.
static std::string level_to_color(log_level level, bool use_colors=true)
Convert log level to ANSI color code.
static std::string level_to_string(log_level level)
Convert log level to human-readable string.
static const char * color_reset()
ANSI color reset sequence.
static std::string extract_filename(const std::string &file_path)
Extract filename from full file path.
static std::string format_timestamp(const std::chrono::system_clock::time_point &tp)
Format timestamp to human-readable format (YYYY-MM-DD HH:MM:SS.mmm)
Definition time_utils.h:38
Interface for log message formatters (Strategy Pattern) kcenon.
String utility functions for log formatting and conversion.
Configuration options for log formatting.
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
std::optional< small_string_64 > thread_id
Optional thread identifier.
Definition log_entry.h:190
log_level level
Severity level of the log message.
Definition log_entry.h:162
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
Time utility functions for timestamp formatting.