diff options
| author | memchr <118117622+memchr@users.noreply.github.com> | 2023-09-20 07:26:20 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-09-20 08:26:20 +0100 |
| commit | 6594b50e57935dd66930ccd35dba7a1b4131399d (patch) | |
| tree | 17d4fa7aabf0e447c7171346c5b851c999093149 /src/debug/Log.hpp | |
| parent | d8d0cd75c2db16a14086e1d5119ed937a60c701e (diff) | |
logging/format: use std::format_string to catch formatting string errors at compile time (#3377)
* fix(log): use constexpr format string
* deprecate getFormat
Diffstat (limited to 'src/debug/Log.hpp')
| -rw-r--r-- | src/debug/Log.hpp | 25 |
1 files changed, 8 insertions, 17 deletions
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 <typename... Args> - void log(LogLevel level, const std::string& fmt, Args&&... args) { + void log(LogLevel level, std::format_string<Args...> 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<Args...> 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"; |
