diff --git a/LiteCore/Logging/Logging.cc b/LiteCore/Logging/Logging.cc index 4950ac0f0..25fc2b8b6 100644 --- a/LiteCore/Logging/Logging.cc +++ b/LiteCore/Logging/Logging.cc @@ -117,9 +117,7 @@ namespace litecore { .fileOnly = !doCallback}; if ( logger ) { entry.objRef = logger->getObjectRef(); - std::stringstream prefixOut; - logger->addLoggingKeyValuePairs(prefixOut); - prefix = prefixOut.str(); + prefix = logger->loggingKeyValuePairs(); } unique_lock lock(sLogMutex); diff --git a/LiteCore/Logging/Logging.hh b/LiteCore/Logging/Logging.hh index 974ec9c3e..22fc83049 100644 --- a/LiteCore/Logging/Logging.hh +++ b/LiteCore/Logging/Logging.hh @@ -16,7 +16,6 @@ #include #include #include //for stdint.h fmt specifiers -#include #include /* @@ -153,6 +152,11 @@ namespace litecore { virtual std::string loggingIdentifier() const; virtual std::string loggingClassName() const; + /** Override this to return aditional metadata about the object, in the form of + space-separated "key=value" pairs. + These will be logged with every message, even in the binary log file. */ + virtual std::string loggingKeyValuePairs() const { return {}; } + #define LOGBODY_(LEVEL) \ va_list args; \ va_start(args, format); \ @@ -177,11 +181,6 @@ namespace litecore { void _logv(LogLevel level, const char* format, va_list) const __printflike(3, 0); - /// Add key=value pairs to the output. They are space separated. If output is not empty - /// upon entry, add a space to start new key=value pairs. - /// Warning: the string must not include printf format specifier, '%'. - virtual void addLoggingKeyValuePairs(std::stringstream& output) const {} - LogDomain& _domain; private: diff --git a/LiteCore/tests/LogObserverTest.hh b/LiteCore/tests/LogObserverTest.hh index 1af4ee227..dedd48aac 100644 --- a/LiteCore/tests/LogObserverTest.hh +++ b/LiteCore/tests/LogObserverTest.hh @@ -14,7 +14,6 @@ #include "Logging.hh" #include "LogObserver.hh" #include -#include using namespace std; using namespace litecore; @@ -32,7 +31,7 @@ class LogObject : public Logging { std::string loggingClassName() const override { return _identifier; } - void addLoggingKeyValuePairs(std::stringstream& output) const override { output << _kv; } + std::string loggingKeyValuePairs() const override { return _kv; } LogObjectRef getRef() const { return getObjectRef(); } diff --git a/Replicator/Replicator.cc b/Replicator/Replicator.cc index e985223ad..8f1878971 100644 --- a/Replicator/Replicator.cc +++ b/Replicator/Replicator.cc @@ -1353,12 +1353,14 @@ namespace litecore::repl { } } - void Replicator::addLoggingKeyValuePairs(std::stringstream& output) const { - Worker::addLoggingKeyValuePairs(output); + string Replicator::loggingKeyValuePairs() const { + string kv = Worker::loggingKeyValuePairs(); if ( _correlationID ) { - if ( output.tellp() > 0 ) output << " "; - output << "CorrID=" << _correlationID.asString(); + if ( !kv.empty() ) kv += " "; + kv += "CorrID="; + kv += _correlationID.asString(); } + return kv; } } // namespace litecore::repl diff --git a/Replicator/Replicator.hh b/Replicator/Replicator.hh index 06ecc2e11..b74c11003 100644 --- a/Replicator/Replicator.hh +++ b/Replicator/Replicator.hh @@ -117,7 +117,7 @@ namespace litecore::repl { protected: std::string loggingClassName() const override { return _options->isActive() ? "Repl" : "PsvRepl"; } - void addLoggingKeyValuePairs(std::stringstream& output) const override; + std::string loggingKeyValuePairs() const override; // BLIP ConnectionDelegate API: void onHTTPResponse(int status, const websocket::Headers& headers) override; diff --git a/Replicator/Worker.cc b/Replicator/Worker.cc index ece17bf0a..aad81d299 100644 --- a/Replicator/Worker.cc +++ b/Replicator/Worker.cc @@ -328,12 +328,14 @@ namespace litecore::repl { return nonConstThis->replicator()->collection(collectionIndex()); } - void Worker::addLoggingKeyValuePairs(std::stringstream& output) const { - actor::Actor::addLoggingKeyValuePairs(output); + string Worker::loggingKeyValuePairs() const { + string kv = Actor::loggingKeyValuePairs(); if ( auto collIdx = collectionIndex(); collIdx != kNotCollectionIndex ) { - if ( output.tellp() > 0 ) output << " "; - output << "Coll=" << collIdx; + if ( !kv.empty() ) kv += " "; + kv += "Coll="; + kv += to_string(collIdx); } + return kv; } const std::unordered_set Options::kWhiteListOfKeysToLog{ diff --git a/Replicator/Worker.hh b/Replicator/Worker.hh index e03812f59..4e692582c 100644 --- a/Replicator/Worker.hh +++ b/Replicator/Worker.hh @@ -241,7 +241,7 @@ namespace litecore::repl { const C4Collection* getCollection() const; - void addLoggingKeyValuePairs(std::stringstream& output) const override; + std::string loggingKeyValuePairs() const override; RetainedConst _options; // The replicator options Retained _parent; // Worker that owns me