From 4d1baa17bee4e143c8af3c7ca37a5094f3199fd4 Mon Sep 17 00:00:00 2001 From: Diavolo Date: Wed, 28 Dec 2022 11:03:14 +0100 Subject: [PATCH] [Logger]: Refactor --- src/Components/Modules/Logger.cpp | 10 +++--- src/Components/Modules/Logger.hpp | 59 ++++++++++++++++++------------- src/STDInclude.hpp | 1 - src/Steam/Proxy.cpp | 11 +++--- 4 files changed, 46 insertions(+), 35 deletions(-) diff --git a/src/Components/Modules/Logger.cpp b/src/Components/Modules/Logger.cpp index 06cfa3f8..b87a7813 100644 --- a/src/Components/Modules/Logger.cpp +++ b/src/Components/Modules/Logger.cpp @@ -58,7 +58,7 @@ namespace Components } } - void Logger::DebugInternal(std::string_view fmt, std::format_args&& args, [[maybe_unused]] const std::source_location& loc) + void Logger::DebugInternal(const std::string_view& fmt, std::format_args&& args, [[maybe_unused]] const std::source_location& loc) { #ifdef LOGGER_TRACE const auto msg = std::vformat(fmt, args); @@ -71,14 +71,14 @@ namespace Components MessagePrint(Game::CON_CHANNEL_DONT_FILTER, out); } - void Logger::PrintInternal(Game::conChannel_t channel, std::string_view fmt, std::format_args&& args) + void Logger::PrintInternal(Game::conChannel_t channel, const std::string_view& fmt, std::format_args&& args) { const auto msg = std::vformat(fmt, args); MessagePrint(channel, msg); } - void Logger::ErrorInternal(const Game::errorParm_t error, const std::string_view fmt, std::format_args&& args) + void Logger::ErrorInternal(const Game::errorParm_t error, const std::string_view& fmt, std::format_args&& args) { #ifdef _DEBUG if (IsDebuggerPresent()) __debugbreak(); @@ -88,7 +88,7 @@ namespace Components Game::Com_Error(error, "%s", msg.data()); } - void Logger::PrintErrorInternal(Game::conChannel_t channel, std::string_view fmt, std::format_args&& args) + void Logger::PrintErrorInternal(Game::conChannel_t channel, const std::string_view& fmt, std::format_args&& args) { const auto msg = "^1Error: " + std::vformat(fmt, args); @@ -101,7 +101,7 @@ namespace Components } } - void Logger::WarningInternal(Game::conChannel_t channel, std::string_view fmt, std::format_args&& args) + void Logger::WarningInternal(Game::conChannel_t channel, const std::string_view& fmt, std::format_args&& args) { const auto msg = "^3" + std::vformat(fmt, args); diff --git a/src/Components/Modules/Logger.hpp b/src/Components/Modules/Logger.hpp index ff976a45..2aeace2b 100644 --- a/src/Components/Modules/Logger.hpp +++ b/src/Components/Modules/Logger.hpp @@ -14,87 +14,98 @@ namespace Components static void PipeOutput(const std::function& callback); - static void PrintInternal(Game::conChannel_t channel, std::string_view fmt, std::format_args&& args); - static void ErrorInternal(Game::errorParm_t error, std::string_view fmt, std::format_args&& args); - static void PrintErrorInternal(Game::conChannel_t channel, std::string_view fmt, std::format_args&& args); - static void WarningInternal(Game::conChannel_t channel, std::string_view fmt, std::format_args&& args); - static void DebugInternal(std::string_view fmt, std::format_args&& args, const std::source_location& loc); + static void PrintInternal(Game::conChannel_t channel, const std::string_view& fmt, std::format_args&& args); + static void ErrorInternal(Game::errorParm_t error, const std::string_view& fmt, std::format_args&& args); + static void PrintErrorInternal(Game::conChannel_t channel, const std::string_view& fmt, std::format_args&& args); + static void WarningInternal(Game::conChannel_t channel, const std::string_view& fmt, std::format_args&& args); + static void DebugInternal(const std::string_view& fmt, std::format_args&& args, const std::source_location& loc); - static void Print(std::string_view fmt) + static void Print(const std::string_view& fmt) { PrintInternal(Game::CON_CHANNEL_DONT_FILTER, fmt, std::make_format_args(0)); } - static void Print(Game::conChannel_t channel, std::string_view fmt) + static void Print(Game::conChannel_t channel, const std::string_view& fmt) { PrintInternal(channel, fmt, std::make_format_args(0)); } template - static void Print(std::string_view fmt, Args&&... args) + static void Print(const std::string_view& fmt, Args&&... args) { (Utils::String::SanitizeFormatArgs(args), ...); PrintInternal(Game::CON_CHANNEL_DONT_FILTER, fmt, std::make_format_args(args...)); } template - static void Print(Game::conChannel_t channel, std::string_view fmt, Args&&... args) + static void Print(Game::conChannel_t channel, const std::string_view& fmt, Args&&... args) { (Utils::String::SanitizeFormatArgs(args), ...); PrintInternal(channel, fmt, std::make_format_args(args...)); } - static void Error(Game::errorParm_t error, std::string_view fmt) + static void Error(Game::errorParm_t error, const std::string_view& fmt) { ErrorInternal(error, fmt, std::make_format_args(0)); } template - static void Error(Game::errorParm_t error, std::string_view fmt, Args&&... args) + static void Error(Game::errorParm_t error, const std::string_view& fmt, Args&&... args) { (Utils::String::SanitizeFormatArgs(args), ...); ErrorInternal(error, fmt, std::make_format_args(args...)); } - static void Warning(Game::conChannel_t channel, std::string_view fmt) + static void Warning(Game::conChannel_t channel, const std::string_view& fmt) { WarningInternal(channel, fmt, std::make_format_args(0)); } template - static void Warning(Game::conChannel_t channel, std::string_view fmt, Args&&... args) + static void Warning(Game::conChannel_t channel, const std::string_view& fmt, Args&&... args) { (Utils::String::SanitizeFormatArgs(args), ...); WarningInternal(channel, fmt, std::make_format_args(args...)); } - static void PrintError(Game::conChannel_t channel, std::string_view fmt) + static void PrintError(Game::conChannel_t channel, const std::string_view& fmt) { PrintErrorInternal(channel, fmt, std::make_format_args(0)); } template - static void PrintError(Game::conChannel_t channel, std::string_view fmt, Args&&... args) + static void PrintError(Game::conChannel_t channel, const std::string_view& fmt, Args&&... args) { (Utils::String::SanitizeFormatArgs(args), ...); PrintErrorInternal(channel, fmt, std::make_format_args(args...)); } - template - class Debug + struct FormatWithLocation { - public: - Debug([[maybe_unused]] std::string_view fmt, [[maybe_unused]] const Args&... args, [[maybe_unused]] const std::source_location& loc = std::source_location::current()) + std::string_view format; + std::source_location location; + + FormatWithLocation(const std::string_view& fmt, std::source_location loc = std::source_location::current()) + : format(fmt) + , location(std::move(loc)) + { + } + + FormatWithLocation(const char* fmt, std::source_location loc = std::source_location::current()) + : format(fmt) + , location(std::move(loc)) { -#ifdef _DEBUG - (Utils::String::SanitizeFormatArgs(args), ...); - DebugInternal(fmt, std::make_format_args(args...), loc); -#endif } }; template - Debug(std::string_view fmt, const Args&... args) -> Debug; + static void Debug([[maybe_unused]] const FormatWithLocation& f, [[maybe_unused]] const Args&... args) + { +#ifdef _DEBUG + (Utils::String::SanitizeFormatArgs(args), ...); + DebugInternal(f.format, std::make_format_args(args...), f.location); +#endif + } private: static std::mutex MessageMutex; diff --git a/src/STDInclude.hpp b/src/STDInclude.hpp index 62a39359..776d1211 100644 --- a/src/STDInclude.hpp +++ b/src/STDInclude.hpp @@ -80,7 +80,6 @@ #include #include -#include #include // Enable additional literals diff --git a/src/Steam/Proxy.cpp b/src/Steam/Proxy.cpp index 4d352322..9ec04148 100644 --- a/src/Steam/Proxy.cpp +++ b/src/Steam/Proxy.cpp @@ -1,4 +1,5 @@ #include +#include namespace Steam { @@ -6,11 +7,11 @@ namespace Steam ::Utils::Library Proxy::Overlay; ISteamClient008* Proxy::SteamClient = nullptr; - IClientEngine* Proxy::ClientEngine = nullptr; - Interface Proxy::ClientUser; - Interface Proxy::ClientFriends; + IClientEngine* Proxy::ClientEngine = nullptr; + Interface Proxy::ClientUser; + Interface Proxy::ClientFriends; - Interface Proxy::Placeholder; + Interface Proxy::Placeholder; Proxy::Handle Proxy::SteamPipe = nullptr; Proxy::Handle Proxy::SteamUser = nullptr; @@ -237,7 +238,7 @@ namespace Steam { std::lock_guard _(Proxy::CallMutex); - for(auto i = Proxy::Calls.begin(); i != Proxy::Calls.end(); ++i) + for (auto i = Proxy::Calls.begin(); i != Proxy::Calls.end(); ++i) { if(i->handled) {