summaryrefslogtreecommitdiff
path: root/src/mongo/logger
diff options
context:
space:
mode:
authorAndy Schwerin <schwerin@mongodb.com>2015-04-16 15:38:28 -0400
committerAndy Schwerin <schwerin@mongodb.com>2015-04-20 10:49:09 -0400
commite64daecc3f37180093e90cf79a2524cc5135862f (patch)
tree555be1223772dfd73ce1848b71000f81a9de4247 /src/mongo/logger
parent71e1d4f8d5f515d6fc272faa20f81255e5766dc9 (diff)
downloadmongo-e64daecc3f37180093e90cf79a2524cc5135862f.tar.gz
SERVER-18133 Make LogstreamBuilder movable, other C++11 improvements.
Diffstat (limited to 'src/mongo/logger')
-rw-r--r--src/mongo/logger/logstream_builder.cpp93
-rw-r--r--src/mongo/logger/logstream_builder.h27
2 files changed, 58 insertions, 62 deletions
diff --git a/src/mongo/logger/logstream_builder.cpp b/src/mongo/logger/logstream_builder.cpp
index cc3135ec59b..1ed0e701c45 100644
--- a/src/mongo/logger/logstream_builder.cpp
+++ b/src/mongo/logger/logstream_builder.cpp
@@ -29,11 +29,13 @@
#include "mongo/logger/logstream_builder.h"
+#include <memory>
+
#include "mongo/base/init.h"
-#include "mongo/base/owned_pointer_vector.h"
#include "mongo/base/status.h"
#include "mongo/logger/message_event_utf8_encoder.h"
#include "mongo/logger/tee.h"
+#include "mongo/stdx/memory.h"
#include "mongo/util/assert_util.h" // TODO: remove apple dep for this in threadlocal.h
#include "mongo/util/concurrency/threadlocal.h"
#include "mongo/util/time_support.h"
@@ -42,11 +44,6 @@ namespace mongo {
namespace {
- /// Type of per-thread cache for storing pre-constructed ostringstreams. While its type is
- /// vector, it should only ever contain 0 or 1 item. It is a vector rather than just a
- /// thread_specific_ptr<> because of the high cost of thread_specific_ptr<>::reset().
- typedef OwnedPointerVector<std::ostringstream> OwnedOstreamVector;
-
/// This flag indicates whether the system providing a per-thread cache of ostringstreams
/// for use by LogstreamBuilder instances is initialized and ready for use. Until this
/// flag is true, LogstreamBuilder instances must not use the cache.
@@ -59,8 +56,8 @@ namespace {
} // namespace
- TSP_DECLARE(OwnedOstreamVector, threadOstreamCache);
- TSP_DEFINE(OwnedOstreamVector, threadOstreamCache);
+ TSP_DECLARE(std::unique_ptr<std::ostringstream>, threadOstreamCache);
+ TSP_DEFINE(std::unique_ptr<std::ostringstream>, threadOstreamCache);
namespace {
// During unittests, where we don't use quickExit(), static finalization may destroy the
@@ -76,52 +73,53 @@ namespace {
namespace logger {
LogstreamBuilder::LogstreamBuilder(MessageLogDomain* domain,
- const std::string& contextName,
+ std::string contextName,
LogSeverity severity)
- : _domain(domain),
- _contextName(contextName),
- _severity(severity),
- _component(LogComponent::kDefault),
- _os(NULL),
- _tee(NULL) {
- }
+ : LogstreamBuilder(domain,
+ std::move(contextName),
+ std::move(severity),
+ LogComponent::kDefault) {}
LogstreamBuilder::LogstreamBuilder(MessageLogDomain* domain,
- const std::string& contextName,
+ std::string contextName,
LogSeverity severity,
LogComponent component)
: _domain(domain),
- _contextName(contextName),
- _severity(severity),
- _component(component),
- _os(NULL),
- _tee(NULL) {
+ _contextName(std::move(contextName)),
+ _severity(std::move(severity)),
+ _component(std::move(component)),
+ _tee(nullptr) {
}
LogstreamBuilder::LogstreamBuilder(logger::MessageLogDomain* domain,
const std::string& contextName,
LabeledLevel labeledLevel)
- : _domain(domain),
- _contextName(contextName),
- _severity(labeledLevel),
- _component(LogComponent::kDefault),
- _os(NULL),
- _tee(NULL) {
+ : LogstreamBuilder(domain,
+ std::move(contextName),
+ static_cast<LogSeverity>(labeledLevel)) {
setBaseMessage(labeledLevel.getLabel());
}
- LogstreamBuilder::LogstreamBuilder(const LogstreamBuilder& other)
- : _domain(other._domain),
- _contextName(other._contextName),
- _severity(other._severity),
- _component(other._component),
- _baseMessage(other._baseMessage),
- _os(NULL),
- _tee(NULL) {
-
- if (other._os || other._tee)
- abort();
+ LogstreamBuilder::LogstreamBuilder(LogstreamBuilder&& other)
+ : _domain(std::move(other._domain)),
+ _contextName(std::move(other._contextName)),
+ _severity(std::move(other._severity)),
+ _component(std::move(other._component)),
+ _baseMessage(std::move(other._baseMessage)),
+ _os(std::move(other._os)),
+ _tee(std::move(other._tee)) {
+ }
+
+ LogstreamBuilder& LogstreamBuilder::operator=(LogstreamBuilder&& other) {
+ _domain = std::move(other._domain);
+ _contextName = std::move(other._contextName);
+ _severity = std::move(other._severity);
+ _component = std::move(other._component);
+ _baseMessage = std::move(other._baseMessage);
+ _os = std::move(other._os);
+ _tee = std::move(other._tee);
+ return *this;
}
@@ -139,11 +137,8 @@ namespace logger {
_tee->write(_os->str());
}
_os->str("");
- if (isThreadOstreamCacheInitialized && threadOstreamCache.getMake()->vector().empty()) {
- threadOstreamCache.get()->mutableVector().push_back(_os);
- }
- else {
- delete _os;
+ if (isThreadOstreamCacheInitialized && !threadOstreamCache.getMake()->get()) {
+ *threadOstreamCache.get() = std::move(_os);
}
}
}
@@ -156,15 +151,11 @@ namespace logger {
void LogstreamBuilder::makeStream() {
if (!_os) {
- if (isThreadOstreamCacheInitialized &&
- !threadOstreamCache.getMake()->vector().empty()) {
-
- std::vector<std::ostringstream*>& oses = threadOstreamCache.get()->mutableVector();
- _os = oses.back();
- oses.pop_back();
+ if (isThreadOstreamCacheInitialized && threadOstreamCache.getMake()->get()) {
+ _os = std::move(*threadOstreamCache.get());
}
else {
- _os = new std::ostringstream;
+ _os = stdx::make_unique<std::ostringstream>();
}
}
}
diff --git a/src/mongo/logger/logstream_builder.h b/src/mongo/logger/logstream_builder.h
index 1b76178c377..7c923d6ece3 100644
--- a/src/mongo/logger/logstream_builder.h
+++ b/src/mongo/logger/logstream_builder.h
@@ -27,7 +27,7 @@
#pragma once
-#include <boost/scoped_ptr.hpp>
+#include <memory>
#include <sstream>
#include <string>
@@ -58,7 +58,7 @@ namespace logger {
* "severity" is the logging severity of the message.
*/
LogstreamBuilder(MessageLogDomain* domain,
- const std::string& contextName,
+ std::string contextName,
LogSeverity severity);
/**
@@ -69,7 +69,7 @@ namespace logger {
* "component" is the primary log component of the message.
*/
LogstreamBuilder(MessageLogDomain* domain,
- const std::string& contextName,
+ std::string contextName,
LogSeverity severity,
LogComponent component);
@@ -81,13 +81,19 @@ namespace logger {
LabeledLevel labeledLevel);
/**
- * Copies a LogstreamBuilder. LogstreamBuilder instances are copyable only until the first
- * call to stream() or operator<<.
+ * Move constructor.
+ *
+ * TODO: Replace with = default implementation when minimum MSVC version is bumped to
+ * MSVC2015.
+ */
+ LogstreamBuilder(LogstreamBuilder&& other);
+
+ /**
+ * Move assignment operator.
*
- * TODO(schwerin): After C++11 transition, replace with a move-constructor, and make
- * LogstreamBuilder movable.
+ * TODO: Replace with =default implementation when minimum MSVC version is bumped to VS2015.
*/
- LogstreamBuilder(const LogstreamBuilder& other);
+ LogstreamBuilder& operator=(LogstreamBuilder&& other);
/**
* Destroys a LogstreamBuilder(). If anything was written to it via stream() or operator<<,
@@ -104,7 +110,7 @@ namespace logger {
return *this;
}
- std::ostream& stream() { makeStream(); return *_os; }
+ std::ostream& stream() { if (!_os) makeStream(); return *_os; }
LogstreamBuilder& operator<<(const char *x) { stream() << x; return *this; }
LogstreamBuilder& operator<<(const std::string& x) { stream() << x; return *this; }
@@ -146,7 +152,6 @@ namespace logger {
void operator<<(Tee* tee);
private:
- LogstreamBuilder& operator=(const LogstreamBuilder& other);
void makeStream();
@@ -155,7 +160,7 @@ namespace logger {
LogSeverity _severity;
LogComponent _component;
std::string _baseMessage;
- std::ostringstream* _os;
+ std::unique_ptr<std::ostringstream> _os;
Tee* _tee;
};