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
57 void set_timestamp_offset(int64_t offset_ms) override {
58 m_offset_ms = offset_ms;
59 }
60
68 std::string format(const LogRecord& record) const override {
69 if (m_config.json_format) {
70 return format_as_json(record);
71 } else {
72 return format_as_pattern(record);
73 }
74 }
75
76 private:
78 std::vector<FormatInstruction> m_compiled_instructions;
79 std::atomic<int64_t> m_offset_ms = ATOMIC_VAR_INIT(0);
80
88
96 std::string format_as_pattern(const LogRecord& record) const {
97 auto dt = time_shield::to_date_time_ms<time_shield::DateTimeStruct>(record.timestamp_ms + m_offset_ms);
98 std::ostringstream oss;
99 for (const auto& instruction : m_compiled_instructions) {
100 instruction.apply(oss, record, dt);
101 }
102 return oss.str();
103 }
104
111 std::string format_as_json(const LogRecord& record) const {
112 std::ostringstream oss;
113 oss << "{"
114 << "\"log_level\": " << static_cast<int>(record.log_level) << ", "
115 << "\"timestamp_ms\": " << record.timestamp_ms << ", "
116 << "\"file\": \"" << escape_json_string(record.file) << "\", "
117 << "\"line\": " << record.line << ", "
118 << "\"function\": \"" << escape_json_string(record.function) << "\", "
119 << "\"format\": \"" << escape_json_string(record.format) << "\", "
120 << "\"arg_names\": \"" << escape_json_string(record.arg_names) << "\", "
121 << "\"args_array\": [";
122
123 for (size_t i = 0; i < record.args_array.size(); ++i) {
124 if (i > 0) oss << ", ";
125 oss << "\"" << escape_json_string(record.args_array[i].to_string()) << "\""; // Assuming to_string()
126 }
127
128 oss << "], "
129 << "\"thread_id\": \"" << escape_json_string(thread_id_to_string(record.thread_id)) << "\""
130 << "}";
131
132 return oss.str();
133 }
134
142 std::string escape_json_string(const std::string& input) const {
143 std::ostringstream oss;
144 for (char c : input) {
145 switch (c) {
146 case '"': oss << "\\\""; break;
147 case '\\': oss << "\\\\"; break;
148 case '\b': oss << "\\b"; break;
149 case '\f': oss << "\\f"; break;
150 case '\n': oss << "\\n"; break;
151 case '\r': oss << "\\r"; break;
152 case '\t': oss << "\\t"; break;
153 default:
154 if ('\x00' <= c && c <= '\x1f') {
155 oss << "\\u"
156 << std::hex << std::setw(4) << std::setfill('0') << static_cast<int>(c);
157 } else {
158 oss << c;
159 }
160 }
161 }
162 return oss.str();
163 }
164
171 std::string thread_id_to_string(const std::thread::id& thread_id) const {
172 std::ostringstream oss;
173 oss << thread_id;
174 return oss.str();
175 }
176 }; // class SimpleLogFormatter
177
178}; // namespace logit
179
180#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, CompileContext context=CompileContext::Default)
Compiles a pattern string into a list of format instructions.
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 set_timestamp_offset(int64_t offset_ms) override
Sets the timestamp offset for log 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::atomic< int64_t > m_offset_ms
Timestamp offset in milliseconds.
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:13
const std::string function
Function name.
Definition LogRecord.hpp:18
const int line
Line number in the source file.
Definition LogRecord.hpp:17
std::thread::id thread_id
ID of the logging thread.
Definition LogRecord.hpp:22
const LogLevel log_level
Log level (severity).
Definition LogRecord.hpp:14
const int64_t timestamp_ms
Timestamp in milliseconds.
Definition LogRecord.hpp:15
const std::string file
Source file name.
Definition LogRecord.hpp:16
const std::string format
Format string for the message.
Definition LogRecord.hpp:19
std::vector< VariableValue > args_array
Argument values for the log.
Definition LogRecord.hpp:21
const std::string arg_names
Argument names for the log.
Definition LogRecord.hpp:20
Configuration for the log formatter.
bool json_format
Flag to enable JSON formatting.
std::string pattern
Default log format string.