|
1 | 1 | #include <logging/impl/formatters/tskv.hpp> |
2 | 2 |
|
3 | | -#include <iostream> |
4 | | - |
5 | 3 | #include <fmt/chrono.h> |
6 | 4 | #include <fmt/compile.h> |
7 | 5 | #include <fmt/format.h> |
|
14 | 12 |
|
15 | 13 | USERVER_NAMESPACE_BEGIN |
16 | 14 |
|
| 15 | +namespace { |
| 16 | + |
| 17 | +template <class... Args> |
| 18 | +char* Append(char* output, const Args&... args) { |
| 19 | + const auto append_impl = [&output](std::string_view data) noexcept { |
| 20 | + std::memcpy(output, data.data(), data.size()); |
| 21 | + output += data.size(); |
| 22 | + }; |
| 23 | + (append_impl(args), ...); |
| 24 | + return output; |
| 25 | +}; |
| 26 | + |
| 27 | +} // namespace |
| 28 | + |
17 | 29 | namespace logging::impl::formatters { |
18 | 30 |
|
19 | 31 | Tskv::Tskv(Level level, Format format, const utils::impl::SourceLocation& location) : format_(format) { |
20 | 32 | switch (format) { |
21 | 33 | case Format::kTskv: { |
22 | | - constexpr std::string_view kTemplate = "tskv\ttimestamp=0000-00-00T00:00:00.000000\tlevel="; |
| 34 | + constexpr std::string_view kTemplate = "tskv\ttimestamp=0000-00-00T00:00:00.000000\tlevel=\tmodule= ( : )"; |
23 | 35 | const auto now = std::chrono::system_clock::now(); |
24 | 36 | const auto level_string = logging::ToUpperCaseString(level); |
25 | | - item_.log_line.resize(kTemplate.size() + level_string.size()); |
26 | | - fmt::format_to( |
27 | | - item_.log_line.data(), |
28 | | - FMT_COMPILE("tskv\ttimestamp={}.{:06}\tlevel={}"), |
29 | | - GetCurrentLocalTimeString(now).ToStringView(), |
30 | | - FractionalMicroseconds(now), |
31 | | - level_string |
32 | | - ); |
33 | | - fmt::format_to( |
34 | | - std::back_inserter(item_.log_line), |
35 | | - FMT_COMPILE("\tmodule={} ( {}:{} )"), |
36 | | - location.GetFunctionName(), |
37 | | - location.GetFileName(), |
38 | | - location.GetLineString() |
| 37 | + item_.log_line.resize_and_overwrite( |
| 38 | + kTemplate.size() + level_string.size() + location.GetFunctionName().size() + |
| 39 | + location.GetFileName().size() + location.GetLineString().size(), |
| 40 | + [&](char* output, std::size_t size) { |
| 41 | + output = Append(output, "tskv\ttimestamp=", GetCurrentLocalTimeString(now).ToStringView(), "."); |
| 42 | + output = fmt::format_to(output, FMT_COMPILE("{:06}"), FractionalMicroseconds(now)); |
| 43 | + output = Append(output, "\tlevel=", level_string, "\tmodule=", location.GetFunctionName()); |
| 44 | + output = Append(output, " ( ", location.GetFileName(), ":", location.GetLineString(), " )"); |
| 45 | + return size; |
| 46 | + } |
39 | 47 | ); |
40 | 48 | return; |
41 | 49 | } |
42 | 50 | case Format::kLtsv: { |
43 | | - constexpr std::string_view kTemplate = "timestamp:0000-00-00T00:00:00.000000\tlevel:"; |
| 51 | + constexpr std::string_view kTemplate = "timestamp:0000-00-00T00:00:00.000000\tlevel:\tmodule: ( : )"; |
44 | 52 | const auto now = TimePoint::clock::now(); |
45 | 53 | const auto level_string = logging::ToUpperCaseString(level); |
46 | | - item_.log_line.resize(kTemplate.size() + level_string.size()); |
47 | | - fmt::format_to( |
48 | | - item_.log_line.data(), |
49 | | - FMT_COMPILE("timestamp:{}.{:06}\tlevel:{}"), |
50 | | - GetCurrentLocalTimeString(now).ToStringView(), |
51 | | - FractionalMicroseconds(now), |
52 | | - level_string |
53 | | - ); |
54 | | - fmt::format_to( |
55 | | - std::back_inserter(item_.log_line), |
56 | | - FMT_COMPILE("\tmodule:{} ( {}:{} )"), |
57 | | - location.GetFunctionName(), |
58 | | - location.GetFileName(), |
59 | | - location.GetLineString() |
| 54 | + item_.log_line.resize_and_overwrite( |
| 55 | + kTemplate.size() + level_string.size() + location.GetFunctionName().size() + |
| 56 | + location.GetFileName().size() + location.GetLineString().size(), |
| 57 | + [&](char* output, std::size_t size) { |
| 58 | + output = Append(output, "timestamp:", GetCurrentLocalTimeString(now).ToStringView(), "."); |
| 59 | + output = fmt::format_to(output, FMT_COMPILE("{:06}"), FractionalMicroseconds(now)); |
| 60 | + output = Append(output, "\tlevel:", level_string, "\tmodule:", location.GetFunctionName()); |
| 61 | + output = Append(output, " ( ", location.GetFileName(), ":", location.GetLineString(), " )"); |
| 62 | + return size; |
| 63 | + } |
60 | 64 | ); |
61 | 65 | return; |
62 | 66 | } |
|
0 commit comments