From e45e589ba9b05d428db082a1ef9d8fc58634c499 Mon Sep 17 00:00:00 2001 From: Cocoa Date: Tue, 13 Sep 2022 23:29:20 +0800 Subject: [PATCH] Added functions that wrap around logging macros for C++/CLI integration --- .../src/Application/SBApplication.cpp | 5 +- SHADE_Application/src/WinMain.cpp | 2 - SHADE_Engine/SHADE_Engine.vcxproj | 1 + SHADE_Engine/SHADE_Engine.vcxproj.filters | 1 + SHADE_Engine/src/Tools/SHLogger.cpp | 137 ++++++++++++++++++ SHADE_Engine/src/Tools/SHLogger.h | 56 ++++--- 6 files changed, 180 insertions(+), 22 deletions(-) diff --git a/SHADE_Application/src/Application/SBApplication.cpp b/SHADE_Application/src/Application/SBApplication.cpp index 184b9611..0cb1ecd1 100644 --- a/SHADE_Application/src/Application/SBApplication.cpp +++ b/SHADE_Application/src/Application/SBApplication.cpp @@ -6,6 +6,8 @@ #include "Scenes/SBEditorScene.h" #endif // SHEDITOR +#include "Tools/SHLogger.h" + #include #include #include @@ -21,7 +23,8 @@ namespace Sandbox _In_ INT nCmdShow ) { - + SHLOG_TITLE("Initialising SBApplication") + window.Create(hInstance, hPrevInstance, lpCmdLine, nCmdShow); #ifdef SHEDITOR diff --git a/SHADE_Application/src/WinMain.cpp b/SHADE_Application/src/WinMain.cpp index ea1ac5fe..956566ab 100644 --- a/SHADE_Application/src/WinMain.cpp +++ b/SHADE_Application/src/WinMain.cpp @@ -34,8 +34,6 @@ INT WINAPI wWinMain //ShowWindow(::GetConsoleWindow(), SW_HIDE); #endif - SHLOG_INFO("sup") - SHADE::SHEngine::Run(hInstance, hPrevInstance, lpCmdLine, nCmdShow); _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); } diff --git a/SHADE_Engine/SHADE_Engine.vcxproj b/SHADE_Engine/SHADE_Engine.vcxproj index e54c82ec..2feaa3ac 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj +++ b/SHADE_Engine/SHADE_Engine.vcxproj @@ -174,6 +174,7 @@ + diff --git a/SHADE_Engine/SHADE_Engine.vcxproj.filters b/SHADE_Engine/SHADE_Engine.vcxproj.filters index 7486fad4..3e537216 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj.filters +++ b/SHADE_Engine/SHADE_Engine.vcxproj.filters @@ -382,6 +382,7 @@ Tools + diff --git a/SHADE_Engine/src/Tools/SHLogger.cpp b/SHADE_Engine/src/Tools/SHLogger.cpp index 7b39e979..768dc084 100644 --- a/SHADE_Engine/src/Tools/SHLogger.cpp +++ b/SHADE_Engine/src/Tools/SHLogger.cpp @@ -211,6 +211,143 @@ namespace SHADE sinks[1]->set_pattern(verbosePattern + "%v"); // File Sink } + void SHLogger::LogInfo(const std::string& msg) noexcept + { + SHLOG_INFO(msg) + } + + void SHLogger::LogVerboseInfo(const std::string& msg, const std::source_location& src) noexcept + { + const bool SHOW_SRC_FILE = configFlags & (1U << 3); + const bool SHOW_SRC_LINE = configFlags & (1U << 4); + + std::stringstream ss; + ss << "["; + if (SHOW_SRC_FILE) + { + ss << std::filesystem::path(src.file_name()).filename().string() << ", "; + if (SHOW_SRC_LINE) + { + ss << src.line() << ", "; + } + } + + ss << src.function_name() << "] " << msg; + + SHLOG_INFO(ss.str()) + } + + void SHLogger::LogWarning(const std::string& msg) noexcept + { + SHLOG_WARNING(msg) + } + + void SHLogger::LogVerboseWarning(const std::string& msg, const std::source_location& src) noexcept + { + const bool SHOW_SRC_FILE = configFlags & (1U << 3); + const bool SHOW_SRC_LINE = configFlags & (1U << 4); + + std::stringstream ss; + ss << "["; + if (SHOW_SRC_FILE) + { + ss << std::filesystem::path(src.file_name()).filename().string() << ", "; + if (SHOW_SRC_LINE) + { + ss << src.line() << ", "; + } + } + + ss << src.function_name() << "] " << msg; + + SHLOG_WARNING(ss.str()) + } + + void SHLogger::LogError(const std::string& msg) noexcept + { + SHLOG_ERROR(msg) + } + + void SHLogger::LogVerboseError(const std::string& msg, const std::source_location& src) noexcept + { + const bool SHOW_SRC_FILE = configFlags & (1U << 3); + const bool SHOW_SRC_LINE = configFlags & (1U << 4); + + std::stringstream ss; + ss << "["; + if (SHOW_SRC_FILE) + { + ss << std::filesystem::path(src.file_name()).filename().string() << ", "; + if (SHOW_SRC_LINE) + { + ss << src.line() << ", "; + } + } + + ss << src.function_name() << "] " << msg; + + SHLOG_ERROR(ss.str()) + } + + void SHLogger::LogCritical(const std::string& msg) noexcept + { + SHLOG_CRITICAL(msg) + } + + void SHLogger::LogVerboseCritical(const std::string& msg, const std::source_location& src) noexcept + { + const bool SHOW_SRC_FILE = configFlags & (1U << 3); + const bool SHOW_SRC_LINE = configFlags & (1U << 4); + + std::stringstream ss; + ss << "["; + if (SHOW_SRC_FILE) + { + ss << std::filesystem::path(src.file_name()).filename().string() << ", "; + if (SHOW_SRC_LINE) + { + ss << src.line() << ", "; + } + } + + ss << src.function_name() << "] " << msg; + + SHLOG_CRITICAL(ss.str()) + } + + void SHLogger::LogFloor() noexcept + { + SHLOG_FLOOR() + } + + #ifdef _DEBUG + void SHLogger::LogTrace(const std::string& msg) noexcept + { + SHLOG_TRACE(msg) + } + + void SHLogger::LogVerboseTrace(const std::string& msg, const std::source_location& src) noexcept + { + const bool SHOW_SRC_FILE = configFlags & (1U << 3); + const bool SHOW_SRC_LINE = configFlags & (1U << 4); + + std::stringstream ss; + ss << "["; + if (SHOW_SRC_FILE) + { + ss << std::filesystem::path(src.file_name()).filename().string() << ", "; + if (SHOW_SRC_LINE) + { + ss << src.line() << ", "; + } + } + + ss << src.function_name() << "] " << msg; + + SHLOG_TRACE(ss.str()) + } + #endif + /*-----------------------------------------------------------------------------------*/ /* Private Function Member Definitions */ /*-----------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Tools/SHLogger.h b/SHADE_Engine/src/Tools/SHLogger.h index ac5f9308..3c5c6f1b 100644 --- a/SHADE_Engine/src/Tools/SHLogger.h +++ b/SHADE_Engine/src/Tools/SHLogger.h @@ -12,6 +12,7 @@ #include #include +#include #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE #include @@ -70,33 +71,33 @@ namespace SHADE /* Getter Functions */ /*---------------------------------------------------------------------------------*/ - [[nodiscard]] static const std::string& GetTrivialPattern () noexcept { return trivialPattern; } - [[nodiscard]] static const std::string& GetVerbosePattern () noexcept { return verbosePattern; } + [[nodiscard]] static const std::string& GetTrivialPattern () noexcept { return trivialPattern; } + [[nodiscard]] static const std::string& GetVerbosePattern () noexcept { return verbosePattern; } /*---------------------------------------------------------------------------------*/ /* Setter Functions */ /*---------------------------------------------------------------------------------*/ - static void SetTrivialPattern (const std::string& pattern) noexcept { trivialPattern = pattern; } - static void SetVerbosePattern (const std::string& pattern) noexcept { verbosePattern = pattern; } + static void SetTrivialPattern (const std::string& pattern) noexcept { trivialPattern = pattern; } + static void SetVerbosePattern (const std::string& pattern) noexcept { verbosePattern = pattern; } - static void SetConfig (const Config& config) noexcept; + static void SetConfig (const Config& config) noexcept; - static void SetShowTime (bool showTime) noexcept; - static void SetShowDate (bool showDate) noexcept; - static void SetShowFunctionFileName (bool showFunctionFileName) noexcept; - static void SetShowFunctionLineNumber (bool showFunctionLineNumber) noexcept; + static void SetShowTime (bool showTime) noexcept; + static void SetShowDate (bool showDate) noexcept; + static void SetShowFunctionFileName (bool showFunctionFileName) noexcept; + static void SetShowFunctionLineNumber (bool showFunctionLineNumber) noexcept; - static void SetClockFormat (ClockFormat newClockFormat) noexcept; - static void SetDateFormat (DateFormat newDateFormat) noexcept; + static void SetClockFormat (ClockFormat newClockFormat) noexcept; + static void SetDateFormat (DateFormat newDateFormat) noexcept; - static void SetFileName (const std::string& logFileName) noexcept; - static void SetDirectoryPath (const std::filesystem::path& logDirectoryPath) noexcept; + static void SetFileName (const std::string& logFileName) noexcept; + static void SetDirectoryPath (const std::filesystem::path& logDirectoryPath) noexcept; - static void SetFlushTime (int seconds) noexcept; - static void SetFlushTime (size_t seconds) noexcept { spdlog::flush_every(std::chrono::seconds(seconds)); } + static void SetFlushTime (int seconds) noexcept; + static void SetFlushTime (size_t seconds) noexcept { spdlog::flush_every(std::chrono::seconds(seconds)); } /*---------------------------------------------------------------------------------*/ /* Function Members */ @@ -107,16 +108,33 @@ namespace SHADE * @param[in] config The configuration parameters for the logger. */ static void Initialise (const Config& config = Config{}); - static void Shutdown () noexcept; + static void Shutdown () noexcept; /** * @brief The next message logged by the logger will be set to follow the trivial pattern. */ - static void UseTrivialPattern () noexcept; + static void UseTrivialPattern () noexcept; /** * @brief The next message logged by the logger will be set to follow the verbose pattern. */ - static void UseVerbosePattern () noexcept; + static void UseVerbosePattern () noexcept; + + /// Logging Functions to interface with C++/CLI. + + static void LogInfo (const std::string& msg) noexcept; + static void LogVerboseInfo (const std::string& msg, const std::source_location& src= std::source_location::current()) noexcept; + static void LogWarning (const std::string& msg) noexcept; + static void LogVerboseWarning (const std::string& msg, const std::source_location& src = std::source_location::current()) noexcept; + static void LogError (const std::string& msg) noexcept; + static void LogVerboseError (const std::string& msg, const std::source_location& src = std::source_location::current()) noexcept; + static void LogCritical (const std::string& msg) noexcept; + static void LogVerboseCritical (const std::string& msg, const std::source_location& src = std::source_location::current()) noexcept; + static void LogFloor () noexcept; + + #ifdef _DEBUG + static void LogTrace (const std::string& msg) noexcept; + static void LogVerboseTrace (const std::string& msg, const std::source_location& src = std::source_location::current()) noexcept; + #endif private: /*---------------------------------------------------------------------------------*/ @@ -171,4 +189,4 @@ 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), "--------------------------------");