Skip to content

Commit

Permalink
Logging improvements. (eBay#198)
Browse files Browse the repository at this point in the history
Logging modules are locally defined by SISL_LOGGING_DEF(...) now to allow forward compat.
  • Loading branch information
szmyd authored Dec 13, 2023
1 parent 0328253 commit 2db98d1
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 44 deletions.
1 change: 0 additions & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ AlignOperands: false
AlignTrailingComments: true
AllowShortBlocksOnASingleLine: true
AllowShortIfStatementsOnASingleLine: true
AllowShortBlocksOnASingleLine: true
AllowShortCaseLabelsOnASingleLine: false
# AllowShortFunctionsOnASingleLine: InlineOnly
# AllowShortLoopsOnASingleLine: false
Expand Down
5 changes: 1 addition & 4 deletions conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand Down
12 changes: 5 additions & 7 deletions include/sisl/logging/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@
#include <boost/preprocessor/control/if.hpp>
#include <boost/preprocessor/seq/for_each.hpp>
#include <boost/preprocessor/stringize.hpp>
#include <boost/preprocessor/tuple/push_front.hpp>
#include <boost/preprocessor/tuple/to_seq.hpp>
#include <boost/preprocessor/variadic/to_seq.hpp>
#include <boost/preprocessor/variadic/to_tuple.hpp>
#include <nlohmann/json.hpp>
#include <spdlog/spdlog.h> // NOTE: There is an ordering dependecy on this header and fmt headers below
#include <spdlog/fmt/bin_to_hex.h>
Expand Down Expand Up @@ -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(); \
}
Expand All @@ -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__); }
Expand Down Expand Up @@ -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__))};

Expand Down
2 changes: 2 additions & 0 deletions src/flip/lib/flip_rpc_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 2 additions & 0 deletions src/grpc/rpc_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ extern "C" {

#include <grpcpp/impl/codegen/service_type.h>

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) :
Expand Down
60 changes: 29 additions & 31 deletions src/logging/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool>(), ""))
// 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 {
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/logging/test/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

#include <sisl/logging/logging.h>

SISL_LOGGING_DECL(my_module)
SISL_LOGGING_DEF(my_module)
SISL_LOGGING_INIT(my_module)

void func() {
Expand Down
2 changes: 1 addition & 1 deletion test_package/example_decl.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <sisl/logging/logging.h>

SISL_LOGGING_DECL(my_module)
SISL_LOGGING_DEF(my_module)

void example_decl() {
LOGINFOMOD(my_module, "Example def!");
Expand Down
1 change: 1 addition & 0 deletions test_package/test_package.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <sisl/options/options.h>
#include <sisl/utility/thread_factory.hpp>

SISL_LOGGING_DECL(my_module)
SISL_LOGGING_INIT(my_module)

SISL_OPTIONS_ENABLE(logging)
Expand Down

0 comments on commit 2db98d1

Please sign in to comment.