diff options
author | Henrik Edin <henrik.edin@mongodb.com> | 2020-02-26 13:05:23 -0500 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-02-27 15:52:16 +0000 |
commit | d9a897e34f1e9092dc6117485bd67ab860c12f73 (patch) | |
tree | 207f74876a3e5cd18b140292775d3f4251c36778 /src/mongo/logv2 | |
parent | 835e125ff9cf933ff9804d8f7ca22cca1e3a04f2 (diff) | |
download | mongo-d9a897e34f1e9092dc6117485bd67ab860c12f73.tar.gz |
SERVER-46382 Add exception handler so log statements do not throw.
Exceptions during log is fatal in debug builds only
Diffstat (limited to 'src/mongo/logv2')
-rw-r--r-- | src/mongo/logv2/log_manager.cpp | 27 | ||||
-rw-r--r-- | src/mongo/logv2/log_test_v2.cpp | 6 |
2 files changed, 33 insertions, 0 deletions
diff --git a/src/mongo/logv2/log_manager.cpp b/src/mongo/logv2/log_manager.cpp index 8f8453417e2..b5fc193ace3 100644 --- a/src/mongo/logv2/log_manager.cpp +++ b/src/mongo/logv2/log_manager.cpp @@ -29,8 +29,13 @@ #include "mongo/platform/basic.h" +#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kDefault + #include "mongo/logv2/log_manager.h" +#include <boost/log/core.hpp> + +#include "mongo/logv2/log.h" #include "mongo/logv2/log_domain.h" #include "mongo/logv2/log_domain_global.h" @@ -46,6 +51,28 @@ struct LogManager::Impl { LogManager::LogManager() { _impl = std::make_unique<Impl>(); + + boost::log::core::get()->set_exception_handler([]() { + thread_local uint32_t depth = 0; + auto depthGuard = makeGuard([]() { --depth; }); + ++depth; + // Try and log that we failed to log + if (depth == 1) { + try { + throw; + } catch (...) { + LOGV2(4638200, + "Exception during log, message not written to stream", + "exception"_attr = exceptionToStatus()); + } + } + + // Logging exceptions are fatal in debug builds. Guard ourselves from additional logging + // during the assert that might also fail + if (kDebugBuild && depth <= 2) { + dassert(false, "Exception during log"); + } + }); } LogManager::~LogManager() {} diff --git a/src/mongo/logv2/log_test_v2.cpp b/src/mongo/logv2/log_test_v2.cpp index 12a592975d9..f0ac7e3218f 100644 --- a/src/mongo/logv2/log_test_v2.cpp +++ b/src/mongo/logv2/log_test_v2.cpp @@ -245,6 +245,12 @@ TEST_F(LogTestV2, Basic) { // Text formatter selects format string LOGV2(20084, "fmtstr {name}", "msgstr", "name"_attr = 1); ASSERT_EQUALS(lines.back(), "fmtstr 1"); + + // Test that logging exceptions does not propagate out to user code in release builds + if (!kDebugBuild) { + LOGV2(4638203, "mismatch {name}", "not_name"_attr = 1); + ASSERT(StringData(lines.back()).startsWith("Exception during log"_sd)); + } } TEST_F(LogTestV2, Types) { |