|
Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
|
Customizable formatter using template strings with placeholders. More...
#include <template_formatter.h>


Classes | |
| struct | template_segment |
| Segment of parsed template. More... | |
Public Member Functions | |
| template_formatter (const std::string &template_pattern=DEFAULT_TEMPLATE, const format_options &opts=format_options{}) | |
| std::string | format (const log_entry &entry) const override |
| Format a log entry using the template. | |
| std::string | get_name () const override |
| Get formatter name. | |
| const std::string & | get_template () const |
| Get current template pattern. | |
| void | set_template (const std::string &template_pattern) |
| Set a new template pattern. | |
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. | |
Static Public Attributes | |
| static constexpr const char * | DEFAULT_TEMPLATE |
| Default template pattern. | |
Private Member Functions | |
| void | parse_template () |
| Parse template string into segments. | |
| std::string | resolve_placeholder (const std::string &name, int width, const log_entry &entry) const |
| Resolve a placeholder to its actual value. | |
Static Private Member Functions | |
| static std::string | format_field_value (const log_value &value) |
| Format a structured field value to string. | |
| static size_t | calculate_display_width (const std::string &str) |
| Calculate display width excluding ANSI escape codes. | |
Private Attributes | |
| std::string | template_ |
| Original template string. | |
| std::vector< template_segment > | segments_ |
| Parsed template segments. | |
Additional Inherited Members | |
Protected Attributes inherited from kcenon::logger::log_formatter_interface | |
| format_options | options_ |
Customizable formatter using template strings with placeholders.
Provides flexible log formatting by allowing users to define their own output format using a template string with placeholders. This enables adaptation to various logging requirements without creating new formatter classes.
Features:
Thread-safety: This formatter is thread-safe once constructed.
Definition at line 87 of file template_formatter.h.
|
inlineexplicit |
Definition at line 110 of file template_formatter.h.
|
inlinestaticnodiscardprivate |
Calculate display width excluding ANSI escape codes.
| str | String that may contain ANSI codes |
Definition at line 368 of file template_formatter.h.
Referenced by resolve_placeholder().

|
inlinenodiscardoverridevirtual |
Format a log entry using the template.
| entry | The log entry to format |
Replaces all placeholders in the template with actual values from the log entry. Missing optional fields are replaced with empty strings or default values.
Implements kcenon::logger::log_formatter_interface.
Definition at line 131 of file template_formatter.h.
References resolve_placeholder(), and segments_.
Referenced by template_formatter_example().


|
inlinestaticnodiscardprivate |
Format a structured field value to string.
| value | The log_value to format |
Definition at line 344 of file template_formatter.h.
Referenced by resolve_placeholder().

|
inlinenodiscardoverridevirtual |
Get formatter name.
Implements kcenon::logger::log_formatter_interface.
Definition at line 151 of file template_formatter.h.
|
inlinenodiscard |
Get current template pattern.
Definition at line 161 of file template_formatter.h.
References template_.
|
inlineprivate |
Parse template string into segments.
Breaks down the template into literal text segments and placeholder segments for efficient formatting.
Definition at line 200 of file template_formatter.h.
References segments_, and template_.
Referenced by set_template().

|
inlinenodiscardprivate |
Resolve a placeholder to its actual value.
| name | Placeholder name |
| width | Optional field width |
| entry | Log entry to extract value from |
Definition at line 257 of file template_formatter.h.
References calculate_display_width(), kcenon::logger::log_entry::category, kcenon::logger::utils::string_utils::color_reset(), kcenon::logger::utils::string_utils::extract_filename(), kcenon::logger::log_entry::fields, format_field_value(), kcenon::logger::utils::time_utils::format_iso8601(), kcenon::logger::utils::time_utils::format_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::otel_ctx, kcenon::logger::log_entry::thread_id, kcenon::logger::log_entry::timestamp, kcenon::logger::utils::string_utils::to_lower(), kcenon::logger::small_string< SSO_SIZE >::to_string(), and kcenon::logger::format_options::use_colors.
Referenced by format().


|
inline |
Set a new template pattern.
| template_pattern | New template string |
Definition at line 173 of file template_formatter.h.
References parse_template(), and template_.
Referenced by template_formatter_example().


|
staticconstexpr |
Default template pattern.
Definition at line 92 of file template_formatter.h.
|
private |
Parsed template segments.
Definition at line 192 of file template_formatter.h.
Referenced by format(), and parse_template().
|
private |
Original template string.
Definition at line 191 of file template_formatter.h.
Referenced by get_template(), parse_template(), and set_template().