summaryrefslogtreecommitdiff
path: root/src/mongo/unittest
diff options
context:
space:
mode:
authorBilly Donahue <billy.donahue@mongodb.com>2020-04-13 07:30:51 -0400
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2020-04-13 18:25:44 +0000
commit5c72483523561c0331769abc3250cf623817883f (patch)
tree55b7d4d5db64a03f301f1c3cf5474bfe85445112 /src/mongo/unittest
parent79809da04aa7d49a6a276cb5ebfd191a6f664942 (diff)
downloadmongo-5c72483523561c0331769abc3250cf623817883f.tar.gz
SERVER-47483 restore loggedSeverity after LogWithSamplingTest
Diffstat (limited to 'src/mongo/unittest')
-rw-r--r--src/mongo/unittest/log_test.h38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/mongo/unittest/log_test.h b/src/mongo/unittest/log_test.h
index 9f48830d958..66a2b22ca64 100644
--- a/src/mongo/unittest/log_test.h
+++ b/src/mongo/unittest/log_test.h
@@ -29,6 +29,8 @@
#pragma once
+#include <boost/optional/optional.hpp>
+
#include "mongo/logv2/log_component.h"
#include "mongo/logv2/log_component_settings.h"
#include "mongo/logv2/log_manager.h"
@@ -63,4 +65,40 @@ inline bool hasMinimumLogSeverity(logv2::LogComponent component) {
return logv2::LogManager::global().getGlobalSettings().hasMinimumLogSeverity(component);
}
+/**
+ * Configure a LogComponent`s MinimumLoggedSeverity, saving the old state and restoring it
+ * when this guard object dies. There can be no severity mapping for a LogComponent, so
+ * the logged severity 'state' is read and written as a boost::optional.
+ */
+class MinimumLoggedSeverityGuard {
+public:
+ MinimumLoggedSeverityGuard(logv2::LogComponent component,
+ boost::optional<logv2::LogSeverity> severity)
+ : _component{component}, _savedSeverity{_get()} {
+ _put(severity);
+ }
+
+ ~MinimumLoggedSeverityGuard() {
+ _put(_savedSeverity);
+ }
+
+private:
+ boost::optional<logv2::LogSeverity> _get() {
+ if (hasMinimumLogSeverity(_component))
+ return getMinimumLogSeverity(_component);
+ return boost::none;
+ }
+
+ void _put(boost::optional<logv2::LogSeverity> severity) {
+ if (severity) {
+ setMinimumLoggedSeverity(_component, *severity);
+ } else {
+ clearMinimumLoggedSeverity(_component);
+ }
+ }
+
+ logv2::LogComponent _component;
+ boost::optional<logv2::LogSeverity> _savedSeverity;
+};
+
} // namespace mongo