4
4
#include < log/log.hpp>
5
5
#include < log/module.hpp>
6
6
7
+ #include < stdx/bit.hpp>
7
8
#include < stdx/ct_format.hpp>
8
- #include < stdx/tuple.hpp>
9
9
#include < stdx/tuple_algorithms.hpp>
10
- #include < stdx/utility .hpp>
10
+ #include < stdx/type_traits .hpp>
11
11
12
12
#include < fmt/format.h>
13
13
14
+ #include < array>
14
15
#include < chrono>
15
- #include < type_traits>
16
+ #include < iterator>
17
+ #include < string_view>
16
18
#include < utility>
17
19
20
+ namespace logging {
21
+ template <auto > struct level_wrapper {};
22
+
23
+ namespace fmt_detail {
24
+ using namespace std ::string_view_literals;
25
+ constexpr std::array level_text{" MAX" sv, " FATAL" sv, " ERROR" sv, " WARN" sv,
26
+ " INFO" sv, " USER1" sv, " USER2" sv, " TRACE" sv};
27
+ } // namespace fmt_detail
28
+
18
29
template <logging::level L>
19
- struct fmt ::formatter<std::integral_constant<logging::level, L>> {
20
- constexpr static auto parse (format_parse_context &ctx) {
21
- return ctx.begin ();
22
- }
30
+ constexpr std::string_view level_text =
31
+ fmt_detail::level_text[stdx::to_underlying(L)];
23
32
24
- template <typename FormatContext>
25
- auto format (std::integral_constant<logging::level, L>,
26
- FormatContext &ctx) const {
27
- return ::fmt::format_to (ctx.out (), logging::to_text<L>());
28
- }
29
- };
33
+ template <logging::level L>
34
+ [[nodiscard]] constexpr auto format_as (level_wrapper<L>) -> std::string_view {
35
+ return level_text<L>;
36
+ }
30
37
31
- namespace logging :: fmt {
38
+ namespace fmt {
32
39
template <typename TDestinations> struct log_handler {
33
40
constexpr explicit log_handler (TDestinations &&ds) : dests{std::move (ds)} {}
34
41
@@ -43,7 +50,8 @@ template <typename TDestinations> struct log_handler {
43
50
stdx::for_each (
44
51
[&](auto &out) {
45
52
::fmt::format_to (out, " {:>8}us {} [{}]: " , currentTime,
46
- get_level (Env{}), get_module(Env{}).value);
53
+ level_wrapper<get_level(Env{}).value>{},
54
+ get_module (Env{}).value );
47
55
msg.apply (
48
56
[&]<typename StringType>(StringType, auto const &...args ) {
49
57
::fmt::format_to (out, StringType::value, args...);
@@ -65,4 +73,5 @@ template <typename... TDestinations> struct config {
65
73
66
74
log_handler<destinations_tuple_t > logger;
67
75
};
68
- } // namespace logging::fmt
76
+ } // namespace fmt
77
+ } // namespace logging
0 commit comments