Skip to content

Commit

Permalink
Made Logging::addLoggingKeyValuePairs more efficient
Browse files Browse the repository at this point in the history
The logging code no longer has to create and tear down a stringstream
every time.
  • Loading branch information
snej committed Jan 7, 2025
1 parent 867525a commit 3a65c31
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 21 deletions.
4 changes: 1 addition & 3 deletions LiteCore/Logging/Logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<mutex> lock(sLogMutex);
Expand Down
11 changes: 5 additions & 6 deletions LiteCore/Logging/Logging.hh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include <cstdarg>
#include <cstdint>
#include <cinttypes> //for stdint.h fmt specifiers
#include <iosfwd>
#include <string>

/*
Expand Down Expand Up @@ -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); \
Expand All @@ -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:
Expand Down
3 changes: 1 addition & 2 deletions LiteCore/tests/LogObserverTest.hh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include "Logging.hh"
#include "LogObserver.hh"
#include <queue>
#include <sstream>

using namespace std;
using namespace litecore;
Expand All @@ -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(); }

Expand Down
10 changes: 6 additions & 4 deletions Replicator/Replicator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion Replicator/Replicator.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 6 additions & 4 deletions Replicator/Worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<slice> Options::kWhiteListOfKeysToLog{
Expand Down
2 changes: 1 addition & 1 deletion Replicator/Worker.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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> _options; // The replicator options
Retained<Worker> _parent; // Worker that owns me
Expand Down

0 comments on commit 3a65c31

Please sign in to comment.