-
Notifications
You must be signed in to change notification settings - Fork 58
✨ Fine-grained logging control for flows #644
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#pragma once | ||
|
||
#include <log/log.hpp> | ||
|
||
#include <type_traits> | ||
|
||
namespace flow { | ||
struct default_log_spec { | ||
using flavor = logging::default_flavor_t; | ||
constexpr static auto level = logging::level::TRACE; | ||
}; | ||
template <stdx::ct_string, typename...> | ||
constexpr auto log_spec = default_log_spec{}; | ||
|
||
template <typename T, typename... DummyArgs> | ||
requires(sizeof...(DummyArgs) == 0) | ||
constexpr static auto get_log_spec() { | ||
if constexpr (std::is_same_v<decltype(log_spec<T::ct_name, DummyArgs...>), | ||
default_log_spec const>) { | ||
return log_spec<"default", DummyArgs...>; | ||
} else { | ||
return log_spec<T::ct_name, DummyArgs...>; | ||
} | ||
} | ||
|
||
template <typename T, typename... DummyArgs, typename... Args> | ||
auto log_with_spec(Args &&...args) { | ||
using log_spec_t = decltype(get_log_spec<T, DummyArgs...>()); | ||
logging::log<typename log_spec_t::flavor, log_spec_t::level, | ||
cib_log_module_id_t>(std::forward<Args>(args)...); | ||
} | ||
} // namespace flow |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#include <cib/cib.hpp> | ||
#include <flow/flow.hpp> | ||
|
||
#include <catch2/catch_test_macros.hpp> | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
namespace { | ||
using namespace flow::literals; | ||
|
||
template <auto... Cs> struct wrapper { | ||
struct inner { | ||
constexpr static auto config = cib::config(Cs...); | ||
}; | ||
constexpr static auto n = cib::nexus<inner>{}; | ||
|
||
wrapper() { n.init(); } | ||
|
||
template <typename S> static auto run() -> void { n.template service<S>(); } | ||
}; | ||
|
||
std::vector<logging::level> log_calls{}; | ||
|
||
template <typename S, auto... Cs> | ||
constexpr auto run_flow = []() -> void { | ||
log_calls.clear(); | ||
wrapper<Cs...>::template run<S>(); | ||
}; | ||
|
||
struct TestFlowA : public flow::service<"A"> {}; | ||
struct TestFlowB : public flow::service<"B"> {}; | ||
|
||
constexpr auto msA = flow::milestone<"msA">(); | ||
constexpr auto msB = flow::milestone<"msB">(); | ||
|
||
struct log_config { | ||
struct log_handler { | ||
template <logging::level Level, typename ModuleId, | ||
typename FilenameStringType, typename LineNumberType, | ||
typename MsgType> | ||
auto log(FilenameStringType, LineNumberType, MsgType const &) -> void { | ||
log_calls.push_back(Level); | ||
} | ||
}; | ||
log_handler logger; | ||
}; | ||
} // namespace | ||
|
||
template <> inline auto logging::config<> = log_config{}; | ||
|
||
struct user1_log_spec : flow::default_log_spec { | ||
constexpr static auto level = logging::level::USER1; | ||
}; | ||
struct user2_log_spec : flow::default_log_spec { | ||
constexpr static auto level = logging::level::USER2; | ||
}; | ||
struct info_log_spec : flow::default_log_spec { | ||
constexpr static auto level = logging::level::INFO; | ||
}; | ||
template <> constexpr auto flow::log_spec<"default"> = info_log_spec{}; | ||
|
||
TEST_CASE("override default log level", "[flow_custom_log_levels]") { | ||
run_flow<TestFlowA, cib::exports<TestFlowA>, | ||
cib::extend<TestFlowA>(*msA)>(); | ||
|
||
REQUIRE(not log_calls.empty()); | ||
std::for_each(std::begin(log_calls), std::end(log_calls), | ||
[](auto level) { CHECK(level == logging::level::INFO); }); | ||
} | ||
|
||
template <> constexpr auto flow::log_spec<"B"> = user1_log_spec{}; | ||
template <> constexpr auto flow::log_spec<"msB"> = user2_log_spec{}; | ||
|
||
TEST_CASE("override log level by name", "[flow_custom_log_levels]") { | ||
run_flow<TestFlowB, cib::exports<TestFlowB>, | ||
cib::extend<TestFlowB>(*msB)>(); | ||
|
||
REQUIRE(log_calls.size() == 3); | ||
CHECK(log_calls[0] == logging::level::USER1); | ||
CHECK(log_calls[1] == logging::level::USER2); | ||
CHECK(log_calls[2] == logging::level::USER1); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#include <cib/cib.hpp> | ||
#include <flow/flow.hpp> | ||
|
||
#include <catch2/catch_test_macros.hpp> | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
namespace { | ||
using namespace flow::literals; | ||
|
||
template <auto... Cs> struct wrapper { | ||
struct inner { | ||
constexpr static auto config = cib::config(Cs...); | ||
}; | ||
constexpr static auto n = cib::nexus<inner>{}; | ||
|
||
wrapper() { n.init(); } | ||
|
||
template <typename S> static auto run() -> void { n.template service<S>(); } | ||
}; | ||
|
||
std::vector<logging::level> log_calls{}; | ||
|
||
template <typename S, auto... Cs> | ||
constexpr auto run_flow = []() -> void { | ||
log_calls.clear(); | ||
wrapper<Cs...>::template run<S>(); | ||
}; | ||
|
||
struct NamedTestFlow : public flow::service<"TestFlow"> {}; | ||
|
||
constexpr auto ms = flow::milestone<"ms">(); | ||
|
||
struct log_config { | ||
struct log_handler { | ||
template <logging::level Level, typename ModuleId, | ||
typename FilenameStringType, typename LineNumberType, | ||
typename MsgType> | ||
auto log(FilenameStringType, LineNumberType, MsgType const &) -> void { | ||
log_calls.push_back(Level); | ||
} | ||
}; | ||
log_handler logger; | ||
}; | ||
} // namespace | ||
|
||
template <> inline auto logging::config<> = log_config{}; | ||
|
||
TEST_CASE("flow logs at TRACE by default", "[flow_log_levels]") { | ||
run_flow<NamedTestFlow, cib::exports<NamedTestFlow>, | ||
cib::extend<NamedTestFlow>(*ms)>(); | ||
|
||
REQUIRE(not log_calls.empty()); | ||
std::for_each(std::begin(log_calls), std::end(log_calls), | ||
[](auto level) { CHECK(level == logging::level::TRACE); }); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.