From 0da54c8f3ad4aa6d2f3e9eac3ff1a77f07d19fb7 Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Tue, 19 Sep 2023 05:03:54 -0400 Subject: handle errors in spdlog gracefully (#410) * handle errors in spdlog gracefully - try to report and avoid termination * changelog --- src/zenserver/diag/logging.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'src') diff --git a/src/zenserver/diag/logging.cpp b/src/zenserver/diag/logging.cpp index 13270b41d..0f15253cc 100644 --- a/src/zenserver/diag/logging.cpp +++ b/src/zenserver/diag/logging.cpp @@ -446,6 +446,35 @@ InitializeLogging(const ZenServerOptions& GlobalOptions) FileSink->set_pattern("[%C-%m-%d.%e %T] [%n] [%l] %v"); } + spdlog::set_error_handler([](const std::string& msg) { + if (msg == std::bad_alloc().what()) + { + // Don't report out of memory in spdlog as we usually log in response to errors which will cause another OOM crashing the + // program + return; + } + // Bypass zen logging wrapping to reduce potential other error sources + if (auto ErrLogger = zen::logging::ErrorLog(); ErrLogger != nullptr) + { + try + { + ErrLogger->log(spdlog::level::err, msg); + } + catch (const std::exception&) + { + // Just ignore any errors when in error handler + } + } + try + { + Log().error(msg); + } + catch (const std::exception&) + { + // Just ignore any errors when in error handler + } + }); + const std::string StartLogTime = zen::DateTime::Now().ToIso8601(); const zen::Oid ServerSessionId = zen::GetSessionId(); -- cgit v1.2.3