From 2db98d136394bfd1c23c9a8f3eb4e69571dbc9c4 Mon Sep 17 00:00:00 2001 From: Brian Szmyd Date: Wed, 13 Dec 2023 10:16:06 -0700 Subject: [PATCH] Logging improvements. (#198) Logging modules are locally defined by SISL_LOGGING_DEF(...) now to allow forward compat. --- .clang-format | 1 - conanfile.py | 5 +-- include/sisl/logging/logging.h | 12 +++---- src/flip/lib/flip_rpc_server.cpp | 2 ++ src/grpc/rpc_server.cpp | 2 ++ src/logging/logging.cpp | 60 +++++++++++++++----------------- src/logging/test/example.cpp | 2 ++ test_package/example_decl.cpp | 2 +- test_package/test_package.cpp | 1 + 9 files changed, 43 insertions(+), 44 deletions(-) diff --git a/.clang-format b/.clang-format index 2f771200..fdfa11f5 100644 --- a/.clang-format +++ b/.clang-format @@ -18,7 +18,6 @@ AlignOperands: false AlignTrailingComments: true AllowShortBlocksOnASingleLine: true AllowShortIfStatementsOnASingleLine: true -AllowShortBlocksOnASingleLine: true AllowShortCaseLabelsOnASingleLine: false # AllowShortFunctionsOnASingleLine: InlineOnly # AllowShortLoopsOnASingleLine: false diff --git a/conanfile.py b/conanfile.py index 3c86519c..6285d60b 100644 --- a/conanfile.py +++ b/conanfile.py @@ -8,7 +8,7 @@ class SISLConan(ConanFile): name = "sisl" - version = "11.0.2" + version = "11.0.3" homepage = "https://github.com/eBay/sisl" description = "Library for fast data structures, utilities" @@ -148,9 +148,6 @@ def package(self): def package_info(self): self.cpp_info.libs = ["sisl"] - if self.settings.compiler == "gcc": - self.cpp_info.cppflags.extend(["-fconcepts"]) - if self.settings.os == "Linux": self.cpp_info.libs.append("flip") self.cpp_info.cppflags.append("-D_POSIX_C_SOURCE=200809L") diff --git a/include/sisl/logging/logging.h b/include/sisl/logging/logging.h index 4d643ab4..3738ac97 100644 --- a/include/sisl/logging/logging.h +++ b/include/sisl/logging/logging.h @@ -36,10 +36,7 @@ #include #include #include -#include -#include #include -#include #include #include // NOTE: There is an ordering dependecy on this header and fmt headers below #include @@ -245,7 +242,7 @@ constexpr const char* file_name(const char* const str) { return str_slant(str) ? #define _ABORT_OR_DUMP(is_log_assert) \ assert(0); \ if (is_log_assert) { \ - if (sisl::logging::is_crash_handler_installed()) { raise(SIGUSR3); } \ + if (sisl::logging::is_crash_handler_installed()) { raise(SIGUSR3); } \ } else { \ abort(); \ } @@ -266,7 +263,7 @@ constexpr const char* file_name(const char* const str) { return str_slant(str) ? * LOGMSG_ASSERT: If condition is not met: Logs the message with stack trace, aborts in debug build only. * DEBUG_ASSERT: No-op in release build, for debug build, if condition is not met, logs the message and aborts */ -//#if __cplusplus > 201703L +// #if __cplusplus > 201703L #if 0 #define _GENERIC_ASSERT(is_log_assert, cond, formatter, msg, ...) \ [[unlikely]] if (!(cond)) { _LOG_AND_ASSERT_FMT(is_log_assert, formatter, msg, ##__VA_ARGS__); } @@ -448,9 +445,10 @@ MODLEVELDEC(_, _, base) #define SISL_LOGGING_DECL(...) \ BOOST_PP_SEQ_FOR_EACH(MODLEVELDEC, spdlog::level::level_enum::off, BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__)) +#define SISL_LOGGING_DEF(...) \ + BOOST_PP_SEQ_FOR_EACH(MODLEVELDEF, spdlog::level::level_enum::err, BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__)) + #define SISL_LOGGING_INIT(...) \ - BOOST_PP_SEQ_FOR_EACH(MODLEVELDEF, spdlog::level::level_enum::info, \ - BOOST_PP_TUPLE_TO_SEQ(BOOST_PP_VARIADIC_TO_TUPLE(__VA_ARGS__))) \ sisl::logging::InitModules s_init_enabled_mods{ \ BOOST_PP_SEQ_FOR_EACH(MOD_LEVEL_STRING, , BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__))}; diff --git a/src/flip/lib/flip_rpc_server.cpp b/src/flip/lib/flip_rpc_server.cpp index d60b01e6..07cc20bc 100644 --- a/src/flip/lib/flip_rpc_server.cpp +++ b/src/flip/lib/flip_rpc_server.cpp @@ -25,6 +25,8 @@ #include "sisl/flip/flip_rpc_server.hpp" #include "sisl/flip/flip.hpp" +SISL_LOGGING_DEF(flip) + namespace flip { grpc::Status FlipRPCServer::InjectFault(grpc::ServerContext*, const FlipSpec* request, FlipResponse* response) { LOGTRACEMOD(flip, "InjectFault request = {}", request->DebugString()); diff --git a/src/grpc/rpc_server.cpp b/src/grpc/rpc_server.cpp index 45ba425e..f23afc88 100644 --- a/src/grpc/rpc_server.cpp +++ b/src/grpc/rpc_server.cpp @@ -26,6 +26,8 @@ extern "C" { #include +SISL_LOGGING_DEF(grpc_server) + namespace sisl { GrpcServer::GrpcServer(const std::string& listen_addr, uint32_t threads, const std::string& ssl_key, const std::string& ssl_cert) : diff --git a/src/logging/logging.cpp b/src/logging/logging.cpp index 8c51e27b..f2bd808d 100644 --- a/src/logging/logging.cpp +++ b/src/logging/logging.cpp @@ -57,10 +57,7 @@ SISL_OPTION_GROUP(logging, (enab_mods, "", "log_mods", "Module loggers to enabl (version, "V", "version", "Print the version and exist", ::cxxopts::value(), "")) // clang-format on -// logger required define if not inited -extern "C" { -spdlog::level::level_enum module_level_base{spdlog::level::level_enum::info}; -} +SISL_LOGGING_DEF(base) namespace sisl { namespace logging { @@ -263,37 +260,38 @@ static std::string setup_modules() { fmt::vformat_to(std::back_inserter(out_str), fmt::string_view{"{}={}, "}, fmt::make_format_args(mod_name, lvl_str)); } - } else { - if (SISL_OPTIONS.count("log_mods")) { - std::regex re{"[\\s,]+"}; - const auto s{SISL_OPTIONS["log_mods"].as< std::string >()}; - std::sregex_token_iterator it{std::cbegin(s), std::cend(s), re, -1}; - std::sregex_token_iterator reg_end; - for (; it != reg_end; ++it) { - auto mod_stream{std::istringstream(it->str())}; - std::string module_name, module_level; - std::getline(mod_stream, module_name, ':'); - const auto sym{std::string{"module_level_"} + module_name}; - if (auto* const mod_level{ - static_cast< spdlog::level::level_enum* >(::dlsym(RTLD_DEFAULT, sym.c_str()))}; - nullptr != mod_level) { - if (std::getline(mod_stream, module_level, ':')) { - *mod_level = (1 == module_level.size()) - ? static_cast< spdlog::level::level_enum >(std::strtol(module_level.data(), nullptr, 0)) - : spdlog::level::from_str(module_level.data()); - } - } else { - LOGWARN("Could not load module logger: {}\n{}", module_name, dlerror()); + } else + set_module_log_level("base", spdlog::level::level_enum::info); + + if (SISL_OPTIONS.count("log_mods")) { + std::regex re{"[\\s,]+"}; + const auto s{SISL_OPTIONS["log_mods"].as< std::string >()}; + std::sregex_token_iterator it{std::cbegin(s), std::cend(s), re, -1}; + std::sregex_token_iterator reg_end; + for (; it != reg_end; ++it) { + auto mod_stream{std::istringstream(it->str())}; + std::string module_name, module_level; + std::getline(mod_stream, module_name, ':'); + const auto sym{std::string{"module_level_"} + module_name}; + if (auto* const mod_level{static_cast< spdlog::level::level_enum* >(::dlsym(RTLD_DEFAULT, sym.c_str()))}; + nullptr != mod_level) { + if (std::getline(mod_stream, module_level, ':')) { + *mod_level = (1 == module_level.size()) + ? static_cast< spdlog::level::level_enum >(std::strtol(module_level.data(), nullptr, 0)) + : spdlog::level::from_str(module_level.data()); } + } else { + std::cout << fmt::format("Unable to locate the module {} in registered modules, error: {}\n", + module_name, dlerror()); } } + } - for (size_t mod_num{0}; mod_num < glob_num_mods; ++mod_num) { - const std::string& mod_name{glob_enabled_mods[mod_num]}; - fmt::vformat_to( - std::back_inserter(out_str), fmt::string_view{"{}={}, "}, - fmt::make_format_args(mod_name, spdlog::level::to_string_view(GetModuleLogLevel(mod_name)).data())); - } + for (size_t mod_num{0}; mod_num < glob_num_mods; ++mod_num) { + const std::string& mod_name{glob_enabled_mods[mod_num]}; + fmt::vformat_to( + std::back_inserter(out_str), fmt::string_view{"{}={}, "}, + fmt::make_format_args(mod_name, spdlog::level::to_string_view(GetModuleLogLevel(mod_name)).data())); } return out_str; diff --git a/src/logging/test/example.cpp b/src/logging/test/example.cpp index d6df91b4..adc4a675 100644 --- a/src/logging/test/example.cpp +++ b/src/logging/test/example.cpp @@ -24,6 +24,8 @@ #include +SISL_LOGGING_DECL(my_module) +SISL_LOGGING_DEF(my_module) SISL_LOGGING_INIT(my_module) void func() { diff --git a/test_package/example_decl.cpp b/test_package/example_decl.cpp index e6580b2b..2b560a84 100644 --- a/test_package/example_decl.cpp +++ b/test_package/example_decl.cpp @@ -1,6 +1,6 @@ #include -SISL_LOGGING_DECL(my_module) +SISL_LOGGING_DEF(my_module) void example_decl() { LOGINFOMOD(my_module, "Example def!"); diff --git a/test_package/test_package.cpp b/test_package/test_package.cpp index d0b62e0f..1cc93839 100644 --- a/test_package/test_package.cpp +++ b/test_package/test_package.cpp @@ -2,6 +2,7 @@ #include #include +SISL_LOGGING_DECL(my_module) SISL_LOGGING_INIT(my_module) SISL_OPTIONS_ENABLE(logging)