diff options
author | Mark Benvenuto <mark.benvenuto@mongodb.com> | 2020-09-11 11:33:33 -0400 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-09-11 15:53:33 +0000 |
commit | 642adc7547023ec17f9c912e631790fa508c9818 (patch) | |
tree | e1f09c57d744e5c89f3a0c0f76d97cd63f475699 /src/mongo/logv2 | |
parent | a5f72d4b37ed92fa72d3a31e0af4266c9ef8d014 (diff) | |
download | mongo-642adc7547023ec17f9c912e631790fa508c9818.tar.gz |
SERVER-50036 logd() fails with invalid format string
Diffstat (limited to 'src/mongo/logv2')
-rw-r--r-- | src/mongo/logv2/logv2_test.cpp | 12 | ||||
-rw-r--r-- | src/mongo/logv2/plain_formatter.cpp | 9 |
2 files changed, 21 insertions, 0 deletions
diff --git a/src/mongo/logv2/logv2_test.cpp b/src/mongo/logv2/logv2_test.cpp index f863c7db555..e6139d964be 100644 --- a/src/mongo/logv2/logv2_test.cpp +++ b/src/mongo/logv2/logv2_test.cpp @@ -1769,6 +1769,18 @@ TEST_F(UnstructuredLoggingTest, Args) { }); } +TEST_F(UnstructuredLoggingTest, ArgsLikeFormatSpecifier) { + // Ensure the plain formatter does not process the formatted string + startCapturingLogMessages(); + + std::string format_str = "format {} str {} fields"; + logd(format_str, 1, "{ x : 1}"); // NOLINT + validate([&format_str](const BSONObj& obj) { + ASSERT_EQUALS(obj.getField(kMessageFieldName).String(), + fmt::format(format_str, 1, "{ x : 1}")); + }); +} + TEST_F(UnstructuredLoggingTest, ManyArgs) { std::string format_str = "{}{}{}{}{}{}{}{}{}{}{}"; logd(format_str, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11); // NOLINT diff --git a/src/mongo/logv2/plain_formatter.cpp b/src/mongo/logv2/plain_formatter.cpp index 0fad5d46b84..de83f01667e 100644 --- a/src/mongo/logv2/plain_formatter.cpp +++ b/src/mongo/logv2/plain_formatter.cpp @@ -152,6 +152,15 @@ void PlainFormatter::operator()(boost::log::record_view const& rec, StringData message = extract<StringData>(attributes::message(), rec).get(); const auto& attrs = extract<TypeErasedAttributeStorage>(attributes::attributes(), rec).get(); + // Log messages logged via logd are already formatted and have the id == 0 + if (attrs.empty()) { + if (extract<int32_t>(attributes::id(), rec).get() == 0) { + buffer.append(message.rawData(), message.rawData() + message.size()); + + return; + } + } + TextValueExtractor extractor; extractor.args.reserve(attrs.size()); attrs.apply(extractor); |