-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Problem: - Logging is parameterized on many axes. Most logging systems include the level. Some also include a module or source. Some include other parameters. - Logging machinery should be agnostic to the parameterizations of each logging backend. Adding extra parameters to logging macros is not scalable. Solution: - Expand the current logging module to the idea of a logging environment. An environment allows compile-time scope-based arguments to be passed through the logging machinery; logging backends can extract whichever values they can use from the current environment.
- Loading branch information
Showing
12 changed files
with
228 additions
and
71 deletions.
There are no files selected for viewing
This file contains 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 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,91 @@ | ||
#pragma once | ||
|
||
#include <stdx/compiler.hpp> | ||
#include <stdx/ct_string.hpp> | ||
#include <stdx/utility.hpp> | ||
|
||
#include <boost/mp11/algorithm.hpp> | ||
|
||
#ifdef __clang__ | ||
#define CIB_PRAGMA_SEMI | ||
#else | ||
#define CIB_PRAGMA_SEMI ; | ||
#endif | ||
|
||
namespace logging { | ||
template <auto Query, auto Value> struct prop { | ||
[[nodiscard]] CONSTEVAL static auto query(decltype(Query)) noexcept { | ||
return Value; | ||
} | ||
}; | ||
|
||
namespace detail { | ||
template <typename Q, typename Env> | ||
concept valid_query_for = requires { Env::query(Q{}); }; | ||
|
||
template <typename Q, typename... Envs> | ||
concept valid_query_over = (... or valid_query_for<Q, Envs>); | ||
|
||
template <typename Q> struct has_query { | ||
template <typename Env> | ||
using fn = std::bool_constant<valid_query_for<Q, Env>>; | ||
}; | ||
} // namespace detail | ||
|
||
template <typename... Envs> struct env { | ||
template <detail::valid_query_over<Envs...> Q> | ||
CONSTEVAL static auto query(Q) noexcept { | ||
using I = boost::mp11::mp_find_if_q<boost::mp11::mp_list<Envs...>, | ||
detail::has_query<Q>>; | ||
using E = boost::mp11::mp_at<boost::mp11::mp_list<Envs...>, I>; | ||
return Q{}(E{}); | ||
} | ||
}; | ||
|
||
namespace detail { | ||
template <typename T> struct autowrap { | ||
CONSTEVAL autowrap(T t) : value(t) {} | ||
T value; | ||
}; | ||
|
||
template <std::size_t N> using str_lit_t = char const (&)[N]; | ||
|
||
template <std::size_t N> struct autowrap<str_lit_t<N>> { | ||
CONSTEVAL autowrap(str_lit_t<N> str) : value(str) {} | ||
stdx::ct_string<N> value; | ||
}; | ||
|
||
template <typename T> autowrap(T) -> autowrap<T>; | ||
template <std::size_t N> autowrap(str_lit_t<N>) -> autowrap<str_lit_t<N>>; | ||
|
||
template <auto V> struct wrap { | ||
constexpr static auto value = V; | ||
}; | ||
|
||
template <typename> struct for_each_pair; | ||
template <std::size_t... Is> struct for_each_pair<std::index_sequence<Is...>> { | ||
template <auto... Args> | ||
using type = env< | ||
prop<boost::mp11::mp_at_c<boost::mp11::mp_list<wrap<Args>...>, | ||
2 * Is>::value.value, | ||
stdx::ct<boost::mp11::mp_at_c<boost::mp11::mp_list<wrap<Args>...>, | ||
2 * Is + 1>::value.value>()>...>; | ||
}; | ||
} // namespace detail | ||
} // namespace logging | ||
|
||
using cib_log_env_t = logging::env<>; | ||
|
||
// NOLINTBEGIN(cppcoreguidelines-macro-usage) | ||
#define CIB_LOG_ENV(...) \ | ||
STDX_PRAGMA(diagnostic push) \ | ||
STDX_PRAGMA(diagnostic ignored "-Wshadow") \ | ||
using cib_log_env_t [[maybe_unused]] = boost::mp11::mp_append< \ | ||
decltype([]<logging::detail::autowrap... Args> { \ | ||
return typename logging::detail::for_each_pair< \ | ||
std::make_index_sequence<sizeof...(Args) / \ | ||
2>>::template type<Args...>{}; \ | ||
}.template operator()<__VA_ARGS__>()), \ | ||
cib_log_env_t> \ | ||
CIB_PRAGMA_SEMI STDX_PRAGMA(diagnostic pop) | ||
// NOLINTEND(cppcoreguidelines-macro-usage) |
This file contains 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 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 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,27 @@ | ||
#pragma once | ||
|
||
#include <log/env.hpp> | ||
|
||
#include <stdx/ct_string.hpp> | ||
|
||
#include <utility> | ||
|
||
namespace logging { | ||
[[maybe_unused]] constexpr inline struct get_module_t { | ||
template <typename T> | ||
requires true // more constrained | ||
CONSTEVAL auto operator()(T &&t) const noexcept( | ||
noexcept(std::forward<T>(t).query(std::declval<get_module_t>()))) | ||
-> decltype(std::forward<T>(t).query(*this)) { | ||
return std::forward<T>(t).query(*this); | ||
} | ||
|
||
CONSTEVAL auto operator()(auto &&) const { | ||
using namespace stdx::literals; | ||
return "default"_ctst; | ||
} | ||
} get_module; | ||
} // namespace logging | ||
|
||
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage) | ||
#define CIB_LOG_MODULE(S) CIB_LOG_ENV(logging::get_module, S) |
This file contains 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 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 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 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 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,50 @@ | ||
#include <log/env.hpp> | ||
#include <log/module.hpp> | ||
|
||
#include <catch2/catch_test_macros.hpp> | ||
|
||
namespace { | ||
[[maybe_unused]] constexpr inline struct custom_t { | ||
template <typename T> | ||
requires true // more constrained | ||
CONSTEVAL auto operator()(T &&t) const | ||
noexcept(noexcept(std::forward<T>(t).query(std::declval<custom_t>()))) | ||
-> decltype(std::forward<T>(t).query(*this)) { | ||
return std::forward<T>(t).query(*this); | ||
} | ||
|
||
CONSTEVAL auto operator()(auto &&) const { | ||
using namespace stdx::literals; | ||
return 42; | ||
} | ||
} custom; | ||
} // namespace | ||
|
||
TEST_CASE("override environment", "[log_env]") { | ||
static_assert(custom(cib_log_env_t{}) == 42); | ||
CIB_LOG_ENV(custom, 1); | ||
static_assert(custom(cib_log_env_t{}) == 1); | ||
{ | ||
CIB_LOG_ENV(custom, 2); | ||
static_assert(custom(cib_log_env_t{}) == 2); | ||
} | ||
} | ||
|
||
TEST_CASE("supplement environment", "[log_env]") { | ||
CIB_LOG_ENV(custom, 1); | ||
static_assert(custom(cib_log_env_t{}) == 1); | ||
{ | ||
using namespace stdx::literals; | ||
CIB_LOG_ENV(logging::get_module, "hello"); | ||
static_assert(custom(cib_log_env_t{}) == 1); | ||
static_assert(logging::get_module(cib_log_env_t{}) == "hello"_ctst); | ||
} | ||
} | ||
|
||
TEST_CASE("multi-value environment", "[log_env]") { | ||
CIB_LOG_ENV(custom, 1, logging::get_module, "hello"); | ||
|
||
using namespace stdx::literals; | ||
static_assert(custom(cib_log_env_t{}) == 1); | ||
static_assert(logging::get_module(cib_log_env_t{}) == "hello"_ctst); | ||
} |
Oops, something went wrong.