Skip to content

Commit

Permalink
✨ Add CIB_LOG_VERSION
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
elbeno committed Apr 8, 2024
1 parent bc0e936 commit bdf36f4
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 29 deletions.
4 changes: 1 addition & 3 deletions docs/logging.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -35,8 +35,6 @@ enum level {
};
----

NOTE: For backward compatibility, this is an unscoped enumeration.

=== Log macros

_cib_ log macros follow the log levels:
Expand Down
5 changes: 3 additions & 2 deletions include/log/catalog/mipi_encoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ template <typename TDestinations> struct log_handler {

template <auto Version, stdx::ct_string S = ""> 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<std::uint32_t>(((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(
Expand Down
2 changes: 1 addition & 1 deletion include/log/fmt/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ template <auto L> struct fmt::formatter<logging::level_constant<L>> {

template <typename FormatContext>
auto format(logging::level_constant<L>, FormatContext &ctx) {
return ::fmt::format_to(ctx.out(), to_text(L));
return ::fmt::format_to(ctx.out(), logging::to_text<L>());
}
};

Expand Down
29 changes: 13 additions & 16 deletions include/log/level.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#pragma once

#include <stdx/type_traits.hpp>

#include <array>
#include <cstdint>
#include <string_view>
#include <type_traits>

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,
Expand All @@ -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 <level L>
[[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 <level L> struct level_constant : std::integral_constant<level, L> {};
Expand Down
31 changes: 31 additions & 0 deletions include/log/log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,22 @@
#include <sc/format.hpp>
#include <sc/fwd.hpp>

#include <stdx/ct_string.hpp>
#include <stdx/panic.hpp>

#include <cstdint>
#include <utility>

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 <typename...> inline auto config = null::config{};
} // namespace version

namespace logging {
namespace null {
struct config {
Expand Down Expand Up @@ -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 <typename... Ts> static auto log_version() -> void {
auto &l_cfg = config<Ts...>;
auto &v_cfg = ::version::config<Ts...>;
if constexpr (requires {
l_cfg.logger.template log_build<v_cfg.build_id,
v_cfg.version_string>();
}) {
l_cfg.logger.template log_build<v_cfg.build_id, v_cfg.version_string>();
} else {
CIB_LOG(level::MAX, "Version: {} ({})", sc::uint_<v_cfg.build_id>,
stdx::ct_string_to_type<v_cfg.version_string,
sc::string_constant>());
}
}
} // namespace logging

#define CIB_LOG_VERSION() logging::log_version()
4 changes: 2 additions & 2 deletions include/msg/field_matchers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,15 @@ struct rel_matcher_t {
[[nodiscard]] constexpr auto describe() const {
return format("{} {} 0x{:x}"_sc, Field::name,
detail::to_string<RelOp>(),
sc::int_<static_cast<std::uint32_t>(ExpectedValue)>);
sc::uint_<static_cast<std::uint32_t>(ExpectedValue)>);
}

template <typename MsgType>
[[nodiscard]] constexpr auto describe_match(MsgType const &msg) const {
return format("{} (0x{:x}) {} 0x{:x}"_sc, Field::name,
static_cast<std::uint32_t>(extract_field(msg)),
detail::to_string<RelOp>(),
sc::int_<static_cast<std::uint32_t>(ExpectedValue)>);
sc::uint_<static_cast<std::uint32_t>(ExpectedValue)>);
}

private:
Expand Down
3 changes: 2 additions & 1 deletion include/sc/format.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ template <typename Fmt> 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<int>(std::distance(std::begin(fmt), spec_end) + 1);
return std::pair{fmt.substr(int_<0>, int_<len>), fmt.substr(int_<len>)};
}
}
Expand Down
9 changes: 6 additions & 3 deletions include/sc/fwd.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#pragma once

#include <concepts>
#include <type_traits>

namespace sc {
template <int value> constexpr static std::integral_constant<int, value> int_{};
template <std::signed_integral auto value>
constexpr static std::integral_constant<decltype(value), value> int_{};

template <unsigned int value>
constexpr static std::integral_constant<unsigned int, value> uint_{};
template <std::unsigned_integral auto value>
constexpr static std::integral_constant<decltype(value), value> uint_{};

template <bool value>
constexpr static std::integral_constant<bool, value> bool_{};
Expand All @@ -15,6 +17,7 @@ template <char value>
constexpr static std::integral_constant<char, value> char_{};

template <auto enumValue>
requires std::is_enum_v<decltype(enumValue)>
constexpr static std::integral_constant<decltype(enumValue), enumValue> enum_{};

template <typename T> struct type_name {
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 31 additions & 1 deletion test/log/fmt_logger.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <log/fmt/logger.hpp>

#include <stdx/ct_string.hpp>

#include <catch2/catch_test_macros.hpp>

#include <atomic>
Expand Down Expand Up @@ -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<logging::level::MAX>{});
CHECK(level == "UNKNOWN");
CHECK(level == "MAX");
}
{
std::string level{};
fmt::format_to(std::back_inserter(level), "{}",
logging::level_constant<logging::level::USER1>{});
CHECK(level == "USER1");
}
{
std::string level{};
fmt::format_to(std::back_inserter(level), "{}",
logging::level_constant<logging::level::USER2>{});
CHECK(level == "USER2");
}
}

Expand Down Expand Up @@ -120,3 +134,19 @@ TEST_CASE("logging can use std::cout", "[fmt_logger]") {
[[maybe_unused]] auto cfg =
logging::fmt::config{std::ostream_iterator<char>{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);
}
33 changes: 33 additions & 0 deletions test/log/mipi_logger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <log/catalog/mipi_encoder.hpp>

#include <stdx/ct_string.hpp>

#include <catch2/catch_test_macros.hpp>

#include <cstdint>

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 <auto Header, auto... ExpectedArgs>
struct test_log_version_destination {
template <typename... Args>
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(); }

0 comments on commit bdf36f4

Please sign in to comment.