RSTools 3.1.0
Versatile C++20 utility library
Loading...
Searching...
No Matches
Formatter.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <string>
5#include <string_view>
6#include <vector>
7
8namespace RST::Log {
9
10 struct LogMessage;
11
17 class Formatter {
18 public:
19 static constexpr std::string_view DEFAULT_PATTERN = "[%Y-%m-%d %H:%M:%S.%e] [%l] [%n] %v";
20
21 explicit Formatter(std::string_view pattern = DEFAULT_PATTERN);
22
24 void format(const LogMessage& message, std::string& dest) const;
25 [[nodiscard]] std::string format(const LogMessage& message) const;
26
27 void setPattern(std::string_view pattern);
28 [[nodiscard]] const std::string& getPattern() const noexcept { return _pattern; }
29
30 private:
31 enum class Token : std::uint8_t {
32 Literal,
33 Year, Month, Day,
34 Hour, Minute, Second, Millisecond,
35 LevelFull, LevelShort,
36 Category,
37 Message,
38 SourceFile, SourceFunc, SourceLine,
39 ThreadId, ProcessId,
40 };
41
42 struct Piece {
43 Token token = Token::Literal;
44 std::string literal;
45 };
46
47 void compile(std::string_view pattern);
48
49 std::string _pattern;
50 std::vector<Piece> _pieces;
51 bool _needsTime = false;
52 };
53
54}
Turns a LogMessage into a line of text following a pattern.
Definition Formatter.hpp:17
void format(const LogMessage &message, std::string &dest) const
Appends the formatted message to dest.
void setPattern(std::string_view pattern)
std::string format(const LogMessage &message) const
Formatter(std::string_view pattern=DEFAULT_PATTERN)
static constexpr std::string_view DEFAULT_PATTERN
Definition Formatter.hpp:19
const std::string & getPattern() const noexcept
Definition Formatter.hpp:28
Thread-safe logging: loggers, sinks, patterns and macros.
Definition Terminal.hpp:8
A log record as passed to the sinks.
Definition LogMessage.hpp:18