Skip to content

Commit fbe7b99

Browse files
authored
Merge pull request #694 from elbeno/update-fmt
⬆️ Update fmt to 11.1.3
2 parents c649489 + d39a7b5 commit fbe7b99

File tree

5 files changed

+58
-54
lines changed

5 files changed

+58
-54
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ find_package(
2323
include(cmake/string_catalog.cmake)
2424

2525
add_versioned_package("gh:boostorg/mp11#boost-1.83.0")
26-
fmt_recipe(10.2.1)
26+
fmt_recipe(11.1.3)
2727
add_versioned_package("gh:intel/cpp-baremetal-concurrency#7c5b26c")
2828
add_versioned_package("gh:intel/cpp-std-extensions#37cc9c5")
2929
add_versioned_package("gh:intel/cpp-baremetal-senders-and-receivers#73d95bc")

include/log/fmt/logger.hpp

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,38 @@
44
#include <log/log.hpp>
55
#include <log/module.hpp>
66

7+
#include <stdx/bit.hpp>
78
#include <stdx/ct_format.hpp>
8-
#include <stdx/tuple.hpp>
99
#include <stdx/tuple_algorithms.hpp>
10-
#include <stdx/utility.hpp>
10+
#include <stdx/type_traits.hpp>
1111

1212
#include <fmt/format.h>
1313

14+
#include <array>
1415
#include <chrono>
15-
#include <type_traits>
16+
#include <iterator>
17+
#include <string_view>
1618
#include <utility>
1719

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+
1829
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)];
2332

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+
}
3037

31-
namespace logging::fmt {
38+
namespace fmt {
3239
template <typename TDestinations> struct log_handler {
3340
constexpr explicit log_handler(TDestinations &&ds) : dests{std::move(ds)} {}
3441

@@ -43,7 +50,8 @@ template <typename TDestinations> struct log_handler {
4350
stdx::for_each(
4451
[&](auto &out) {
4552
::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);
4755
msg.apply(
4856
[&]<typename StringType>(StringType, auto const &...args) {
4957
::fmt::format_to(out, StringType::value, args...);
@@ -65,4 +73,5 @@ template <typename... TDestinations> struct config {
6573

6674
log_handler<destinations_tuple_t> logger;
6775
};
68-
} // namespace logging::fmt
76+
} // namespace fmt
77+
} // namespace logging

include/log/level.hpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
#pragma once
22

3-
#include <log/env.hpp>
4-
53
#include <stdx/compiler.hpp>
6-
#include <stdx/type_traits.hpp>
74

8-
#include <array>
95
#include <cstdint>
10-
#include <string_view>
11-
#include <type_traits>
126
#include <utility>
137

148
namespace logging {
@@ -24,16 +18,6 @@ enum struct level : std::uint8_t {
2418
TRACE = 7
2519
};
2620

27-
template <level L>
28-
[[nodiscard]] constexpr auto to_text() -> std::string_view
29-
requires(L <= level::TRACE)
30-
{
31-
using namespace std::string_view_literals;
32-
constexpr std::array level_text{"MAX"sv, "FATAL"sv, "ERROR"sv, "WARN"sv,
33-
"INFO"sv, "USER1"sv, "USER2"sv, "TRACE"sv};
34-
return level_text[stdx::to_underlying(L)];
35-
}
36-
3721
[[maybe_unused]] constexpr inline struct get_level_t {
3822
template <typename T>
3923
CONSTEVAL auto operator()(T &&t) const noexcept(

test/log/fmt_logger.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,49 +63,49 @@ TEST_CASE("log levels are properly represented", "[fmt_logger]") {
6363
{
6464
std::string level{};
6565
fmt::format_to(std::back_inserter(level), "{}",
66-
stdx::ct<logging::level::TRACE>());
66+
logging::level_wrapper<logging::level::TRACE>{});
6767
CHECK(level == "TRACE");
6868
}
6969
{
7070
std::string level{};
7171
fmt::format_to(std::back_inserter(level), "{}",
72-
stdx::ct<logging::level::INFO>());
72+
logging::level_wrapper<logging::level::INFO>{});
7373
CHECK(level == "INFO");
7474
}
7575
{
7676
std::string level{};
7777
fmt::format_to(std::back_inserter(level), "{}",
78-
stdx::ct<logging::level::WARN>());
78+
logging::level_wrapper<logging::level::WARN>{});
7979
CHECK(level == "WARN");
8080
}
8181
{
8282
std::string level{};
8383
fmt::format_to(std::back_inserter(level), "{}",
84-
stdx::ct<logging::level::ERROR>());
84+
logging::level_wrapper<logging::level::ERROR>{});
8585
CHECK(level == "ERROR");
8686
}
8787
{
8888
std::string level{};
8989
fmt::format_to(std::back_inserter(level), "{}",
90-
stdx::ct<logging::level::FATAL>());
90+
logging::level_wrapper<logging::level::FATAL>{});
9191
CHECK(level == "FATAL");
9292
}
9393
{
9494
std::string level{};
9595
fmt::format_to(std::back_inserter(level), "{}",
96-
stdx::ct<logging::level::MAX>());
96+
logging::level_wrapper<logging::level::MAX>{});
9797
CHECK(level == "MAX");
9898
}
9999
{
100100
std::string level{};
101101
fmt::format_to(std::back_inserter(level), "{}",
102-
stdx::ct<logging::level::USER1>());
102+
logging::level_wrapper<logging::level::USER1>{});
103103
CHECK(level == "USER1");
104104
}
105105
{
106106
std::string level{};
107107
fmt::format_to(std::back_inserter(level), "{}",
108-
stdx::ct<logging::level::USER2>());
108+
logging::level_wrapper<logging::level::USER2>{});
109109
CHECK(level == "USER2");
110110
}
111111
}

test/log/level.cpp

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <cstdint>
1212
#include <iterator>
1313
#include <string>
14+
#include <string_view>
1415
#include <type_traits>
1516

1617
namespace {
@@ -19,23 +20,18 @@ enum struct custom_level { THE_ONE_LEVEL = 5 };
1920
std::string buffer{};
2021
} // namespace
2122

23+
namespace logging {
24+
template <custom_level L>
25+
[[nodiscard]] constexpr auto format_as(level_wrapper<L>) -> std::string_view {
26+
static_assert(L == custom_level::THE_ONE_LEVEL);
27+
return "THE_ONE_LEVEL";
28+
}
29+
} // namespace logging
30+
2231
template <>
2332
inline auto logging::config<> =
2433
logging::fmt::config{std::back_inserter(buffer)};
2534

26-
template <custom_level L>
27-
struct fmt::formatter<std::integral_constant<custom_level, L>> {
28-
constexpr static auto parse(format_parse_context &ctx) {
29-
return ctx.begin();
30-
}
31-
32-
template <typename FormatContext>
33-
auto format(std::integral_constant<custom_level, L>,
34-
FormatContext &ctx) const {
35-
return ::fmt::format_to(ctx.out(), stdx::enum_as_string<L>());
36-
}
37-
};
38-
3935
TEST_CASE("fmt logger works with custom level", "[level]") {
4036
CIB_LOG_ENV(logging::get_level, custom_level::THE_ONE_LEVEL);
4137
buffer.clear();
@@ -66,3 +62,18 @@ TEST_CASE("mipi logger works with custom level", "[level]") {
6662
cfg.logger.log_msg<cib_log_env_t>(sc::format("Hello {} {}"_sc, 17, 42));
6763
CHECK(log_calls == 1);
6864
}
65+
66+
namespace {
67+
constexpr auto MY_LEVEL = logging::level::USER1;
68+
}
69+
namespace logging {
70+
template <> constexpr std::string_view level_text<MY_LEVEL> = "MY_LEVEL";
71+
}
72+
73+
TEST_CASE("mipi logger USER level can be renamed", "[level]") {
74+
CIB_LOG_ENV(logging::get_level, MY_LEVEL);
75+
buffer.clear();
76+
CIB_LOG("Hello");
77+
CAPTURE(buffer);
78+
CHECK(buffer.find("MY_LEVEL [default]:") != std::string::npos);
79+
}

0 commit comments

Comments
 (0)