From 803b29146ea4394d008daecdbc11f55f91b20612 Mon Sep 17 00:00:00 2001 From: Cocoa Date: Wed, 14 Sep 2022 17:57:36 +0800 Subject: [PATCH] Fixed memory error with SHLogger & spdlog The logger needs to be registered outside the library. As such, a macro SHLOG_REGISTER has been added, as well as a variable to the actual logger. The logger needs to be passed into the macro anywhere outside the library after it has been initialised to work on the dll. This has already been done right before the application runs. --- SHADE_Application/src/WinMain.cpp | 6 ++++-- SHADE_Engine/src/Tools/SHLogger.cpp | 17 ++++++++++------- SHADE_Engine/src/Tools/SHLogger.h | 13 ++++++++++--- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/SHADE_Application/src/WinMain.cpp b/SHADE_Application/src/WinMain.cpp index ea1ac5fe..e993d23f 100644 --- a/SHADE_Application/src/WinMain.cpp +++ b/SHADE_Application/src/WinMain.cpp @@ -26,13 +26,15 @@ INT WINAPI wWinMain ) { const SHADE::SHLogger::Config LOGGER_CONFIG{ .directoryPath = "./logs/" }; - SHADE::SHLogger::Initialise(LOGGER_CONFIG); + auto logger = SHADE::SHLogger::Initialise(LOGGER_CONFIG); try { #ifndef SHEDITOR //ShowWindow(::GetConsoleWindow(), SW_HIDE); - #endif + #endif + + SHLOG_REGISTER(logger) SHLOG_INFO("sup") diff --git a/SHADE_Engine/src/Tools/SHLogger.cpp b/SHADE_Engine/src/Tools/SHLogger.cpp index 7b39e979..9b6e5da2 100644 --- a/SHADE_Engine/src/Tools/SHLogger.cpp +++ b/SHADE_Engine/src/Tools/SHLogger.cpp @@ -30,8 +30,9 @@ namespace SHADE /*-----------------------------------------------------------------------------------*/ /* Static Data Member Definitions */ /*-----------------------------------------------------------------------------------*/ - unsigned char SHLogger::configFlags = DEFAULT_CONFIG_FLAG; - SHLogger::DateFormat SHLogger::dateFormat = SHLogger::DateFormat::DD_MM_YY; + unsigned char SHLogger::configFlags = DEFAULT_CONFIG_FLAG; + SHLogger::DateFormat SHLogger::dateFormat = SHLogger::DateFormat::DD_MM_YY; + SHLogger::Logger SHLogger::logger = nullptr; std::string SHLogger::trivialPattern; std::string SHLogger::verbosePattern; @@ -142,7 +143,7 @@ namespace SHADE /* Public Function Member Definitions */ /*-----------------------------------------------------------------------------------*/ - void SHLogger::Initialise(const Config& config) + SHLogger::Logger SHLogger::Initialise(const Config& config) { SetConfig(config); @@ -176,10 +177,10 @@ namespace SHADE FILE_SINK->set_pattern(trivialPattern + "%v"); // Create and register logger with spdlog - const auto LOGGER = std::make_shared(SHLOGGER_NAME, sinks.begin(), sinks.end()); - LOGGER->set_level(spdlog::level::trace); - LOGGER->flush_on(spdlog::level::trace); - register_logger(LOGGER); + logger = std::make_shared(SHLOGGER_NAME, sinks.begin(), sinks.end()); + logger->set_level(spdlog::level::trace); + logger->flush_on(spdlog::level::trace); + spdlog::register_logger(logger); // Flush every 3 seconds spdlog::flush_every(std::chrono::seconds(config.flushTime)); @@ -190,6 +191,8 @@ namespace SHADE { std::cout << "Log initialisation failed: " << e.what() << std::endl; } + + return logger; } void SHLogger::Shutdown() noexcept diff --git a/SHADE_Engine/src/Tools/SHLogger.h b/SHADE_Engine/src/Tools/SHLogger.h index dfb7dffd..f6ffda47 100644 --- a/SHADE_Engine/src/Tools/SHLogger.h +++ b/SHADE_Engine/src/Tools/SHLogger.h @@ -35,6 +35,8 @@ namespace SHADE /* Type Definitions */ /*---------------------------------------------------------------------------------*/ + using Logger = std::shared_ptr; + enum class ClockFormat { _12HR, _24HR }; enum class DateFormat @@ -107,7 +109,7 @@ namespace SHADE * @brief Creates a console and a file to log to. * @param[in] config The configuration parameters for the logger. */ - static void Initialise (const Config& config = Config{}); + static Logger Initialise (const Config& config = Config{}); static void Shutdown () noexcept; /** @@ -127,7 +129,9 @@ namespace SHADE static constexpr short DEFAULT_CONSOLE_LEN = 1024; static unsigned char configFlags; // Initialised 0 0 FuncLine# FuncFileName Date TimeFormat Time - static DateFormat dateFormat; + static DateFormat dateFormat; + + static Logger logger; static std::string trivialPattern; static std::string verbosePattern; @@ -172,4 +176,7 @@ namespace SHADE #define SHLOGV_CRITICAL(format, ...) SHADE::SHLogger::UseVerbosePattern(); SPDLOG_LOGGER_CRITICAL(spdlog::get(SHLOGGER_NAME), format, ## __VA_ARGS__); // Misc Logging Macros -#define SHLOG_FLOOR() SHADE::SHLogger::UseTrivialPattern(); SPDLOG_LOGGER_INFO(spdlog::get(SHLOGGER_NAME), "--------------------------------"); \ No newline at end of file +#define SHLOG_FLOOR() SHADE::SHLogger::UseTrivialPattern(); SPDLOG_LOGGER_INFO(spdlog::get(SHLOGGER_NAME), "--------------------------------"); + +// For use outside the library to register the logger +#define SHLOG_REGISTER(logger) spdlog::register_logger(logger); spdlog::set_level(spdlog::level::level_enum::trace); \ No newline at end of file