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,7 +26,7 @@ INT WINAPI wWinMain
)
{
const SHADE::SHLogger::Config LOGGER_CONFIG{ .directoryPath = "./logs/" };
SHADE::SHLogger::Initialise(LOGGER_CONFIG);
auto logger = SHADE::SHLogger::Initialise(LOGGER_CONFIG);
try
{
@ -34,6 +34,8 @@ INT WINAPI wWinMain
//ShowWindow(::GetConsoleWindow(), SW_HIDE);
#endif
SHLOG_REGISTER(logger)
SHLOG_INFO("sup")
SHADE::SHEngine::Run<Sandbox::SBApplication>(hInstance, hPrevInstance, lpCmdLine, nCmdShow);

View File

@ -32,6 +32,7 @@ namespace SHADE
/*-----------------------------------------------------------------------------------*/
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<spdlog::logger>(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<spdlog::logger>(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

View File

@ -35,6 +35,8 @@ namespace SHADE
/* Type Definitions */
/*---------------------------------------------------------------------------------*/
using Logger = std::shared_ptr<spdlog::logger>;
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;
/**
@ -129,6 +131,8 @@ namespace SHADE
static unsigned char configFlags; // Initialised 0 0 FuncLine# FuncFileName Date TimeFormat Time
static DateFormat dateFormat;
static Logger logger;
static std::string trivialPattern;
static std::string verbosePattern;
@ -173,3 +177,6 @@ namespace SHADE
// Misc Logging Macros
#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);