From 6594b50e57935dd66930ccd35dba7a1b4131399d Mon Sep 17 00:00:00 2001 From: memchr <118117622+memchr@users.noreply.github.com> Date: Wed, 20 Sep 2023 07:26:20 +0000 Subject: logging/format: use std::format_string to catch formatting string errors at compile time (#3377) * fix(log): use constexpr format string * deprecate getFormat --- src/debug/Log.hpp | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) (limited to 'src/debug/Log.hpp') diff --git a/src/debug/Log.hpp b/src/debug/Log.hpp index a65bccb1..125ed7f4 100644 --- a/src/debug/Log.hpp +++ b/src/debug/Log.hpp @@ -9,8 +9,7 @@ #define LOGMESSAGESIZE 1024 -enum LogLevel -{ +enum LogLevel { NONE = -1, LOG = 0, WARN, @@ -29,7 +28,7 @@ namespace Debug { void init(const std::string& IS); template - void log(LogLevel level, const std::string& fmt, Args&&... args) { + void log(LogLevel level, std::format_string fmt, Args&&... args) { if (disableLogs && *disableLogs) return; @@ -63,20 +62,12 @@ namespace Debug { #endif } - try { - logMsg += std::vformat(fmt, std::make_format_args(args...)); - } catch (std::exception& e) { - std::string exceptionMsg = e.what(); - Debug::log(ERR, "caught exception in Debug::log: {}", exceptionMsg); - - const auto CALLSTACK = getBacktrace(); - - Debug::log(LOG, "stacktrace:"); - - for (size_t i = 0; i < CALLSTACK.size(); ++i) { - Debug::log(NONE, "\t #{} | {}", i, CALLSTACK[i].desc); - } - } + // no need for try {} catch {} because std::format_string ensures that vformat never throw std::format_error + // because + // 1. any faulty format specifier that sucks will cause a compilation error. + // 2. and `std::bad_alloc` is catastrophic, (Almost any operation in stdlib could throw this.) + // 3. this is actually what std::format in stdlib does + logMsg += std::vformat(fmt.get(), std::make_format_args(args...)); ofs << logMsg << "\n"; -- cgit v1.2.3