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:
parent
44ef3a500f
commit
803b29146e
|
@ -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")
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -127,7 +129,9 @@ namespace SHADE
|
||||||
static constexpr short DEFAULT_CONSOLE_LEN = 1024;
|
static constexpr short DEFAULT_CONSOLE_LEN = 1024;
|
||||||
|
|
||||||
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;
|
||||||
|
@ -172,4 +176,7 @@ namespace SHADE
|
||||||
#define SHLOGV_CRITICAL(format, ...) SHADE::SHLogger::UseVerbosePattern(); SPDLOG_LOGGER_CRITICAL(spdlog::get(SHLOGGER_NAME), format, ## __VA_ARGS__);
|
#define SHLOGV_CRITICAL(format, ...) SHADE::SHLogger::UseVerbosePattern(); SPDLOG_LOGGER_CRITICAL(spdlog::get(SHLOGGER_NAME), format, ## __VA_ARGS__);
|
||||||
|
|
||||||
// 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);
|
Loading…
Reference in New Issue