Skip to content

Commit 179769c

Browse files
authored
Merge pull request #732 from elbeno/fixed-module-id
✨ Add fixed module ID query to logging
2 parents 5e97e40 + 62819d8 commit 179769c

File tree

5 files changed

+58
-12
lines changed

5 files changed

+58
-12
lines changed

include/log/catalog/encoder.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <log/catalog/catalog.hpp>
55
#include <log/log.hpp>
66
#include <log/module.hpp>
7+
#include <log/module_id.hpp>
78
#include <log/string_id.hpp>
89

910
#include <stdx/ct_string.hpp>
@@ -31,10 +32,10 @@ constexpr static auto to_message() {
3132
}(std::make_integer_sequence<std::size_t, std::size(s)>{});
3233
}
3334

34-
template <stdx::ct_string S> constexpr static auto to_module() {
35+
template <stdx::ct_string S, auto Id> constexpr static auto to_module() {
3536
constexpr auto s = std::string_view{S};
3637
return [&]<std::size_t... Is>(std::integer_sequence<std::size_t, Is...>) {
37-
return sc::module_string<sc::undefined<void, -1, char, s[Is]...>>{};
38+
return sc::module_string<sc::undefined<void, Id, char, s[Is]...>>{};
3839
}(std::make_integer_sequence<std::size_t, std::size(s)>{});
3940
}
4041

@@ -91,7 +92,9 @@ template <typename Writer> struct log_handler {
9192
detail::to_message_t<decltype(fr.str), logging::get_string_id(
9293
Env{})>::template fn,
9394
std::remove_cvref_t<Args>...>;
94-
using Module = decltype(detail::to_module<get_module(Env{})>());
95+
using Module =
96+
decltype(detail::to_module<get_module(Env{}),
97+
logging::get_module_id(Env{})>());
9598
w(builder.template build<L>(catalog<Message>(), module<Module>(),
9699
std::forward<Args>(args)...));
97100
});

include/log/module_id.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
3+
#include <log/env.hpp>
4+
5+
#include <utility>
6+
7+
namespace logging {
8+
[[maybe_unused]] constexpr inline struct get_module_id_t {
9+
template <typename T>
10+
requires true // more constrained
11+
CONSTEVAL auto operator()(T &&t) const noexcept(
12+
noexcept(std::forward<T>(t).query(std::declval<get_module_id_t>())))
13+
-> decltype(std::forward<T>(t).query(*this)) {
14+
return std::forward<T>(t).query(*this);
15+
}
16+
17+
CONSTEVAL auto operator()(auto &&) const -> int { return -1; }
18+
} get_module_id;
19+
} // namespace logging

test/log/catalog1_lib.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ auto log_one_ct_arg() -> void;
3030
auto log_one_32bit_rt_arg() -> void;
3131
auto log_one_64bit_rt_arg() -> void;
3232
auto log_one_formatted_rt_arg() -> void;
33-
auto log_with_non_default_module_id() -> void;
34-
auto log_with_fixed_module_id() -> void;
33+
auto log_with_non_default_module() -> void;
34+
auto log_with_fixed_module() -> void;
3535
auto log_with_fixed_string_id() -> void;
36+
auto log_with_fixed_module_id() -> void;
3637

3738
auto log_zero_args() -> void {
3839
auto cfg = logging::binary::config{test_log_args_destination{}};
@@ -65,7 +66,7 @@ auto log_one_formatted_rt_arg() -> void {
6566
stdx::ct_format<"C3 string with {:08x} placeholder">(std::int32_t{1}));
6667
}
6768

68-
auto log_with_non_default_module_id() -> void {
69+
auto log_with_non_default_module() -> void {
6970
CIB_WITH_LOG_ENV(logging::get_level, logging::level::TRACE,
7071
logging::get_module, "not default") {
7172
auto cfg = logging::binary::config{test_log_args_destination{}};
@@ -74,7 +75,7 @@ auto log_with_non_default_module_id() -> void {
7475
}
7576
}
7677

77-
auto log_with_fixed_module_id() -> void {
78+
auto log_with_fixed_module() -> void {
7879
CIB_WITH_LOG_ENV(logging::get_level, logging::level::TRACE,
7980
logging::get_module, "fixed") {
8081
auto cfg = logging::binary::config{test_log_args_destination{}};
@@ -91,3 +92,13 @@ auto log_with_fixed_string_id() -> void {
9192
stdx::ct_format<"Fixed StringID string">());
9293
}
9394
}
95+
96+
auto log_with_fixed_module_id() -> void {
97+
CIB_WITH_LOG_ENV(logging::get_level, logging::level::TRACE,
98+
logging::get_module_id, 7, logging::get_module,
99+
"fixed_id") {
100+
auto cfg = logging::binary::config{test_log_args_destination{}};
101+
cfg.logger.log_msg<cib_log_env_t>(
102+
stdx::ct_format<"Fixed ModuleID string with {} placeholder">(1));
103+
}
104+
}

test/log/catalog_app.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ extern auto log_one_64bit_rt_arg() -> void;
1717
extern auto log_one_formatted_rt_arg() -> void;
1818
extern auto log_two_rt_args() -> void;
1919
extern auto log_rt_enum_arg() -> void;
20-
extern auto log_with_non_default_module_id() -> void;
21-
extern auto log_with_fixed_module_id() -> void;
20+
extern auto log_with_non_default_module() -> void;
21+
extern auto log_with_fixed_module() -> void;
2222
extern auto log_with_fixed_string_id() -> void;
23+
extern auto log_with_fixed_module_id() -> void;
2324

2425
TEST_CASE("log zero arguments", "[catalog]") {
2526
test_critical_section::count = 0;
@@ -88,16 +89,17 @@ TEST_CASE("log module ids change", "[catalog]") {
8889
CHECK((last_header & expected_static) == expected_static);
8990

9091
auto default_header = last_header;
91-
log_with_non_default_module_id();
92+
log_with_non_default_module();
9293
CHECK((last_header & expected_static) == expected_static);
9394
CHECK((last_header ^ default_header) == (1u << 16u));
9495
}
9596

96-
TEST_CASE("log with fixed module id", "[catalog]") {
97+
TEST_CASE("log with stable module id", "[catalog]") {
9798
std::uint32_t expected_static = (1u << 24u) | (7u << 4u) | 3u;
9899

99-
log_with_fixed_module_id();
100+
log_with_fixed_module();
100101
CHECK((last_header & expected_static) == expected_static);
102+
// module ID 17 is fixed by stable_strings.json
101103
CHECK((last_header & ~expected_static) == (17u << 16u));
102104
}
103105

@@ -110,3 +112,12 @@ TEST_CASE("log with fixed string id", "[catalog]") {
110112
// string ID 1337 is fixed by environment
111113
CHECK(last_header == ((1337u << 4u) | 1u));
112114
}
115+
116+
TEST_CASE("log with fixed module id", "[catalog]") {
117+
std::uint32_t expected_static = (1u << 24u) | (7u << 4u) | 3u;
118+
119+
log_with_fixed_module_id();
120+
CHECK((last_header & expected_static) == expected_static);
121+
// module ID 7 is fixed by environment
122+
CHECK((last_header & ~expected_static) == (7u << 16u));
123+
}

tools/gen_str_catalog.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ def get_id(stables, gen, obj):
163163
stable_msgs, stable_modules = stable_data
164164
for msg in filter(lambda m: m.id != -1, messages):
165165
stable_msgs[msg.key()] = msg
166+
for module in filter(lambda m: m.id != -1, modules):
167+
stable_modules[module.key()] = module
166168

167169
old_msg_ids = set(m.id for m in stable_msgs.values())
168170
msg_id_gen = itertools.filterfalse(old_msg_ids.__contains__, itertools.count(0))

0 commit comments

Comments
 (0)