diff options
Diffstat (limited to 'src/mongo/unittest/unittest.cpp')
| -rw-r--r-- | src/mongo/unittest/unittest.cpp | 57 |
1 files changed, 22 insertions, 35 deletions
diff --git a/src/mongo/unittest/unittest.cpp b/src/mongo/unittest/unittest.cpp index ef9233554c2..94adc7f1046 100644 --- a/src/mongo/unittest/unittest.cpp +++ b/src/mongo/unittest/unittest.cpp @@ -193,39 +193,27 @@ public: void startCapturingLogMessages(); void stopCapturingLogMessages(); void stopCapturingLogMessagesIfNeeded(); - const synchronized_value<std::vector<std::string>>& getCapturedTextFormatLogMessages() const { - return _capturedLogMessages; - } + const std::vector<std::string>& getCapturedTextFormatLogMessages() const; std::vector<BSONObj> getCapturedBSONFormatLogMessages() const; int64_t countTextFormatLogLinesContaining(const std::string& needle); int64_t countBSONFormatLogLinesIsSubset(const BSONObj& needle); void printCapturedTextFormatLogLines() const; private: - class Listener : public logv2::LogLineListener { - public: - explicit Listener(synchronized_value<std::vector<std::string>>* sv) : _sv(sv) {} - void accept(const std::string& line) override { - (***_sv).push_back(line); - } - - private: - synchronized_value<std::vector<std::string>>* _sv; - }; - bool _isCapturingLogMessages{false}; // Captures Plain Text Log - synchronized_value<std::vector<std::string>> _capturedLogMessages; + std::vector<std::string> _capturedLogMessages; // Captured BSON - synchronized_value<std::vector<std::string>> _capturedBSONLogMessages; + std::vector<std::string> _capturedBSONLogMessages; // Capture Sink for Plain Text - boost::shared_ptr<boost::log::sinks::unlocked_sink<logv2::LogCaptureBackend>> _captureSink; + boost::shared_ptr<boost::log::sinks::synchronous_sink<logv2::LogCaptureBackend>> _captureSink; // Capture Sink for BSON - boost::shared_ptr<boost::log::sinks::unlocked_sink<logv2::LogCaptureBackend>> _captureBSONSink; + boost::shared_ptr<boost::log::sinks::synchronous_sink<logv2::LogCaptureBackend>> + _captureBSONSink; }; static CaptureLogs* getCaptureLogs() { @@ -260,18 +248,16 @@ namespace { void CaptureLogs::startCapturingLogMessages() { invariant(!_isCapturingLogMessages); - (**_capturedLogMessages).clear(); - (**_capturedBSONLogMessages).clear(); + _capturedLogMessages.clear(); + _capturedBSONLogMessages.clear(); if (!_captureSink) { - _captureSink = logv2::LogCaptureBackend::create( - std::make_unique<Listener>(&_capturedLogMessages), true); + _captureSink = logv2::LogCaptureBackend::create(_capturedLogMessages, true); _captureSink->set_filter( logv2::AllLogsFilter(logv2::LogManager::global().getGlobalDomain())); _captureSink->set_formatter(logv2::PlainFormatter()); - _captureBSONSink = logv2::LogCaptureBackend::create( - std::make_unique<Listener>(&_capturedBSONLogMessages), false); + _captureBSONSink = logv2::LogCaptureBackend::create(_capturedBSONLogMessages, false); _captureBSONSink->set_filter( logv2::AllLogsFilter(logv2::LogManager::global().getGlobalDomain())); @@ -297,11 +283,14 @@ void CaptureLogs::stopCapturingLogMessagesIfNeeded() { } } +const std::vector<std::string>& CaptureLogs::getCapturedTextFormatLogMessages() const { + return _capturedLogMessages; +} + std::vector<BSONObj> CaptureLogs::getCapturedBSONFormatLogMessages() const { std::vector<BSONObj> objs; - auto logLinesLockGuard = *_capturedBSONLogMessages; - std::transform(logLinesLockGuard->cbegin(), - logLinesLockGuard->cend(), + std::transform(_capturedBSONLogMessages.cbegin(), + _capturedBSONLogMessages.cend(), std::back_inserter(objs), [](const std::string& str) { return BSONObj(str.c_str()); }); return objs; @@ -309,8 +298,7 @@ std::vector<BSONObj> CaptureLogs::getCapturedBSONFormatLogMessages() const { void CaptureLogs::printCapturedTextFormatLogLines() const { LOGV2(23054, "****************************** Captured Lines (start) *****************************"); - auto logLinesLockGuard = *getCapturedTextFormatLogMessages(); - for (const auto& line : *logLinesLockGuard) { + for (const auto& line : getCapturedTextFormatLogMessages()) { LOGV2(23055, "{line}", "line"_attr = line); } LOGV2(23056, @@ -318,10 +306,9 @@ void CaptureLogs::printCapturedTextFormatLogLines() const { } int64_t CaptureLogs::countTextFormatLogLinesContaining(const std::string& needle) { - auto msgs = *getCapturedTextFormatLogMessages(); - return std::count_if(msgs->begin(), msgs->end(), [&](const std::string& s) { - return stringContains(s, needle); - }); + const auto& msgs = getCapturedTextFormatLogMessages(); + return std::count_if( + msgs.begin(), msgs.end(), [&](const std::string& s) { return stringContains(s, needle); }); } bool isSubset(BSONObj haystack, BSONObj needle) { @@ -373,8 +360,8 @@ void Test::startCapturingLogMessages() { void Test::stopCapturingLogMessages() { getCaptureLogs()->stopCapturingLogMessages(); } -std::vector<std::string> Test::getCapturedTextFormatLogMessages() const { - return getCaptureLogs()->getCapturedTextFormatLogMessages().get(); +const std::vector<std::string>& Test::getCapturedTextFormatLogMessages() const { + return getCaptureLogs()->getCapturedTextFormatLogMessages(); } std::vector<BSONObj> Test::getCapturedBSONFormatLogMessages() const { return getCaptureLogs()->getCapturedBSONFormatLogMessages(); |
