Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ include(cmake/string_catalog.cmake)
add_versioned_package("gh:boostorg/mp11#boost-1.83.0")
fmt_recipe(11.1.3)
add_versioned_package("gh:intel/cpp-baremetal-concurrency#7c5b26c")
add_versioned_package("gh:intel/cpp-std-extensions#7725142")
add_versioned_package("gh:intel/cpp-std-extensions#01be679")
add_versioned_package("gh:intel/cpp-baremetal-senders-and-receivers#0525974")

set(GEN_STR_CATALOG
Expand Down
60 changes: 53 additions & 7 deletions include/log/log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#include <stdx/ct_format.hpp>
#include <stdx/ct_string.hpp>
#include <stdx/panic.hpp>
#if __cpp_pack_indexing < 202311L
#include <stdx/tuple.hpp>
#endif
#include <stdx/type_traits.hpp>
#include <stdx/utility.hpp>

Expand Down Expand Up @@ -74,14 +77,57 @@ static auto log(TArgs &&...args) -> void {
#define CIB_ERROR(...) \
CIB_LOG_WITH_LEVEL(logging::level::ERROR __VA_OPT__(, ) __VA_ARGS__)

namespace logging::detail {
template <stdx::ct_string Fmt, typename Env, typename F, typename L>
[[nodiscard]] constexpr auto panic(F file, L line, auto &&...args) {
STDX_PRAGMA(diagnostic push)
#ifdef __clang__
STDX_PRAGMA(diagnostic ignored "-Wunknown-warning-option")
STDX_PRAGMA(diagnostic ignored "-Wc++26-extensions")
#endif

constexpr auto N = stdx::num_fmt_specifiers<Fmt>;
constexpr auto sz = sizeof...(args);
using env_t =
stdx::extend_env_t<Env, logging::get_level, logging::level::FATAL>;

#if __cpp_pack_indexing >= 202311L
auto s = [&]<std::size_t... Is>(std::index_sequence<Is...>) {
return stdx::ct_format<Fmt>(FWD(args...[Is])...);
}(std::make_index_sequence<N>{});
logging::log<env_t>(file, line, s);

[&]<std::size_t... Is>(std::index_sequence<Is...>) {
stdx::panic<decltype(s.str)::value>(std::move(s).args,
FWD(args...[N + Is])...);
}(std::make_index_sequence<sz - N>{});
#else
auto tup = stdx::make_tuple(FWD(args)...);
auto t = [&]<std::size_t... Is, std::size_t... Js>(
std::index_sequence<Is...>, std::index_sequence<Js...>) {
return stdx::make_tuple(
stdx::make_tuple(stdx::get<Is>(std::move(tup))...),
stdx::make_tuple(stdx::get<sizeof...(Is) + Js>(std::move(tup))...));
}(std::make_index_sequence<N>{}, std::make_index_sequence<sz - N>{});

auto s = stdx::get<0>(std::move(t)).apply([](auto &&...fmt_args) {
return stdx::ct_format<Fmt>(FWD(fmt_args)...);
});
logging::log<env_t>(file, line, s);

stdx::get<1>(std::move(t)).apply([&](auto &&...extra_args) {
stdx::panic<decltype(s.str)::value>(std::move(s).args,
FWD(extra_args)...);
});
#endif

STDX_PRAGMA(diagnostic pop)
}
} // namespace logging::detail

#define CIB_FATAL(MSG, ...) \
[](auto &&s) { \
CIB_LOG_ENV(logging::get_level, logging::level::FATAL); \
logging::log<cib_log_env_t>(__FILE__, __LINE__, s); \
FWD(s).args.apply([](auto &&...args) { \
stdx::panic<decltype(s.str)::value>(FWD(args)...); \
}); \
}(stdx::ct_format<MSG>(__VA_ARGS__))
logging::detail::panic<MSG, cib_log_env_t>( \
__FILE__, __LINE__ __VA_OPT__(, ) __VA_ARGS__)

#define CIB_ASSERT(expr) \
((expr) ? void(0) : CIB_FATAL("Assertion failure: " #expr))
Expand Down
32 changes: 23 additions & 9 deletions test/log/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@

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

#include <catch2/catch_test_macros.hpp>

#include <any>
#include <iterator>
#include <optional>
#include <string>
#include <string_view>
#include <tuple>

namespace {
bool panicked{};
std::string_view expected_why{};
std::optional<int> expected_arg{};
std::any expected_args{};

struct injected_handler {
template <stdx::ct_string Why, typename... Args>
Expand All @@ -24,11 +26,11 @@ struct injected_handler {
CAPTURE(s);
CHECK(s.ends_with(expected_why));
panicked = true;
if (expected_arg) {
CHECK(sizeof...(Args) == 1);
if constexpr (sizeof...(Args) == 1) {
CHECK(*expected_arg == (args, ...));
}

if (expected_args.has_value()) {
using expected_t = std::tuple<std::decay_t<Args>...>;
CHECK(std::any_cast<expected_t>(expected_args) ==
std::make_tuple(args...));
}
}
};
Expand All @@ -38,7 +40,7 @@ std::string buffer{};
auto reset_test_state() {
panicked = false;
expected_why = {};
expected_arg.reset();
expected_args.reset();
buffer.clear();
}
} // namespace
Expand Down Expand Up @@ -88,11 +90,23 @@ TEST_CASE("CIB_FATAL pre-formats arguments passed to panic", "[log]") {
TEST_CASE("CIB_FATAL can format stack arguments", "[log]") {
reset_test_state();
expected_why = "Hello {}";
expected_arg = 42;
expected_args = std::make_tuple(stdx::make_tuple(42));

auto x = 42;
CIB_FATAL("Hello {}", x);
CAPTURE(buffer);
CHECK(buffer.find("Hello 42") != std::string::npos);
CHECK(panicked);
}

TEST_CASE("CIB_FATAL passes extra arguments to panic", "[log]") {
reset_test_state();
expected_why = "Hello {}";
expected_args = std::make_tuple(stdx::make_tuple(42), 17);

auto x = 42;
CIB_FATAL("Hello {}", x, 17);
CAPTURE(buffer);
CHECK(buffer.find("Hello 42") != std::string::npos);
CHECK(panicked);
}
Loading