|
| 1 | +#include <cib/cib.hpp> |
| 2 | +#include <flow/flow.hpp> |
| 3 | + |
| 4 | +#include <catch2/catch_test_macros.hpp> |
| 5 | + |
| 6 | +#include <string> |
| 7 | +#include <vector> |
| 8 | + |
| 9 | +namespace { |
| 10 | +using namespace flow::literals; |
| 11 | + |
| 12 | +template <auto... Cs> struct wrapper { |
| 13 | + struct inner { |
| 14 | + constexpr static auto config = cib::config(Cs...); |
| 15 | + }; |
| 16 | + constexpr static auto n = cib::nexus<inner>{}; |
| 17 | + |
| 18 | + wrapper() { n.init(); } |
| 19 | + |
| 20 | + template <typename S> static auto run() -> void { n.template service<S>(); } |
| 21 | +}; |
| 22 | + |
| 23 | +std::vector<logging::level> log_calls{}; |
| 24 | + |
| 25 | +template <typename S, auto... Cs> |
| 26 | +constexpr auto run_flow = []() -> void { |
| 27 | + log_calls.clear(); |
| 28 | + wrapper<Cs...>::template run<S>(); |
| 29 | +}; |
| 30 | + |
| 31 | +struct TestFlowA : public flow::service<"A"> {}; |
| 32 | +struct TestFlowB : public flow::service<"B"> {}; |
| 33 | + |
| 34 | +constexpr auto msA = flow::milestone<"msA">(); |
| 35 | +constexpr auto msB = flow::milestone<"msB">(); |
| 36 | + |
| 37 | +struct log_config { |
| 38 | + struct log_handler { |
| 39 | + template <logging::level Level, typename ModuleId, |
| 40 | + typename FilenameStringType, typename LineNumberType, |
| 41 | + typename MsgType> |
| 42 | + auto log(FilenameStringType, LineNumberType, MsgType const &) -> void { |
| 43 | + log_calls.push_back(Level); |
| 44 | + } |
| 45 | + }; |
| 46 | + log_handler logger; |
| 47 | +}; |
| 48 | +} // namespace |
| 49 | + |
| 50 | +template <> inline auto logging::config<> = log_config{}; |
| 51 | + |
| 52 | +struct user1_log_spec : flow::default_log_spec { |
| 53 | + constexpr static auto level = logging::level::USER1; |
| 54 | +}; |
| 55 | +struct user2_log_spec : flow::default_log_spec { |
| 56 | + constexpr static auto level = logging::level::USER2; |
| 57 | +}; |
| 58 | +struct info_log_spec : flow::default_log_spec { |
| 59 | + constexpr static auto level = logging::level::INFO; |
| 60 | +}; |
| 61 | +template <> constexpr auto flow::log_spec<"default"> = info_log_spec{}; |
| 62 | + |
| 63 | +TEST_CASE("override default log level", "[flow_custom_log_levels]") { |
| 64 | + run_flow<TestFlowA, cib::exports<TestFlowA>, |
| 65 | + cib::extend<TestFlowA>(*msA)>(); |
| 66 | + |
| 67 | + REQUIRE(not log_calls.empty()); |
| 68 | + std::for_each(std::begin(log_calls), std::end(log_calls), |
| 69 | + [](auto level) { CHECK(level == logging::level::INFO); }); |
| 70 | +} |
| 71 | + |
| 72 | +template <> constexpr auto flow::log_spec<"B"> = user1_log_spec{}; |
| 73 | +template <> constexpr auto flow::log_spec<"msB"> = user2_log_spec{}; |
| 74 | + |
| 75 | +TEST_CASE("override log level by name", "[flow_custom_log_levels]") { |
| 76 | + run_flow<TestFlowB, cib::exports<TestFlowB>, |
| 77 | + cib::extend<TestFlowB>(*msB)>(); |
| 78 | + |
| 79 | + REQUIRE(log_calls.size() == 3); |
| 80 | + CHECK(log_calls[0] == logging::level::USER1); |
| 81 | + CHECK(log_calls[1] == logging::level::USER2); |
| 82 | + CHECK(log_calls[2] == logging::level::USER1); |
| 83 | +} |
0 commit comments