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
4
7
8#include "ILogFormatter.hpp"
10#include <time_shield/time_conversions.hpp>
11
12namespace logit {
13
20 public:
23 struct Config {
24 std::string pattern = "[%Y-%m-%d %H:%M:%S.%e] [%ffn:%#] [%!] [thread:%t] [%l] %^%v%$";
25 bool json_format = false;
26 };
27
32
36 SimpleLogFormatter(const std::string& pattern, const bool json_format = false) {
37 set_pattern(pattern, json_format);
38 }
39
47 void set_pattern(const std::string& pattern, bool json_format = false) {
48 m_config.pattern = pattern;
49 m_config.json_format = json_format;
51 }
52
59 void set_timestamp_offset(int64_t offset_ms) override {
60 m_offset_ms = offset_ms;
61 }
62
70 std::string format(const LogRecord& record) const override {
71 if (m_config.json_format) {
72 return format_as_json(record);
73 } else {
74 return format_as_pattern(record);
75 }
76 }
77
78 private:
80 std::vector<FormatInstruction> m_compiled_instructions;
81 std::atomic<int64_t> m_offset_ms = ATOMIC_VAR_INIT(0);
82
90
98 std::string format_as_pattern(const LogRecord& record) const {
99 auto dt = time_shield::to_date_time_ms<time_shield::DateTimeStruct>(record.timestamp_ms + m_offset_ms);
100 std::ostringstream oss;
101 for (const auto& instruction : m_compiled_instructions) {
102 instruction.apply(oss, record, dt);
103 }
104 return oss.str();
105 }
106
113 std::string format_as_json(const LogRecord& record) const {
114 std::ostringstream oss;
115 oss << "{"
116 << "\"log_level\": " << static_cast<int>(record.log_level) << ", "
117 << "\"timestamp_ms\": " << record.timestamp_ms << ", "
118 << "\"file\": \"" << escape_json_string(record.file) << "\", "
119 << "\"line\": " << record.line << ", "
120 << "\"function\": \"" << escape_json_string(record.function) << "\", "
121 << "\"format\": \"" << escape_json_string(record.format) << "\", "
122 << "\"arg_names\": \"" << escape_json_string(record.arg_names) << "\", "
123 << "\"args_array\": [";
124
125 for (size_t i = 0; i < record.args_array.size(); ++i) {
126 if (i > 0) oss << ", ";
127 oss << "\"" << escape_json_string(record.args_array[i].to_string()) << "\""; // Assuming to_string()
128 }
129
130 oss << "], "
131 << "\"thread_id\": \"" << escape_json_string(thread_id_to_string(record.thread_id)) << "\""
132 << "}";
133
134 return oss.str();
135 }
136
144 std::string escape_json_string(const std::string& input) const {
145 std::ostringstream oss;
146 for (char c : input) {
147 switch (c) {
148 case '"': oss << "\\\""; break;
149 case '\\': oss << "\\\\"; break;
150 case '\b': oss << "\\b"; break;
151 case '\f': oss << "\\f"; break;
152 case '\n': oss << "\\n"; break;
153 case '\r': oss << "\\r"; break;
154 case '\t': oss << "\\t"; break;
155 default:
156 if ('\x00' <= c && c <= '\x1f') {
157 oss << "\\u"
158 << std::hex << std::setw(4) << std::setfill('0') << static_cast<int>(c);
159 } else {
160 oss << c;
161 }
162 }
163 }
164 return oss.str();
165 }
166
173 std::string thread_id_to_string(const std::thread::id& thread_id) const {
174 std::ostringstream oss;
175 oss << thread_id;
176 return oss.str();
177 }
178 }; // class SimpleLogFormatter
179
180}; // namespace logit
181
182#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:14
const std::string function
Function name.
Definition LogRecord.hpp:19
const int line
Line number in the source file.
Definition LogRecord.hpp:18
std::thread::id thread_id
ID of the logging thread.
Definition LogRecord.hpp:23
const LogLevel log_level
Log level (severity).
Definition LogRecord.hpp:15
const int64_t timestamp_ms
Timestamp in milliseconds.
Definition LogRecord.hpp:16
const std::string file
Source file name.
Definition LogRecord.hpp:17
const std::string format
Format string for the message.
Definition LogRecord.hpp:20
std::vector< VariableValue > args_array
Argument values for the log.
Definition LogRecord.hpp:22
const std::string arg_names
Argument names for the log.
Definition LogRecord.hpp:21
Configuration for the log formatter.
bool json_format
Flag to enable JSON formatting.
std::string pattern
Default log format string.