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.
This commit is contained in:
Cocoa 2022-09-14 17:57:36 +08:00
parent 44ef3a500f
commit 803b29146e
3 changed files with 24 additions and 12 deletions

View File

@ -26,13 +26,15 @@ INT WINAPI wWinMain
) )
{ {
const SHADE::SHLogger::Config LOGGER_CONFIG{ .directoryPath = "./logs/" }; const SHADE::SHLogger::Config LOGGER_CONFIG{ .directoryPath = "./logs/" };
SHADE::SHLogger::Initialise(LOGGER_CONFIG); auto logger = SHADE::SHLogger::Initialise(LOGGER_CONFIG);
try try
{ {
#ifndef SHEDITOR #ifndef SHEDITOR
//ShowWindow(::GetConsoleWindow(), SW_HIDE); //ShowWindow(::GetConsoleWindow(), SW_HIDE);
#endif #endif
SHLOG_REGISTER(logger)
SHLOG_INFO("sup") SHLOG_INFO("sup")

View File

@ -30,8 +30,9 @@ namespace SHADE
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* Static Data Member Definitions */ /* Static Data Member Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
unsigned char SHLogger::configFlags = DEFAULT_CONFIG_FLAG; unsigned char SHLogger::configFlags = DEFAULT_CONFIG_FLAG;
SHLogger::DateFormat SHLogger::dateFormat = SHLogger::DateFormat::DD_MM_YY; SHLogger::DateFormat SHLogger::dateFormat = SHLogger::DateFormat::DD_MM_YY;
SHLogger::Logger SHLogger::logger = nullptr;
std::string SHLogger::trivialPattern; std::string SHLogger::trivialPattern;
std::string SHLogger::verbosePattern; std::string SHLogger::verbosePattern;
@ -142,7 +143,7 @@ namespace SHADE
/* Public Function Member Definitions */ /* Public Function Member Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
void SHLogger::Initialise(const Config& config) SHLogger::Logger SHLogger::Initialise(const Config& config)
{ {
SetConfig(config); SetConfig(config);
@ -176,10 +177,10 @@ namespace SHADE
FILE_SINK->set_pattern(trivialPattern + "%v"); FILE_SINK->set_pattern(trivialPattern + "%v");
// Create and register logger with spdlog // Create and register logger with spdlog
const auto LOGGER = std::make_shared<spdlog::logger>(SHLOGGER_NAME, sinks.begin(), sinks.end()); logger = std::make_shared<spdlog::logger>(SHLOGGER_NAME, sinks.begin(), sinks.end());
LOGGER->set_level(spdlog::level::trace); logger->set_level(spdlog::level::trace);
LOGGER->flush_on(spdlog::level::trace); logger->flush_on(spdlog::level::trace);
register_logger(LOGGER); spdlog::register_logger(logger);
// Flush every 3 seconds // Flush every 3 seconds
spdlog::flush_every(std::chrono::seconds(config.flushTime)); spdlog::flush_every(std::chrono::seconds(config.flushTime));
@ -190,6 +191,8 @@ namespace SHADE
{ {
std::cout << "Log initialisation failed: " << e.what() << std::endl; std::cout << "Log initialisation failed: " << e.what() << std::endl;
} }
return logger;
} }
void SHLogger::Shutdown() noexcept void SHLogger::Shutdown() noexcept

View File

@ -35,6 +35,8 @@ namespace SHADE
/* Type Definitions */ /* Type Definitions */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
using Logger = std::shared_ptr<spdlog::logger>;
enum class ClockFormat { _12HR, _24HR }; enum class ClockFormat { _12HR, _24HR };
enum class DateFormat enum class DateFormat
@ -107,7 +109,7 @@ namespace SHADE
* @brief Creates a console and a file to log to. * @brief Creates a console and a file to log to.
* @param[in] config The configuration parameters for the logger. * @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; static void Shutdown () noexcept;
/** /**
@ -129,6 +131,8 @@ namespace SHADE
static unsigned char configFlags; // Initialised 0 0 FuncLine# FuncFileName Date TimeFormat Time 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 trivialPattern;
static std::string verbosePattern; static std::string verbosePattern;
@ -173,3 +177,6 @@ namespace SHADE
// Misc Logging Macros // Misc Logging Macros
#define SHLOG_FLOOR() SHADE::SHLogger::UseTrivialPattern(); SPDLOG_LOGGER_INFO(spdlog::get(SHLOGGER_NAME), "--------------------------------"); #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);