From bc0e9366f6d174956e5f6e1be98747919c48dde5 Mon Sep 17 00:00:00 2001 From: Ben Deane Date: Fri, 5 Apr 2024 10:41:09 -0600 Subject: [PATCH 1/2] :arrow_up: Update `stdx` to use `CX_VALUE` --- CMakeLists.txt | 2 +- include/lookup/input.hpp | 3 ++- test/lookup/cx_value.hpp | 10 ---------- test/lookup/direct_array.cpp | 4 ++-- test/lookup/fast_hash.cpp | 4 ++-- test/lookup/linear_search.cpp | 4 ++-- test/lookup/lookup.cpp | 3 +-- test/lookup/strategies.cpp | 3 +-- test/msg/indexed_handler.cpp | 12 +----------- 9 files changed, 12 insertions(+), 33 deletions(-) delete mode 100644 test/lookup/cx_value.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index fbcd7d24..c6d6a503 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,7 +29,7 @@ add_versioned_package( OPTIONS "FMT_INSTALL OFF" "FMT_OS OFF") -add_versioned_package("gh:intel/cpp-std-extensions#cff7a15") +add_versioned_package("gh:intel/cpp-std-extensions#1e7f7cd") add_versioned_package("gh:intel/cpp-baremetal-concurrency#8d49b6d") add_library(cib INTERFACE) diff --git a/include/lookup/input.hpp b/include/lookup/input.hpp index c02f102c..050c1421 100644 --- a/include/lookup/input.hpp +++ b/include/lookup/input.hpp @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -33,5 +34,5 @@ input(V, A) -> input input(V) -> input; template -concept compile_time = requires { typename T::cx_value_t; }; +concept compile_time = stdx::is_cx_value_v; } // namespace lookup diff --git a/test/lookup/cx_value.hpp b/test/lookup/cx_value.hpp deleted file mode 100644 index 2cc970d9..00000000 --- a/test/lookup/cx_value.hpp +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once - -#define CX_VALUE(...) \ - [] { \ - struct { \ - CONSTEVAL auto operator()() const noexcept { return __VA_ARGS__; } \ - using cx_value_t [[maybe_unused]] = void; \ - } val; \ - return val; \ - }() diff --git a/test/lookup/direct_array.cpp b/test/lookup/direct_array.cpp index 114cf958..13489ebb 100644 --- a/test/lookup/direct_array.cpp +++ b/test/lookup/direct_array.cpp @@ -1,8 +1,8 @@ -#include "cx_value.hpp" - #include #include +#include + #include namespace { diff --git a/test/lookup/fast_hash.cpp b/test/lookup/fast_hash.cpp index 9383be50..ac2d97c3 100644 --- a/test/lookup/fast_hash.cpp +++ b/test/lookup/fast_hash.cpp @@ -1,9 +1,9 @@ -#include "cx_value.hpp" - #include #include #include +#include + #include namespace { diff --git a/test/lookup/linear_search.cpp b/test/lookup/linear_search.cpp index 3ff05396..e4b21d68 100644 --- a/test/lookup/linear_search.cpp +++ b/test/lookup/linear_search.cpp @@ -1,8 +1,8 @@ -#include "cx_value.hpp" - #include #include +#include + #include namespace { diff --git a/test/lookup/lookup.cpp b/test/lookup/lookup.cpp index ac88d0dc..c5645f8a 100644 --- a/test/lookup/lookup.cpp +++ b/test/lookup/lookup.cpp @@ -1,10 +1,9 @@ -#include "cx_value.hpp" - #include #include #include #include +#include #include diff --git a/test/lookup/strategies.cpp b/test/lookup/strategies.cpp index 67825377..9235305b 100644 --- a/test/lookup/strategies.cpp +++ b/test/lookup/strategies.cpp @@ -1,5 +1,3 @@ -#include "cx_value.hpp" - #include #include #include @@ -7,6 +5,7 @@ #include #include +#include #include diff --git a/test/msg/indexed_handler.cpp b/test/msg/indexed_handler.cpp index ecf450ed..46db2df3 100644 --- a/test/msg/indexed_handler.cpp +++ b/test/msg/indexed_handler.cpp @@ -6,6 +6,7 @@ #include #include +#include #include @@ -13,15 +14,6 @@ #include #include -#define CX_VALUE(...) \ - [] { \ - struct { \ - CONSTEVAL auto operator()() const noexcept { return __VA_ARGS__; } \ - using cx_value_t [[maybe_unused]] = void; \ - } val; \ - return val; \ - }() - namespace { using namespace msg; @@ -164,5 +156,3 @@ TEST_CASE("create handler with extra callback arg", "[indexed_handler]") { CHECK(h.is_match(test_msg{"opcode_field"_field = 42})); CHECK(callbacks_called[1]); } - -#undef CX_VALUE From bdf36f4711d4e65d04ce6a671a722ef8fdadd61b Mon Sep 17 00:00:00 2001 From: Ben Deane Date: Fri, 5 Apr 2024 15:57:51 -0600 Subject: [PATCH 2/2] :sparkles: Add `CIB_LOG_VERSION` - Injection of version ID and optional string. - If the logger provides log_build, call that; else call the regular CIB_LOG at max level to output the version info. --- docs/logging.adoc | 4 +--- include/log/catalog/mipi_encoder.hpp | 5 +++-- include/log/fmt/logger.hpp | 2 +- include/log/level.hpp | 29 +++++++++++------------- include/log/log.hpp | 31 ++++++++++++++++++++++++++ include/msg/field_matchers.hpp | 4 ++-- include/sc/format.hpp | 3 ++- include/sc/fwd.hpp | 9 +++++--- test/CMakeLists.txt | 1 + test/log/fmt_logger.cpp | 32 ++++++++++++++++++++++++++- test/log/mipi_logger.cpp | 33 ++++++++++++++++++++++++++++ 11 files changed, 124 insertions(+), 29 deletions(-) create mode 100644 test/log/mipi_logger.cpp diff --git a/docs/logging.adoc b/docs/logging.adoc index a5935273..dbadc135 100644 --- a/docs/logging.adoc +++ b/docs/logging.adoc @@ -23,7 +23,7 @@ _cib_ offers 6 well-known and 2 user-defined log levels, according to the https: [source,cpp] ---- -enum level { +enum struct level { MAX = 0, FATAL = 1, ERROR = 2, @@ -35,8 +35,6 @@ enum level { }; ---- -NOTE: For backward compatibility, this is an unscoped enumeration. - === Log macros _cib_ log macros follow the log levels: diff --git a/include/log/catalog/mipi_encoder.hpp b/include/log/catalog/mipi_encoder.hpp index 81abdda3..d8d9ab8a 100644 --- a/include/log/catalog/mipi_encoder.hpp +++ b/include/log/catalog/mipi_encoder.hpp @@ -68,8 +68,9 @@ template struct log_handler { template auto log_build() -> void { if constexpr (S.empty() and stdx::bit_width(Version) <= 22) { - dispatch_pass_by_args(((Version & 0x3'00'00'0u) << 10u) | - ((Version & 0xff'ff'fu) << 4u)); + dispatch_pass_by_args( + static_cast(((Version & 0x3'00'00'0u) << 10u) | + ((Version & 0xff'ff'fu) << 4u))); } else if constexpr (S.empty() and stdx::bit_width(Version) <= 54) { constexpr auto subtype = 0x1u; // compact64 dispatch_pass_by_args( diff --git a/include/log/fmt/logger.hpp b/include/log/fmt/logger.hpp index 569633e7..3e10b90a 100644 --- a/include/log/fmt/logger.hpp +++ b/include/log/fmt/logger.hpp @@ -17,7 +17,7 @@ template struct fmt::formatter> { template auto format(logging::level_constant, FormatContext &ctx) { - return ::fmt::format_to(ctx.out(), to_text(L)); + return ::fmt::format_to(ctx.out(), logging::to_text()); } }; diff --git a/include/log/level.hpp b/include/log/level.hpp index 9006e08e..79b3a126 100644 --- a/include/log/level.hpp +++ b/include/log/level.hpp @@ -1,11 +1,15 @@ #pragma once +#include + +#include +#include #include #include namespace logging { // enum assignment is according to Mipi_Sys-T Severity definition -enum level { +enum struct level : std::uint8_t { MAX = 0, FATAL = 1, ERROR = 2, @@ -16,21 +20,14 @@ enum level { TRACE = 7 }; -[[nodiscard]] constexpr auto to_text(level l) -> std::string_view { - switch (l) { - case level::TRACE: - return "TRACE"; - case level::INFO: - return "INFO"; - case level::WARN: - return "WARN"; - case level::ERROR: - return "ERROR"; - case level::FATAL: - return "FATAL"; - default: - return "UNKNOWN"; - } +template +[[nodiscard]] constexpr auto to_text() -> std::string_view + requires(L <= level::TRACE) +{ + using namespace std::string_view_literals; + constexpr std::array level_text{"MAX"sv, "FATAL"sv, "ERROR"sv, "WARN"sv, + "INFO"sv, "USER1"sv, "USER2"sv, "TRACE"sv}; + return level_text[stdx::to_underlying(L)]; } template struct level_constant : std::integral_constant {}; diff --git a/include/log/log.hpp b/include/log/log.hpp index 8f143af3..e2755f12 100644 --- a/include/log/log.hpp +++ b/include/log/log.hpp @@ -4,10 +4,22 @@ #include #include +#include #include +#include #include +namespace version { +namespace null { +struct config { + constexpr static auto build_id = std::uint64_t{}; + constexpr static auto version_string = stdx::ct_string{""}; +}; +} // namespace null +template inline auto config = null::config{}; +} // namespace version + namespace logging { namespace null { struct config { @@ -67,3 +79,22 @@ using cib_log_module_id_t = typename logging::module_id_t<"default">::type; #define CIB_ASSERT(expr) \ ((expr) ? void(0) : CIB_FATAL("Assertion failure: " #expr)) + +namespace logging { +template static auto log_version() -> void { + auto &l_cfg = config; + auto &v_cfg = ::version::config; + if constexpr (requires { + l_cfg.logger.template log_build(); + }) { + l_cfg.logger.template log_build(); + } else { + CIB_LOG(level::MAX, "Version: {} ({})", sc::uint_, + stdx::ct_string_to_type()); + } +} +} // namespace logging + +#define CIB_LOG_VERSION() logging::log_version() diff --git a/include/msg/field_matchers.hpp b/include/msg/field_matchers.hpp index 187a132f..06091cb5 100644 --- a/include/msg/field_matchers.hpp +++ b/include/msg/field_matchers.hpp @@ -140,7 +140,7 @@ struct rel_matcher_t { [[nodiscard]] constexpr auto describe() const { return format("{} {} 0x{:x}"_sc, Field::name, detail::to_string(), - sc::int_(ExpectedValue)>); + sc::uint_(ExpectedValue)>); } template @@ -148,7 +148,7 @@ struct rel_matcher_t { return format("{} (0x{:x}) {} 0x{:x}"_sc, Field::name, static_cast(extract_field(msg)), detail::to_string(), - sc::int_(ExpectedValue)>); + sc::uint_(ExpectedValue)>); } private: diff --git a/include/sc/format.hpp b/include/sc/format.hpp index c4c4c085..0827901e 100644 --- a/include/sc/format.hpp +++ b/include/sc/format.hpp @@ -63,7 +63,8 @@ template constexpr auto split_format_spec() { } else { constexpr auto spec_end = std::find_if(spec_start, std::end(fmt), [](auto c) { return c == '}'; }); - constexpr auto len = std::distance(std::begin(fmt), spec_end) + 1; + constexpr auto len = + static_cast(std::distance(std::begin(fmt), spec_end) + 1); return std::pair{fmt.substr(int_<0>, int_), fmt.substr(int_)}; } } diff --git a/include/sc/fwd.hpp b/include/sc/fwd.hpp index 408f373e..a3de1e82 100644 --- a/include/sc/fwd.hpp +++ b/include/sc/fwd.hpp @@ -1,12 +1,14 @@ #pragma once +#include #include namespace sc { -template constexpr static std::integral_constant int_{}; +template +constexpr static std::integral_constant int_{}; -template -constexpr static std::integral_constant uint_{}; +template +constexpr static std::integral_constant uint_{}; template constexpr static std::integral_constant bool_{}; @@ -15,6 +17,7 @@ template constexpr static std::integral_constant char_{}; template + requires std::is_enum_v constexpr static std::integral_constant enum_{}; template struct type_name { diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index bc661883..b92c9927 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -39,6 +39,7 @@ add_tests( log/fmt_logger log/log log/mipi_encoder + log/mipi_logger log/module_id lookup/direct_array lookup/fast_hash diff --git a/test/log/fmt_logger.cpp b/test/log/fmt_logger.cpp index 347db3f1..38659cd8 100644 --- a/test/log/fmt_logger.cpp +++ b/test/log/fmt_logger.cpp @@ -1,5 +1,7 @@ #include +#include + #include #include @@ -90,7 +92,19 @@ TEST_CASE("log levels are properly represented", "[fmt_logger]") { std::string level{}; fmt::format_to(std::back_inserter(level), "{}", logging::level_constant{}); - CHECK(level == "UNKNOWN"); + CHECK(level == "MAX"); + } + { + std::string level{}; + fmt::format_to(std::back_inserter(level), "{}", + logging::level_constant{}); + CHECK(level == "USER1"); + } + { + std::string level{}; + fmt::format_to(std::back_inserter(level), "{}", + logging::level_constant{}); + CHECK(level == "USER2"); } } @@ -120,3 +134,19 @@ TEST_CASE("logging can use std::cout", "[fmt_logger]") { [[maybe_unused]] auto cfg = logging::fmt::config{std::ostream_iterator{std::cout}}; } + +namespace { +struct version_config { + constexpr static auto build_id = std::uint64_t{1234}; + constexpr static auto version_string = stdx::ct_string{"test version"}; +}; +} // namespace +template <> inline auto version::config<> = version_config{}; + +TEST_CASE("log version", "[fmt_logger]") { + buffer.clear(); + CIB_LOG_VERSION(); + CAPTURE(buffer); + CHECK(buffer.find("MAX [default]: Version: 1234 (test version)") != + std::string::npos); +} diff --git a/test/log/mipi_logger.cpp b/test/log/mipi_logger.cpp new file mode 100644 index 00000000..cfa3fbcd --- /dev/null +++ b/test/log/mipi_logger.cpp @@ -0,0 +1,33 @@ +#include + +#include + +#include + +#include + +namespace { +struct version_config { + constexpr static auto build_id = std::uint64_t{0x3abcd5u}; + constexpr static auto version_string = stdx::ct_string{""}; +}; +} // namespace +template <> inline auto version::config<> = version_config{}; + +namespace { +template +struct test_log_version_destination { + template + auto log_by_args(std::uint32_t header, Args... args) { + CHECK(header == Header); + (check(args, ExpectedArgs), ...); + } +}; +} // namespace + +template <> +inline auto logging::config<> = logging::mipi::config{ + test_log_version_destination<0b11'000000'1010'1011'1100'1101'0101'0000u>{}}; +// 3 0 a b c d 5 + +TEST_CASE("log version", "[mipi_logger]") { CIB_LOG_VERSION(); }