6 #ifndef SL_LOG_STRING_PATTERN_HPP
7 #define SL_LOG_STRING_PATTERN_HPP
19 namespace sl::log::detail
21 struct IncNumberGenerator
23 unsigned minWidth = 0;
26 void operator()(std::ostream& stream)
28 stream << std::setfill(
'0') << std::setw(minWidth) << current++;
33 struct DateTimeGenerator
37 void operator()(std::ostream& stream)
const
39 const auto now = std::chrono::system_clock::now();
40 auto t_c = std::chrono::system_clock::to_time_t(now);
41 stream << std::put_time(std::localtime(&t_c), token.c_str());
45 struct StringGenerator
49 void operator()(std::ostream& stream)
const
55 using Generator = std::variant<StringGenerator, IncNumberGenerator, DateTimeGenerator>;
57 [[nodiscard]]
inline Generator makeGeneratorFromMatch(std::string_view token)
59 if (token.starts_with(
'%'))
72 return DateTimeGenerator{ { std::cbegin(token), std::cbegin(token) + 2 } };
75 token.remove_prefix(1);
77 std::from_chars(token.data(), &token.back(), width);
78 return IncNumberGenerator{ .minWidth = width };
83 return StringGenerator{ token };
86 [[nodiscard]]
inline std::vector<Generator> makeTokenGeneratorsFromPatternString(std::string_view patternString)
88 std::vector<Generator> generators;
91 const std::regex regEx{
"%(Y|m|d|H|M|S|j|\\d*N)" };
93 std::cregex_token_iterator
96 patternString.data() + std::size(patternString),
100 std::cregex_token_iterator{},
101 [&generators](
const auto& match)
103 if (match.length() != 0)
105 generators.emplace_back(makeGeneratorFromMatch({ match.first, match.second }));
133 m_TokenGenerators{ detail::makeTokenGeneratorsFromPatternString(m_PatternString) }
144 std::ostringstream ss;
145 for (
auto& token : m_TokenGenerators)
148 [&ss](
auto& generator)
155 return std::move(ss).str();
165 return m_PatternString;
175 m_TokenGenerators = detail::makeTokenGeneratorsFromPatternString(m_PatternString);
179 std::string m_PatternString;
180 std::vector<detail::Generator> m_TokenGenerators;
Helper class for generating patterned strings.
Definition: StringPattern.hpp:120
void setPatternString(std::string patternString)
Sets the pattern string.
Definition: StringPattern.hpp:172
std::string_view patternString() const noexcept
Getter of the used pattern string.
Definition: StringPattern.hpp:163
StringPattern() noexcept=default
Default Constructor.
std::string next()
Creates a new string.
Definition: StringPattern.hpp:142
Definition: BasicSink.hpp:22