From f0450a5bf4df4718d0323086bf88c6469df94b54 Mon Sep 17 00:00:00 2001 From: Oliver Hamlet Date: Tue, 12 Sep 2023 19:52:30 +0100 Subject: [PATCH] Refactor logging code --- CMakeLists.txt | 1 + src/api/api.cpp | 6 +-- src/api/helpers/logging.cpp | 88 +++++++++++++++++++++++++++++++++++++ src/api/helpers/logging.h | 50 ++------------------- 4 files changed, 94 insertions(+), 51 deletions(-) create mode 100644 src/api/helpers/logging.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f6bfde59..04c53271 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -208,6 +208,7 @@ set(LIBLOOT_SRC_API_CPP_FILES "${CMAKE_SOURCE_DIR}/src/api/sorting/plugin_sorting_data.cpp" "${CMAKE_SOURCE_DIR}/src/api/sorting/undefined_group_error.cpp" "${CMAKE_SOURCE_DIR}/src/api/helpers/crc.cpp" + "${CMAKE_SOURCE_DIR}/src/api/helpers/logging.cpp" "${CMAKE_SOURCE_DIR}/src/api/helpers/text.cpp" "${CMAKE_SOURCE_DIR}/src/api/vertex.cpp") diff --git a/src/api/api.cpp b/src/api/api.cpp index d0e3f047..a34a0e06 100644 --- a/src/api/api.cpp +++ b/src/api/api.cpp @@ -80,11 +80,9 @@ std::filesystem::path ResolvePath(const std::filesystem::path& path) { LOOT_API void SetLoggingCallback( std::function callback) { - auto sink = std::make_shared(callback); - auto logger = std::make_shared(LOGGER_NAME, sink); - logger->set_level(spdlog::level::level_enum::trace); + const auto logger = createLogger(callback); - spdlog::drop(LOGGER_NAME); + spdlog::drop(logger->name()); spdlog::register_logger(logger); } diff --git a/src/api/helpers/logging.cpp b/src/api/helpers/logging.cpp new file mode 100644 index 00000000..7fbb1493 --- /dev/null +++ b/src/api/helpers/logging.cpp @@ -0,0 +1,88 @@ +/* LOOT + + A load order optimisation tool for Oblivion, Skyrim, Fallout 3 and + Fallout: New Vegas. + + Copyright (C) 2012-2016 WrinklyNinja + + This file is part of LOOT. + + LOOT is free software: you can redistribute + it and/or modify it under the terms of the GNU General Public License + as published by the Free Software Foundation, either version 3 of + the License, or (at your option) any later version. + + LOOT is distributed in the hope that it will + be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with LOOT. If not, see + . + */ + +#include "api/helpers/logging.h" + +#define FMT_USE_STD_STRING_VIEW + +#include + +namespace { +using loot::LogLevel; + +constexpr const char* LOGGER_NAME = "loot_api_logger"; + +LogLevel mapFromSpdlog(spdlog::level::level_enum severity) { + using spdlog::level::level_enum; + switch (severity) { + case level_enum::trace: + return LogLevel::trace; + case level_enum::debug: + return LogLevel::debug; + case level_enum::info: + return LogLevel::info; + case level_enum::warn: + return LogLevel::warning; + case level_enum::err: + return LogLevel::error; + case level_enum::critical: + return LogLevel::fatal; + default: + return LogLevel::trace; + } +} + +class SpdLoggingSink : public spdlog::sinks::base_sink { +public: + explicit SpdLoggingSink(std::function callback) { + this->callback = callback; + } + +protected: + void sink_it_(const spdlog::details::log_msg& msg) override { + // string_view isn't necessarily null-terminated, so using + // msg.payload.data() directly isn't a good idea. + std::string payload = std::string(msg.payload.data(), msg.payload.size()); + callback(mapFromSpdlog(msg.level), payload.c_str()); + } + + void flush_() override {} + +private: + std::function callback; +}; +} + +namespace loot { +std::shared_ptr getLogger() { return spdlog::get(LOGGER_NAME); } + +std::shared_ptr createLogger( + std::function callback) { + auto sink = std::make_shared(callback); + auto logger = std::make_shared(LOGGER_NAME, sink); + logger->set_level(spdlog::level::level_enum::trace); + + return logger; +} +} diff --git a/src/api/helpers/logging.h b/src/api/helpers/logging.h index 9ca31388..a948c4e4 100644 --- a/src/api/helpers/logging.h +++ b/src/api/helpers/logging.h @@ -24,59 +24,15 @@ #ifndef LOOT_API_HELPERS_LOGGING #define LOOT_API_HELPERS_LOGGING -#define FMT_USE_STD_STRING_VIEW - -#include #include #include "loot/enum/log_level.h" namespace loot { -static constexpr const char* LOGGER_NAME = "loot_api_logger"; +std::shared_ptr getLogger(); -inline std::shared_ptr getLogger() { - return spdlog::get(LOGGER_NAME); -} - -class SpdLoggingSink : public spdlog::sinks::base_sink { -public: - explicit SpdLoggingSink(std::function callback) { - this->callback = callback; - } - -protected: - void sink_it_(const spdlog::details::log_msg& msg) override { - // string_view isn't necessarily null-terminated, so using - // msg.payload.data() directly isn't a good idea. - std::string payload = std::string(msg.payload.data(), msg.payload.size()); - callback(mapFromSpdlog(msg.level), payload.c_str()); - } - - void flush_() override {} - -private: - std::function callback; - - static LogLevel mapFromSpdlog(spdlog::level::level_enum severity) { - using spdlog::level::level_enum; - switch (severity) { - case level_enum::trace: - return LogLevel::trace; - case level_enum::debug: - return LogLevel::debug; - case level_enum::info: - return LogLevel::info; - case level_enum::warn: - return LogLevel::warning; - case level_enum::err: - return LogLevel::error; - case level_enum::critical: - return LogLevel::fatal; - default: - return LogLevel::trace; - } - } -}; +std::shared_ptr createLogger( + std::function callback); } #endif