From 0bafe79ee3dc959b2119b2e992c8fca7f553d643 Mon Sep 17 00:00:00 2001 From: Qudix <17361645+Qudix@users.noreply.github.com> Date: Thu, 28 Sep 2023 09:34:48 -0500 Subject: [PATCH] feat: add safer/convenient console log function (#116) - this log function makes use of `std::format` and the benefits that comes with (compile time checking, type representation, numbered fields, etc.) compared to the printf style function. ```cpp auto log = RE::ConsoleLog::GetSingleton(); log->Log("Value: {}", true); ``` ``` Value: true ``` --- CommonLibSF/include/RE/C/ConsoleLog.h | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/CommonLibSF/include/RE/C/ConsoleLog.h b/CommonLibSF/include/RE/C/ConsoleLog.h index 322a1efe..703bb45b 100644 --- a/CommonLibSF/include/RE/C/ConsoleLog.h +++ b/CommonLibSF/include/RE/C/ConsoleLog.h @@ -14,6 +14,14 @@ namespace RE return *singleton; } + void VPrint(const char* a_fmt, std::va_list a_args) + { + using func_t = decltype(&ConsoleLog::VPrint); + REL::Relocation func{ REL::ID(166358) }; + func(this, a_fmt, a_args); + } + + // printf format rules, no compile time checking void Print(const char* a_fmt, ...) { std::va_list args; @@ -22,11 +30,11 @@ namespace RE va_end(args); } - void VPrint(const char* a_fmt, std::va_list a_args) + // std::format rules, compile time checking + template + void Log(const std::format_string a_fmt, Args&&... a_args) { - using func_t = decltype(&ConsoleLog::VPrint); - REL::Relocation func{ REL::ID(166358) }; - func(this, a_fmt, a_args); + Print(std::vformat(a_fmt.get(), std::make_format_args(a_args...)).c_str()); } }; }