Skip to content

Commit 5726ca9

Browse files
authored
Merge pull request #679 from elbeno/log-env
✨ Introduce log environment
2 parents 3545e85 + de2b4ae commit 5726ca9

File tree

14 files changed

+232
-75
lines changed

14 files changed

+232
-75
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ include(cmake/string_catalog.cmake)
2525
add_versioned_package("gh:boostorg/mp11#boost-1.83.0")
2626
fmt_recipe(10.2.1)
2727
add_versioned_package("gh:intel/cpp-baremetal-concurrency#7c5b26c")
28-
add_versioned_package("gh:intel/cpp-std-extensions#4d57b2e")
28+
add_versioned_package("gh:intel/cpp-std-extensions#2834680")
2929
add_versioned_package("gh:intel/cpp-baremetal-senders-and-receivers#73d95bc")
3030

3131
set(GEN_STR_CATALOG

include/cib/detail/runtime_conditional.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ operator and(runtime_condition<LhsName, LhsPs...> const &lhs,
8585
constexpr auto name =
8686
stdx::ct_format<"{} and {}">(CX_VALUE(LhsName), CX_VALUE(RhsName));
8787

88-
return runtime_condition<name, LhsPs..., RhsPs...>{};
88+
return runtime_condition<name.str.value, LhsPs..., RhsPs...>{};
8989
}
9090
}
9191

include/log/catalog/mipi_encoder.hpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <conc/concurrency.hpp>
44
#include <log/catalog/catalog.hpp>
55
#include <log/log.hpp>
6+
#include <log/module.hpp>
67
#include <msg/message.hpp>
78

89
#include <stdx/bit.hpp>
@@ -14,6 +15,7 @@
1415
#include <algorithm>
1516
#include <concepts>
1617
#include <cstdint>
18+
#include <string_view>
1719
#include <utility>
1820

1921
namespace logging::mipi {
@@ -28,11 +30,10 @@ constexpr auto to_message() {
2830
}(std::make_integer_sequence<std::size_t, std::size(s)>{});
2931
}
3032

31-
template <typename S> constexpr auto to_module() {
32-
constexpr auto s = S::value;
33-
using char_t = typename std::remove_cv_t<decltype(s)>::value_type;
33+
template <stdx::ct_string S> constexpr auto to_module() {
34+
constexpr auto s = std::string_view{S};
3435
return [&]<std::size_t... Is>(std::integer_sequence<std::size_t, Is...>) {
35-
return sc::module_string<sc::undefined<void, char_t, s[Is]...>>{};
36+
return sc::module_string<sc::undefined<void, char, s[Is]...>>{};
3637
}(std::make_integer_sequence<std::size_t, std::size(s)>{});
3738
}
3839
} // namespace detail
@@ -102,19 +103,19 @@ using catalog_msg_t =
102103
template <typename TDestinations> struct log_handler {
103104
constexpr explicit log_handler(TDestinations &&ds) : dests{std::move(ds)} {}
104105

105-
template <logging::level Level, typename ModuleId,
106-
typename FilenameStringType, typename LineNumberType,
107-
typename MsgType>
106+
template <logging::level Level, typename Env, typename FilenameStringType,
107+
typename LineNumberType, typename MsgType>
108108
ALWAYS_INLINE auto log(FilenameStringType, LineNumberType,
109109
MsgType const &msg) -> void {
110-
log_msg<Level, ModuleId>(msg);
110+
log_msg<Level, Env>(msg);
111111
}
112112

113-
template <logging::level Level, typename ModuleId, typename Msg>
113+
template <logging::level Level, typename Env, typename Msg>
114114
ALWAYS_INLINE auto log_msg(Msg msg) -> void {
115115
msg.apply([&]<typename S, typename... Args>(S, Args... args) {
116116
using Message = decltype(detail::to_message<Level, S, Args...>());
117-
using Module = decltype(detail::to_module<ModuleId>());
117+
using Module =
118+
decltype(detail::to_module<get_module(Env{}).value>());
118119
dispatch_message<Level>(catalog<Message>(), module<Module>(),
119120
static_cast<std::uint32_t>(args)...);
120121
});

include/log/env.hpp

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#pragma once
2+
3+
#include <stdx/compiler.hpp>
4+
#include <stdx/ct_string.hpp>
5+
#include <stdx/utility.hpp>
6+
7+
#include <boost/mp11/algorithm.hpp>
8+
9+
#ifdef __clang__
10+
#define CIB_PRAGMA_SEMI
11+
#else
12+
#define CIB_PRAGMA_SEMI ;
13+
#endif
14+
15+
namespace logging {
16+
template <auto Query, auto Value> struct prop {
17+
[[nodiscard]] CONSTEVAL static auto query(decltype(Query)) noexcept {
18+
return Value;
19+
}
20+
};
21+
22+
namespace detail {
23+
template <typename Q, typename Env>
24+
concept valid_query_for = requires { Env::query(Q{}); };
25+
26+
template <typename Q, typename... Envs>
27+
concept valid_query_over = (... or valid_query_for<Q, Envs>);
28+
29+
template <typename Q> struct has_query {
30+
template <typename Env>
31+
using fn = std::bool_constant<valid_query_for<Q, Env>>;
32+
};
33+
} // namespace detail
34+
35+
template <typename... Envs> struct env {
36+
template <detail::valid_query_over<Envs...> Q>
37+
CONSTEVAL static auto query(Q) noexcept {
38+
using I = boost::mp11::mp_find_if_q<boost::mp11::mp_list<Envs...>,
39+
detail::has_query<Q>>;
40+
using E = boost::mp11::mp_at<boost::mp11::mp_list<Envs...>, I>;
41+
return Q{}(E{});
42+
}
43+
};
44+
45+
namespace detail {
46+
template <typename T> struct autowrap {
47+
CONSTEVAL autowrap(T t) : value(t) {}
48+
T value;
49+
};
50+
51+
template <std::size_t N> using str_lit_t = char const (&)[N];
52+
53+
template <std::size_t N> struct autowrap<str_lit_t<N>> {
54+
CONSTEVAL autowrap(str_lit_t<N> str) : value(str) {}
55+
stdx::ct_string<N> value;
56+
};
57+
58+
template <typename T> autowrap(T) -> autowrap<T>;
59+
template <std::size_t N> autowrap(str_lit_t<N>) -> autowrap<str_lit_t<N>>;
60+
61+
template <auto V> struct wrap {
62+
constexpr static auto value = V;
63+
};
64+
65+
template <typename> struct for_each_pair;
66+
template <std::size_t... Is> struct for_each_pair<std::index_sequence<Is...>> {
67+
template <auto... Args>
68+
using type = env<
69+
prop<boost::mp11::mp_at_c<boost::mp11::mp_list<wrap<Args>...>,
70+
2 * Is>::value.value,
71+
stdx::ct<boost::mp11::mp_at_c<boost::mp11::mp_list<wrap<Args>...>,
72+
2 * Is + 1>::value.value>()>...>;
73+
};
74+
} // namespace detail
75+
} // namespace logging
76+
77+
using cib_log_env_t = logging::env<>;
78+
79+
// NOLINTBEGIN(cppcoreguidelines-macro-usage)
80+
#define CIB_LOG_ENV(...) \
81+
STDX_PRAGMA(diagnostic push) \
82+
STDX_PRAGMA(diagnostic ignored "-Wshadow") \
83+
using cib_log_env_t [[maybe_unused]] = \
84+
decltype([]<logging::detail::autowrap... Args> { \
85+
using new_env_t = typename logging::detail::for_each_pair< \
86+
std::make_index_sequence<sizeof...(Args) / \
87+
2>>::template type<Args...>; \
88+
return boost::mp11::mp_append<new_env_t, cib_log_env_t>{}; \
89+
}.template operator()<__VA_ARGS__>()) CIB_PRAGMA_SEMI \
90+
STDX_PRAGMA(diagnostic pop)
91+
// NOLINTEND(cppcoreguidelines-macro-usage)

include/log/fmt/logger.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#pragma once
22

33
#include <log/log.hpp>
4+
#include <log/module.hpp>
45

6+
#include <stdx/ct_format.hpp>
57
#include <stdx/tuple.hpp>
68
#include <stdx/tuple_algorithms.hpp>
79

@@ -25,7 +27,7 @@ namespace logging::fmt {
2527
template <typename TDestinations> struct log_handler {
2628
constexpr explicit log_handler(TDestinations &&ds) : dests{std::move(ds)} {}
2729

28-
template <logging::level L, typename ModuleId, typename FilenameStringType,
30+
template <logging::level L, typename Env, typename FilenameStringType,
2931
typename LineNumberType, typename MsgType>
3032
auto log(FilenameStringType, LineNumberType, MsgType const &msg) -> void {
3133
auto const currentTime =
@@ -36,7 +38,7 @@ template <typename TDestinations> struct log_handler {
3638
stdx::for_each(
3739
[&](auto &out) {
3840
::fmt::format_to(out, "{:>8}us {} [{}]: ", currentTime,
39-
level_constant<L>{}, ModuleId::value);
41+
level_constant<L>{}, get_module(Env{}).value);
4042
msg.apply(
4143
[&]<typename StringType>(StringType, auto const &...args) {
4244
::fmt::format_to(out, StringType::value, args...);

include/log/log.hpp

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#pragma once
22

3+
#include <log/env.hpp>
34
#include <log/level.hpp>
5+
#include <log/module.hpp>
46
#include <sc/format.hpp>
57
#include <sc/fwd.hpp>
68

@@ -44,37 +46,18 @@ ALWAYS_INLINE constexpr static auto get_config() -> auto & {
4446
}
4547
}
4648

47-
template <typename Flavor, level L, typename ModuleId, typename... Ts,
49+
template <typename Flavor, level L, typename Env, typename... Ts,
4850
typename... TArgs>
4951
ALWAYS_INLINE static auto log(TArgs &&...args) -> void {
5052
auto &cfg = get_config<Flavor, Ts...>();
51-
cfg.logger.template log<L, ModuleId>(std::forward<TArgs>(args)...);
53+
cfg.logger.template log<L, Env>(std::forward<TArgs>(args)...);
5254
}
53-
54-
template <stdx::ct_string S> struct module_id_t {
55-
using type = decltype(stdx::ct_string_to_type<S, sc::string_constant>());
56-
};
5755
} // namespace logging
5856

59-
using cib_log_module_id_t = typename logging::module_id_t<"default">::type;
60-
6157
// NOLINTBEGIN(cppcoreguidelines-macro-usage)
6258

63-
#ifdef __clang__
64-
#define CIB_PRAGMA_SEMI
65-
#else
66-
#define CIB_PRAGMA_SEMI ;
67-
#endif
68-
69-
#define CIB_LOG_MODULE(S) \
70-
STDX_PRAGMA(diagnostic push) \
71-
STDX_PRAGMA(diagnostic ignored "-Wshadow") \
72-
using cib_log_module_id_t [[maybe_unused]] = \
73-
typename logging::module_id_t<S>::type CIB_PRAGMA_SEMI STDX_PRAGMA( \
74-
diagnostic pop)
75-
7659
#define CIB_LOG(FLAVOR, LEVEL, MSG, ...) \
77-
logging::log<FLAVOR, LEVEL, cib_log_module_id_t>( \
60+
logging::log<FLAVOR, LEVEL, cib_log_env_t>( \
7861
__FILE__, __LINE__, sc::format(MSG##_sc __VA_OPT__(, ) __VA_ARGS__))
7962

8063
#define CIB_TRACE(...) \
@@ -90,7 +73,7 @@ using cib_log_module_id_t = typename logging::module_id_t<"default">::type;
9073
[] { \
9174
constexpr auto str = sc::format(MSG##_sc __VA_OPT__(, ) __VA_ARGS__); \
9275
logging::log<logging::default_flavor_t, logging::level::FATAL, \
93-
cib_log_module_id_t>(__FILE__, __LINE__, str); \
76+
cib_log_env_t>(__FILE__, __LINE__, str); \
9477
str.apply([]<typename S, typename... Args>(S s, Args... args) { \
9578
constexpr auto cts = stdx::ct_string_from_type(s); \
9679
stdx::panic<cts>(args...); \
@@ -111,7 +94,7 @@ ALWAYS_INLINE static auto log_version() -> void {
11194
}) {
11295
l_cfg.logger.template log_build<v_cfg.build_id, v_cfg.version_string>();
11396
} else {
114-
l_cfg.logger.template log<level::MAX, cib_log_module_id_t>(
97+
l_cfg.logger.template log<level::MAX, cib_log_env_t>(
11598
"", 0,
11699
sc::format("Version: {} ({})"_sc, sc::uint_<v_cfg.build_id>,
117100
stdx::ct_string_to_type<v_cfg.version_string,

include/log/module.hpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
3+
#include <log/env.hpp>
4+
5+
#include <stdx/ct_string.hpp>
6+
7+
#include <utility>
8+
9+
namespace logging {
10+
[[maybe_unused]] constexpr inline struct get_module_t {
11+
template <typename T>
12+
requires true // more constrained
13+
CONSTEVAL auto operator()(T &&t) const noexcept(
14+
noexcept(std::forward<T>(t).query(std::declval<get_module_t>())))
15+
-> decltype(std::forward<T>(t).query(*this)) {
16+
return std::forward<T>(t).query(*this);
17+
}
18+
19+
CONSTEVAL auto operator()(auto &&) const {
20+
using namespace stdx::literals;
21+
return "default"_ctst;
22+
}
23+
} get_module;
24+
} // namespace logging
25+
26+
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
27+
#define CIB_LOG_MODULE(S) CIB_LOG_ENV(logging::get_module, S)

test/log/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
add_tests(FILES log module_id LIBRARIES cib_log)
1+
add_tests(FILES log module_id env LIBRARIES cib_log)
22
add_tests(FILES fmt_logger LIBRARIES cib_log_fmt)
33
add_tests(FILES mipi_encoder mipi_logger LIBRARIES cib_log_mipi)
44

test/log/catalog1_lib.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,32 @@ auto log_with_fixed_module_id() -> void;
2727

2828
auto log_zero_args() -> void {
2929
auto cfg = logging::mipi::config{test_log_args_destination{}};
30-
cfg.logger.log_msg<logging::level::TRACE, cib_log_module_id_t>(
30+
cfg.logger.log_msg<logging::level::TRACE, cib_log_env_t>(
3131
"A string with no placeholders"_sc);
3232
}
3333

3434
auto log_one_ct_arg() -> void {
3535
auto cfg = logging::mipi::config{test_log_args_destination{}};
36-
cfg.logger.log_msg<logging::level::TRACE, cib_log_module_id_t>(
36+
cfg.logger.log_msg<logging::level::TRACE, cib_log_env_t>(
3737
format("B string with {} placeholder"_sc, "one"_sc));
3838
}
3939

4040
auto log_one_rt_arg() -> void {
4141
auto cfg = logging::mipi::config{test_log_args_destination{}};
42-
cfg.logger.log_msg<logging::level::TRACE, cib_log_module_id_t>(
42+
cfg.logger.log_msg<logging::level::TRACE, cib_log_env_t>(
4343
format("C string with {} placeholder"_sc, 1));
4444
}
4545

4646
auto log_with_non_default_module_id() -> void {
4747
CIB_LOG_MODULE("not default");
4848
auto cfg = logging::mipi::config{test_log_args_destination{}};
49-
cfg.logger.log_msg<logging::level::TRACE, cib_log_module_id_t>(
49+
cfg.logger.log_msg<logging::level::TRACE, cib_log_env_t>(
5050
format("ModuleID string with {} placeholder"_sc, 1));
5151
}
5252

5353
auto log_with_fixed_module_id() -> void {
5454
CIB_LOG_MODULE("fixed");
5555
auto cfg = logging::mipi::config{test_log_args_destination{}};
56-
cfg.logger.log_msg<logging::level::TRACE, cib_log_module_id_t>(
56+
cfg.logger.log_msg<logging::level::TRACE, cib_log_env_t>(
5757
format("Fixed ModuleID string with {} placeholder"_sc, 1));
5858
}

test/log/catalog2a_lib.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ auto log_two_rt_args() -> void;
2020

2121
auto log_two_rt_args() -> void {
2222
auto cfg = logging::mipi::config{test_log_args_destination{}};
23-
cfg.logger.log_msg<logging::level::TRACE, cib_log_module_id_t>(
23+
cfg.logger.log_msg<logging::level::TRACE, cib_log_env_t>(
2424
format("D string with {} and {} placeholder"_sc, 1, 2));
2525
}

0 commit comments

Comments
 (0)