|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <log/catalog/builder.hpp> |
| 4 | +#include <log/catalog/catalog.hpp> |
| 5 | +#include <log/log.hpp> |
| 6 | +#include <log/module.hpp> |
| 7 | + |
| 8 | +#include <stdx/ct_string.hpp> |
| 9 | +#include <stdx/span.hpp> |
| 10 | +#include <stdx/tuple.hpp> |
| 11 | +#include <stdx/utility.hpp> |
| 12 | + |
| 13 | +#include <conc/concurrency.hpp> |
| 14 | + |
| 15 | +#include <cstddef> |
| 16 | +#include <cstdint> |
| 17 | +#include <string_view> |
| 18 | +#include <type_traits> |
| 19 | +#include <utility> |
| 20 | + |
| 21 | +namespace logging::binary { |
| 22 | +namespace detail { |
| 23 | +template <typename S, typename... Args> constexpr static auto to_message() { |
| 24 | + constexpr auto s = S::value; |
| 25 | + using char_t = typename std::remove_cv_t<decltype(s)>::value_type; |
| 26 | + return [&]<std::size_t... Is>(std::integer_sequence<std::size_t, Is...>) { |
| 27 | + return sc::message< |
| 28 | + sc::undefined<sc::args<Args...>, char_t, s[Is]...>>{}; |
| 29 | + }(std::make_integer_sequence<std::size_t, std::size(s)>{}); |
| 30 | +} |
| 31 | + |
| 32 | +template <stdx::ct_string S> constexpr static auto to_module() { |
| 33 | + constexpr auto s = std::string_view{S}; |
| 34 | + return [&]<std::size_t... Is>(std::integer_sequence<std::size_t, Is...>) { |
| 35 | + return sc::module_string<sc::undefined<void, char, s[Is]...>>{}; |
| 36 | + }(std::make_integer_sequence<std::size_t, std::size(s)>{}); |
| 37 | +} |
| 38 | + |
| 39 | +template <typename S> struct to_message_t { |
| 40 | + template <typename... Args> using fn = decltype(to_message<S, Args...>()); |
| 41 | +}; |
| 42 | +} // namespace detail |
| 43 | + |
| 44 | +template <typename Destinations> struct log_writer { |
| 45 | + template <std::size_t N> |
| 46 | + auto operator()(stdx::span<std::uint8_t const, N> msg) -> void { |
| 47 | + stdx::for_each( |
| 48 | + [&]<typename Dest>(Dest &dest) { |
| 49 | + conc::call_in_critical_section<Dest>( |
| 50 | + [&] { dest.log_by_buf(msg); }); |
| 51 | + }, |
| 52 | + dests); |
| 53 | + } |
| 54 | + |
| 55 | + template <std::size_t N> |
| 56 | + auto operator()(stdx::span<std::uint32_t const, N> msg) -> void { |
| 57 | + [&]<std::size_t... Is>(std::index_sequence<Is...>) { |
| 58 | + stdx::for_each( |
| 59 | + [&]<typename Dest>(Dest &dest) { |
| 60 | + conc::call_in_critical_section<Dest>( |
| 61 | + [&] { dest.log_by_args(msg[Is]...); }); |
| 62 | + }, |
| 63 | + dests); |
| 64 | + }(std::make_index_sequence<N>{}); |
| 65 | + } |
| 66 | + |
| 67 | + auto operator()(auto const &msg) -> void { |
| 68 | + this->operator()(msg.as_const_view().data()); |
| 69 | + } |
| 70 | + |
| 71 | + Destinations dests; |
| 72 | +}; |
| 73 | +template <typename T> log_writer(T) -> log_writer<T>; |
| 74 | + |
| 75 | +template <typename Writer> struct log_handler { |
| 76 | + template <typename Env, typename FilenameStringType, |
| 77 | + typename LineNumberType, typename MsgType> |
| 78 | + auto log(FilenameStringType, LineNumberType, MsgType const &msg) -> void { |
| 79 | + log_msg<Env>(msg); |
| 80 | + } |
| 81 | + |
| 82 | + template <typename Env, typename Msg> auto log_msg(Msg msg) -> void { |
| 83 | + msg.apply([&]<typename S, typename... Args>(S, Args... args) { |
| 84 | + auto builder = get_builder(Env{}); |
| 85 | + constexpr auto L = stdx::to_underlying(get_level(Env{})); |
| 86 | + using Message = typename decltype(builder)::template convert_args< |
| 87 | + detail::to_message_t<S>::template fn, Args...>; |
| 88 | + using Module = decltype(detail::to_module<get_module(Env{})>()); |
| 89 | + w(builder.template build<L>(catalog<Message>(), module<Module>(), |
| 90 | + args...)); |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + template <typename Env, auto Version, stdx::ct_string S = ""> |
| 95 | + auto log_version() -> void { |
| 96 | + auto builder = get_builder(Env{}); |
| 97 | + w(builder.template build_version<Version, S>()); |
| 98 | + } |
| 99 | + |
| 100 | + Writer w; |
| 101 | +}; |
| 102 | + |
| 103 | +template <typename... TDestinations> struct config { |
| 104 | + using destinations_tuple_t = stdx::tuple<TDestinations...>; |
| 105 | + constexpr explicit config(TDestinations... dests) |
| 106 | + : logger{log_writer{stdx::tuple{std::move(dests)...}}} {} |
| 107 | + |
| 108 | + log_handler<log_writer<destinations_tuple_t>> logger; |
| 109 | +}; |
| 110 | +template <typename... Ts> config(Ts...) -> config<Ts...>; |
| 111 | +} // namespace logging::binary |
0 commit comments