Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
kcenon::logger::timestamp_formatter Class Reference

Default formatter with human-readable timestamp format. More...

#include <timestamp_formatter.h>

Inheritance diagram for kcenon::logger::timestamp_formatter:
Inheritance graph
Collaboration diagram for kcenon::logger::timestamp_formatter:
Collaboration graph

Public Member Functions

 timestamp_formatter (const format_options &opts=format_options{})
 Constructor with optional format options.
 
std::string format (const log_entry &entry) const override
 Format a log entry to human-readable string.
 
std::string get_name () const override
 Get formatter name.
 
- Public Member Functions inherited from kcenon::logger::log_formatter_interface
virtual ~log_formatter_interface ()=default
 
virtual void set_options (const format_options &opts)
 Set formatting options.
 
virtual format_options get_options () const
 Get current formatting options.
 

Additional Inherited Members

- Protected Attributes inherited from kcenon::logger::log_formatter_interface
format_options options_
 

Detailed Description

Default formatter with human-readable timestamp format.

Provides the traditional log format with timestamps, levels, and optional source location. This is the default formatter used by the logger system and is optimized for human readability.

Features:

  • Millisecond-precision timestamps
  • Color-coded log levels (if enabled)
  • Thread ID tracking
  • Source location information (file, line, function)
  • Automatic filename extraction from paths

Thread-safety: This formatter is stateless and thread-safe.

Since
1.2.0

Definition at line 57 of file timestamp_formatter.h.

Constructor & Destructor Documentation

◆ timestamp_formatter()

kcenon::logger::timestamp_formatter::timestamp_formatter ( const format_options & opts = format_options{})
inlineexplicit

Constructor with optional format options.

Parameters
optsInitial format options
Since
1.2.0

Definition at line 65 of file timestamp_formatter.h.

65 {}) {
66 options_ = opts;
67 }

Member Function Documentation

◆ format()

std::string kcenon::logger::timestamp_formatter::format ( const log_entry & entry) const
inlineoverridevirtual

Format a log entry to human-readable string.

Parameters
entryThe log entry to format
Returns
Formatted string with timestamp, level, message, and location

Produces output in the format: [YYYY-MM-DD HH:MM:SS.mmm] [LEVEL] [thread:TID] message [file:line in function()]

Note
Thread-safe. Can be called concurrently.
Since
1.2.0

Implements kcenon::logger::log_formatter_interface.

Definition at line 81 of file timestamp_formatter.h.

81 {
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) {
92 oss << utils::string_utils::level_to_color(entry.level, true);
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
109 if (options_.include_source_location && entry.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 }
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

References kcenon::logger::utils::string_utils::color_reset(), kcenon::logger::utils::string_utils::extract_filename(), kcenon::logger::utils::time_utils::format_timestamp(), kcenon::logger::format_options::include_level, kcenon::logger::format_options::include_source_location, kcenon::logger::format_options::include_thread_id, kcenon::logger::format_options::include_timestamp, kcenon::logger::log_entry::level, kcenon::logger::utils::string_utils::level_to_color(), kcenon::logger::utils::string_utils::level_to_string(), kcenon::logger::log_entry::location, kcenon::logger::log_entry::message, kcenon::logger::log_formatter_interface::options_, kcenon::logger::log_entry::thread_id, kcenon::logger::log_entry::timestamp, kcenon::logger::small_string< SSO_SIZE >::to_string(), and kcenon::logger::format_options::use_colors.

Here is the call graph for this function:

◆ get_name()

std::string kcenon::logger::timestamp_formatter::get_name ( ) const
inlineoverridevirtual

Get formatter name.

Returns
"timestamp_formatter"
Since
1.2.0

Implements kcenon::logger::log_formatter_interface.

Definition at line 137 of file timestamp_formatter.h.

137 {
138 return "timestamp_formatter";
139 }

The documentation for this class was generated from the following file: