logs.hpp: minor optimization for non-formatting logs

Also use single template with CharT to match fmt::format.
This commit is contained in:
Nekotekina 2020-03-07 13:43:22 +03:00
parent 12a3cdf0e8
commit b726aa5a3e
2 changed files with 15 additions and 23 deletions

View file

@ -309,8 +309,10 @@ void logs::message::broadcast(const char* fmt, const fmt_type_info* sup, ...) co
thread_local std::string text;
thread_local std::vector<u64> args;
static constexpr fmt_type_info empty_sup{};
std::size_t args_count = 0;
for (auto v = sup; v->fmt_string; v++)
for (auto v = sup; v && v->fmt_string; v++)
args_count++;
text.clear();
@ -321,7 +323,7 @@ void logs::message::broadcast(const char* fmt, const fmt_type_info* sup, ...) co
for (u64& arg : args)
arg = va_arg(c_args, u64);
va_end(c_args);
fmt::raw_append(text, fmt, sup, args.data());
fmt::raw_append(text, fmt, sup ? sup : &empty_sup, args.data());
std::string prefix = g_tls_log_prefix();
// Get first (main) listener

View file

@ -84,35 +84,25 @@ namespace logs
{
}
private:
#if __cpp_char8_t >= 201811
using char2 = char8_t;
#else
using char2 = unsigned char;
#endif
#define GEN_LOG_METHOD(_sev)\
const message msg_##_sev{this, level::_sev};\
template <std::size_t N, typename... Args>\
void _sev(const char(&fmt)[N], const Args&... args)\
template <typename CharT, std::size_t N, typename... Args>\
void _sev(const CharT(&fmt)[N], const Args&... args)\
{\
if (level::_sev <= enabled.load(std::memory_order_relaxed)) [[unlikely]]\
{\
static constexpr fmt_type_info type_list[sizeof...(Args) + 1]{fmt_type_info::make<fmt_unveil_t<Args>>()...};\
msg_##_sev.broadcast(fmt, type_list, u64{fmt_unveil<Args>::get(args)}...);\
}\
}\
template <std::size_t N, typename... Args>\
void _sev(const char2(&fmt)[N], const Args&... args)\
{\
if (level::_sev <= enabled.load(std::memory_order_relaxed)) [[unlikely]]\
{\
static constexpr fmt_type_info type_list[sizeof...(Args) + 1]{fmt_type_info::make<fmt_unveil_t<Args>>()...};\
msg_##_sev.broadcast(reinterpret_cast<const char*>(+fmt), type_list, u64{fmt_unveil<Args>::get(args)}...);\
if constexpr (sizeof...(Args) > 0)\
{\
static constexpr fmt_type_info type_list[sizeof...(Args) + 1]{fmt_type_info::make<fmt_unveil_t<Args>>()...};\
msg_##_sev.broadcast(reinterpret_cast<const char*>(fmt), type_list, u64{fmt_unveil<Args>::get(args)}...);\
}\
else\
{\
msg_##_sev.broadcast(reinterpret_cast<const char*>(fmt), nullptr);\
}\
}\
}\
public:
GEN_LOG_METHOD(fatal)
GEN_LOG_METHOD(error)
GEN_LOG_METHOD(todo)