Skip to content

Commit 0858891

Browse files
committed
✨ Use message environment when logging callback matches
1 parent aba9fc1 commit 0858891

File tree

4 files changed

+54
-18
lines changed

4 files changed

+54
-18
lines changed

include/log/env.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,25 @@ using cib_log_env_t = stdx::env<>;
1818
return stdx::extend_env_t<cib_log_env_t, __VA_ARGS__>{}; \
1919
}()) cib_log_env_t
2020

21+
#define CIB_APPEND_LOG_ENV_DECL(E) \
22+
[[maybe_unused]] typedef decltype([] { \
23+
return stdx::append_env_t<cib_log_env_t, E>{}; \
24+
}()) cib_log_env_t
25+
2126
#define CIB_LOG_ENV(...) \
2227
STDX_PRAGMA(diagnostic push) \
2328
STDX_PRAGMA(diagnostic ignored "-Wshadow") \
2429
CIB_LOG_ENV_DECL(__VA_ARGS__) \
2530
CIB_PRAGMA_SEMI \
2631
STDX_PRAGMA(diagnostic pop)
2732

33+
#define CIB_APPEND_LOG_ENV(E) \
34+
STDX_PRAGMA(diagnostic push) \
35+
STDX_PRAGMA(diagnostic ignored "-Wshadow") \
36+
CIB_APPEND_LOG_ENV_DECL(E) \
37+
CIB_PRAGMA_SEMI \
38+
STDX_PRAGMA(diagnostic pop)
39+
2840
#define CIB_WITH_LOG_ENV(...) \
2941
STDX_PRAGMA(diagnostic push) \
3042
STDX_PRAGMA(diagnostic ignored "-Wshadow") \

include/msg/callback.hpp

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ struct callback {
2929

3030
template <typename... Args>
3131
[[nodiscard]] auto handle(auto const &data, Args &&...args) const -> bool {
32+
CIB_LOG_ENV(logging::get_level, logging::level::INFO);
3233
if (msg::call_with_message<Msg>(matcher, data)) {
33-
CIB_INFO("Incoming message matched [{}], because [{}], executing "
34-
"callback",
35-
stdx::ct_string_to_type<Name, sc::string_constant>(),
36-
matcher.describe());
34+
CIB_APPEND_LOG_ENV(typename Msg::env_t);
35+
CIB_LOG("Incoming message matched [{}], because [{}], executing "
36+
"callback",
37+
stdx::ct_string_to_type<Name, sc::string_constant>(),
38+
matcher.describe());
3739
msg::call_with_message<Msg>(callable, data,
3840
std::forward<Args>(args)...);
3941
return true;
@@ -42,14 +44,19 @@ struct callback {
4244
}
4345

4446
auto log_mismatch(auto const &data) const -> void {
45-
CIB_INFO(" {} - F:({})",
46-
stdx::ct_string_to_type<Name, sc::string_constant>(),
47-
msg::call_with_message<Msg>(
48-
[&]<typename T>(T &&t) -> decltype(matcher.describe_match(
49-
std::forward<T>(t))) {
50-
return matcher.describe_match(std::forward<T>(t));
51-
},
52-
data));
47+
CIB_LOG_ENV(logging::get_level, logging::level::INFO);
48+
{
49+
CIB_APPEND_LOG_ENV(typename Msg::env_t);
50+
CIB_LOG(
51+
" {} - F:({})",
52+
stdx::ct_string_to_type<Name, sc::string_constant>(),
53+
msg::call_with_message<Msg>(
54+
[&]<typename T>(T &&t) -> decltype(matcher.describe_match(
55+
std::forward<T>(t))) {
56+
return matcher.describe_match(std::forward<T>(t));
57+
},
58+
data));
59+
}
5360
}
5461

5562
using msg_t = Msg;

include/msg/detail/indexed_builder_common.hpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,13 @@ struct indexed_builder_base {
163163
}
164164

165165
using msg_t = typename CB::msg_t;
166+
CIB_LOG_ENV(logging::get_level, logging::level::INFO);
166167
if (msg::call_with_message<msg_t>(cb.matcher, data)) {
167-
CIB_INFO(
168-
"Incoming message matched [{}], because [{}] (collapsed to "
169-
"[{}]), executing callback",
170-
stdx::ct_string_to_type<cb.name, sc::string_constant>(),
171-
orig_cb.matcher.describe(), cb.matcher.describe());
168+
CIB_APPEND_LOG_ENV(typename msg_t::env_t);
169+
CIB_LOG("Incoming message matched [{}], because [{}] (collapsed to "
170+
"[{}]), executing callback",
171+
stdx::ct_string_to_type<cb.name, sc::string_constant>(),
172+
orig_cb.matcher.describe(), cb.matcher.describe());
172173
msg::call_with_message<msg_t>(cb.callable, data, args...);
173174
return true;
174175
}

test/msg/callback.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ TEST_CASE("callback handles message (typed)", "[callback]") {
152152
CHECK(dispatched);
153153
}
154154

155-
TEST_CASE("callback logs match", "[callback]") {
155+
TEST_CASE("callback logs match as INFO", "[callback]") {
156156
auto callback = msg::callback<"cb", msg_defn>(
157157
id_match, [](msg::const_view<msg_defn>) { dispatched = true; });
158158
auto const msg_match = std::array{0x8000ba11u, 0x0042d00du};
@@ -162,6 +162,22 @@ TEST_CASE("callback logs match", "[callback]") {
162162
CAPTURE(log_buffer);
163163
CHECK(log_buffer.find("matched [cb], because [id == 0x80]") !=
164164
std::string::npos);
165+
CHECK(log_buffer.find("INFO") != std::string::npos);
166+
}
167+
168+
TEST_CASE("callback logs match using message environment", "[callback]") {
169+
using trace_msg_defn = msg_defn::with_env<
170+
stdx::make_env_t<logging::get_level, logging::level::TRACE>>;
171+
auto callback = msg::callback<"cb", trace_msg_defn>(
172+
id_match, [](msg::const_view<trace_msg_defn>) { dispatched = true; });
173+
auto const msg_match = std::array{0x8000ba11u, 0x0042d00du};
174+
175+
log_buffer.clear();
176+
CHECK(callback.handle(msg_match));
177+
CAPTURE(log_buffer);
178+
CHECK(log_buffer.find("matched [cb], because [id == 0x80]") !=
179+
std::string::npos);
180+
CHECK(log_buffer.find("TRACE") != std::string::npos);
165181
}
166182

167183
TEST_CASE("callback with convenience matcher", "[callback]") {

0 commit comments

Comments
 (0)