LogIt++
Loading...
Searching...
No Matches
SimpleLogFormatter.hpp
Go to the documentation of this file.
1#pragma once
2#ifndef _LOGIT_SIMPLE_LOG_FORMATTER_HPP_INCLUDED
3#define _LOGIT_SIMPLE_LOG_FORMATTER_HPP_INCLUDED
6
7#include "ILogFormatter.hpp"
9
10namespace logit {
11
18 public:
21 struct Config {
22 std::string pattern = "[%Y-%m-%d %H:%M:%S.%e] [%ffn:%#] [%!] [thread:%t] [%l] %^%v%$";
23 bool json_format = false;
24 };
25
30
34 SimpleLogFormatter(const std::string& pattern, const bool json_format = false) {
35 set_pattern(pattern, json_format);
36 }
37
45 void set_pattern(const std::string& pattern, bool json_format = false) {
46 m_config.pattern = pattern;
47 m_config.json_format = json_format;
49 }
50
58 std::string format(const LogRecord& record) const override {
60 return format_as_json(record);
61 } else {
62 return format_as_pattern(record);
63 }
64 }
65
66 private:
68 std::vector<FormatInstruction> m_compiled_instructions;
69
77
85 std::string format_as_pattern(const LogRecord& record) const {
86 auto dt = time_shield::to_date_time_ms<time_shield::DateTimeStruct>(record.timestamp_ms);
87 std::ostringstream oss;
88 for (const auto& instruction : m_compiled_instructions) {
89 instruction.apply(oss, record, dt);
90 }
91 return oss.str();
92 }
93
100 std::string format_as_json(const LogRecord& record) const {
101 std::ostringstream oss;
102 oss << "{"
103 << "\"log_level\": " << static_cast<int>(record.log_level) << ", "
104 << "\"timestamp_ms\": " << record.timestamp_ms << ", "
105 << "\"file\": \"" << escape_json_string(record.file) << "\", "
106 << "\"line\": " << record.line << ", "
107 << "\"function\": \"" << escape_json_string(record.function) << "\", "
108 << "\"format\": \"" << escape_json_string(record.format) << "\", "
109 << "\"arg_names\": \"" << escape_json_string(record.arg_names) << "\", "
110 << "\"args_array\": [";
111
112 for (size_t i = 0; i < record.args_array.size(); ++i) {
113 if (i > 0) oss << ", ";
114 oss << "\"" << escape_json_string(record.args_array[i].to_string()) << "\""; // Assuming to_string()
115 }
116
117 oss << "], "
118 << "\"thread_id\": \"" << escape_json_string(thread_id_to_string(record.thread_id)) << "\""
119 << "}";
120
121 return oss.str();
122 }
123
131 std::string escape_json_string(const std::string& input) const {
132 std::ostringstream oss;
133 for (char c : input) {
134 switch (c) {
135 case '"': oss << "\\\""; break;
136 case '\\': oss << "\\\\"; break;
137 case '\b': oss << "\\b"; break;
138 case '\f': oss << "\\f"; break;
139 case '\n': oss << "\\n"; break;
140 case '\r': oss << "\\r"; break;
141 case '\t': oss << "\\t"; break;
142 default:
143 if ('\x00' <= c && c <= '\x1f') {
144 oss << "\\u"
145 << std::hex << std::setw(4) << std::setfill('0') << static_cast<int>(c);
146 } else {
147 oss << c;
148 }
149 }
150 }
151 return oss.str();
152 }
153
160 std::string thread_id_to_string(const std::thread::id& thread_id) const {
161 std::ostringstream oss;
162 oss << thread_id;
163 return oss.str();
164 }
165 }; // class SimpleLogFormatter
166
167}; // namespace logit
168
169#endif // _LOGIT_SIMPLE_LOG_FORMATTER_HPP_INCLUDED
Defines the interface for log formatters used in the logging system.
Header file for the pattern compiler used in log formatting.
Interface for formatting log records.
static std::vector< FormatInstruction > compile(const std::string &pattern)
Compiles a pattern string into a list of format instructions.
A simple log formatter that formats log messages based on a user-defined pattern.
std::vector< FormatInstruction > m_compiled_instructions
Compiled instructions from the format pattern.
std::string thread_id_to_string(const std::thread::id &thread_id) const
Helper function to convert a thread ID to a string.
std::string format_as_pattern(const LogRecord &record) const
Formats a log record according to the compiled pattern.
SimpleLogFormatter(const std::string &pattern, const bool json_format=false)
Constructor that accepts a custom log pattern.
std::string format_as_json(const LogRecord &record) const
Formats a log record as a JSON string without external JSON libraries.
void set_pattern(const std::string &pattern, bool json_format=false)
Sets a custom pattern for log formatting or switches to JSON formatting.
void compile_pattern()
Compiles the log format pattern into instructions.
std::string escape_json_string(const std::string &input) const
Helper function to escape special characters in JSON strings.
Config m_config
Formatter configuration holding the log format pattern.
std::string format(const LogRecord &record) const override
Formats a log record according to the current pattern or as a JSON string.
SimpleLogFormatter()
Default constructor that uses the default log pattern.
The primary namespace for the LogIt++ library.
Stores log metadata and content.
Definition LogRecord.hpp:15
const std::string function
Function name.
Definition LogRecord.hpp:20
const int line
Line number in the source file.
Definition LogRecord.hpp:19
std::thread::id thread_id
ID of the logging thread.
Definition LogRecord.hpp:24
const LogLevel log_level
Log level (severity).
Definition LogRecord.hpp:16
const int64_t timestamp_ms
Timestamp in milliseconds.
Definition LogRecord.hpp:17
const std::string file
Source file name.
Definition LogRecord.hpp:18
const std::string format
Format string for the message.
Definition LogRecord.hpp:21
std::vector< VariableValue > args_array
Argument values for the log.
Definition LogRecord.hpp:23
const std::string arg_names
Argument names for the log.
Definition LogRecord.hpp:22
Configuration for the log formatter.
bool json_format
Flag to enable JSON formatting.
std::string pattern
Default log format string.