From 4caf726005e467ce3074c943fd248692f423b11e Mon Sep 17 00:00:00 2001 From: DH Date: Fri, 7 Jul 2023 02:03:30 +0300 Subject: [PATCH] [orbis-kernel] Logs: fix crash on char[N] format Cleanup, remove duplicated const formatters Allow filtering by log level --- orbis-kernel/include/orbis/utils/Logs.hpp | 21 ++++++++++----------- orbis-kernel/src/utils/Logs.cpp | 7 +++++-- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/orbis-kernel/include/orbis/utils/Logs.hpp b/orbis-kernel/include/orbis/utils/Logs.hpp index ca1d6480d..e50e8c25b 100644 --- a/orbis-kernel/include/orbis/utils/Logs.hpp +++ b/orbis-kernel/include/orbis/utils/Logs.hpp @@ -26,26 +26,25 @@ template struct log_class_string { static void format(std::string &out, const void *arg); }; -template <> struct log_class_string { +template <> struct log_class_string { static void format(std::string &out, const void *arg); }; template -struct log_class_string : log_class_string {}; +struct log_class_string : log_class_string {}; -template <> struct log_class_string { +template <> struct log_class_string { static void format(std::string &out, const void *arg); }; -template <> -struct log_class_string : log_class_string {}; +template struct log_class_string { + static void format(std::string &out, const void *arg) { + out += reinterpret_cast(arg); + } +}; template <> -struct log_class_string - : log_class_string {}; - -template <> -struct log_class_string : log_class_string {}; +struct log_class_string : log_class_string {}; template using log_args_t = const void *(&&)[sizeof...(Args) + 1]; @@ -62,7 +61,7 @@ struct log_type_info { template constexpr const log_type_info type_info_v[sizeof...(Args) + 1]{ - log_type_info::make>()...}; + log_type_info::make>()...}; void _orbis_log_print(LogLevel lvl, const char *msg, std::string_view names, const log_type_info *sup, ...); diff --git a/orbis-kernel/src/utils/Logs.cpp b/orbis-kernel/src/utils/Logs.cpp index 4d80b0176..8e663cd67 100644 --- a/orbis-kernel/src/utils/Logs.cpp +++ b/orbis-kernel/src/utils/Logs.cpp @@ -13,12 +13,12 @@ static void append_hex(std::string &out, std::uintmax_t value) { } namespace orbis::logs { -void log_class_string::format(std::string &out, const void *arg) { +void log_class_string::format(std::string &out, const void *arg) { const void *ptr = *reinterpret_cast(arg); append_hex(out, reinterpret_cast(ptr)); } -void log_class_string::format(std::string &out, const void *arg) { +void log_class_string::format(std::string &out, const void *arg) { out += *reinterpret_cast(arg); } @@ -137,6 +137,9 @@ void log_class_string::format(std::string &out, const void *arg) { void _orbis_log_print(LogLevel lvl, const char *msg, std::string_view names, const log_type_info *sup, ...) { + if (lvl > logs_level.load(std::memory_order::relaxed)) { + return; + } /*constinit thread_local*/ std::string text; /*constinit thread_local*/ std::vector args;