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