Skip to content
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

🎨 Simplify message catalog types #353

Merged
merged 1 commit into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/log/catalog/catalog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

#include <cstdint>

namespace sc {
template <typename...> struct args;
template <typename, typename T, T...> struct undefined;
} // namespace sc

template <logging::level level, typename MessageString> struct message {};

using string_id = std::uint32_t;
Expand Down
23 changes: 22 additions & 1 deletion include/log/catalog/mipi_encoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,27 @@
#include <exception>
#include <utility>

namespace {
template <logging::level L, typename S, typename T>
constexpr auto to_message() {
constexpr auto s = S::value;
using char_t = typename std::remove_cv_t<decltype(s)>::value_type;
return [&]<template <typename...> typename Tuple, typename... Args,
std::size_t... Is>(Tuple<Args...> const &,
std::integer_sequence<std::size_t, Is...>) {
return message<L, sc::undefined<sc::args<Args...>, char_t, s[Is]...>>{};
}(T{}, std::make_integer_sequence<std::size_t, std::size(s)>{});
}

template <logging::level L, typename Msg> constexpr auto to_message(Msg msg) {
if constexpr (requires { msg.args; }) {
return to_message<L, decltype(msg.str), decltype(msg.args)>();
} else {
return to_message<L, Msg, cib::tuple<>>();
}
}
} // namespace

namespace logging::mipi {
template <typename ConcurrencyPolicy, typename TDestinations>
struct log_handler {
Expand All @@ -28,7 +49,7 @@ struct log_handler {
template <logging::level Level, typename Msg>
CIB_ALWAYS_INLINE auto log_msg(Msg msg) -> void {
msg.apply([&]<typename StringType>(StringType, auto... args) {
using Message = message<Level, Msg>;
using Message = decltype(to_message<Level>(msg));
dispatch_message<Level>(catalog<Message>(),
static_cast<std::uint32_t>(args)...);
});
Expand Down
23 changes: 5 additions & 18 deletions tools/gen_str_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,8 @@

catalog_re = re.compile("^.+?(unsigned int catalog<(.+?)>\(\))\s*$")

# unsigned int catalog<message<(logging::level)7, sc::string_constant<char, (char)102, (char)108, (char)111, (char)119, (char)46, (char)115, (char)116, (char)97, (char)114, (char)116, (char)40, (char)77, (char)101, (char)109, (char)111, (char)114, (char)121, (char)69, (char)114, (char)114, (char)111, (char)114, (char)41> > >()
string_constant_re = re.compile(
"message<\(logging::level\)(\d+), sc::string_constant<char, (.*)>\s*(?:const)?\s*>"
)
# unsigned int catalog<message<(logging::level)7, sc::lazy_string_format<sc::string_constant<char, (char)102, (char)108, (char)111, (char)119, (char)46, (char)115, (char)116, (char)97, (char)114, (char)116, (char)40, (char)77, (char)101, (char)109, (char)111, (char)114, (char)121, (char)69, (char)114, (char)114, (char)111, (char)114, (char)41>, cib::tuple<> > > >()
lazy_string_re = re.compile(
"message<\(logging::level\)(\d+), sc::lazy_string_format<sc::string_constant<char, (.*)>\s*(?:const)?, cib::(?:[a-zA-Z0-9_]+::)*tuple<(.*)>\s*>\s*>"
string_re = re.compile(
"message<\(logging::level\)(\d+), sc::undefined<sc::args<(.*)>, char, (.*)>\s*>"
)

string_id = 0
Expand All @@ -34,10 +29,7 @@

out.write(
"""
#include <cib/tuple.hpp>
#include <log/catalog/catalog.hpp>
#include <sc/lazy_string_format.hpp>
#include <sc/string_constant.hpp>

"""
)
Expand Down Expand Up @@ -136,16 +128,11 @@ def split_args(s):
catalog_m = catalog_re.match(line)

if catalog_m:
string_m = lazy_string_re.match(catalog_m.group(2))
if string_m:
arg_tuple = string_m.group(3)
else:
string_m = string_constant_re.match(catalog_m.group(2))
arg_tuple = ""

string_m = string_re.match(catalog_m.group(2))
catalog_type = catalog_m.group(1)
log_level = string_m.group(1)
string_tuple = string_m.group(2).replace("(char)", "")
arg_tuple = string_m.group(2)
string_tuple = string_m.group(3).replace("(char)", "")
string_value = "".join(
[chr(int(c)) for c in re.split(r"\s*,\s*", string_tuple)]
)
Expand Down