diff options
author | Gabriel Russell <gabriel.russell@mongodb.com> | 2019-12-30 20:28:42 +0000 |
---|---|---|
committer | evergreen <evergreen@mongodb.com> | 2019-12-30 20:28:42 +0000 |
commit | 5155b5258ed92394d5416327cc7fec937bb11362 (patch) | |
tree | 7808f962e69faab038d29bd5c864c504cd30d786 /src/mongo | |
parent | 65b13aa0a585a7b1630faca5c6f3a1895853a35d (diff) | |
download | mongo-5155b5258ed92394d5416327cc7fec937bb11362.tar.gz |
SERVER-44932 support logging the duration type in logv2
Diffstat (limited to 'src/mongo')
-rw-r--r-- | src/mongo/SConscript | 2 | ||||
-rw-r--r-- | src/mongo/logv2/log_test_v2.cpp | 13 | ||||
-rw-r--r-- | src/mongo/util/duration.cpp | 135 | ||||
-rw-r--r-- | src/mongo/util/duration.h | 79 |
4 files changed, 78 insertions, 151 deletions
diff --git a/src/mongo/SConscript b/src/mongo/SConscript index 361151df1bc..d5fb1e289c6 100644 --- a/src/mongo/SConscript +++ b/src/mongo/SConscript @@ -113,9 +113,9 @@ baseEnv.Library( 'logv2/bson_formatter.cpp', 'logv2/console.cpp', 'logv2/json_formatter.cpp', - 'logv2/log_detail.cpp', 'logv2/log_component.cpp', 'logv2/log_component_settings.cpp', + 'logv2/log_detail.cpp', 'logv2/log_domain.cpp', 'logv2/log_domain_global.cpp', 'logv2/log_domain_internal.cpp', diff --git a/src/mongo/logv2/log_test_v2.cpp b/src/mongo/logv2/log_test_v2.cpp index 0fb7db66af3..d0899129074 100644 --- a/src/mongo/logv2/log_test_v2.cpp +++ b/src/mongo/logv2/log_test_v2.cpp @@ -543,6 +543,19 @@ TEST_F(LogTestV2, Types) { mongo::fromjson(json.back()).getField(kAttributesFieldName).Obj().getField("name").String(), withoutBSON.toString()); ASSERT_EQUALS(lastBSONElement().String(), withoutBSON.toString()); + + // Duration + Milliseconds ms{12345}; + LOGV2("Duration {}", "name"_attr = ms); + ASSERT_EQUALS(text.back(), std::string("Duration ") + ms.toString()); + ASSERT_EQUALS(mongo::fromjson(json.back()) + .getField(kAttributesFieldName) + .Obj() + .getField("name") + .Obj() + .woCompare(ms.toBSON()), + 0); + ASSERT(lastBSONElement().Obj().woCompare(ms.toBSON()) == 0); } TEST_F(LogTestV2, TextFormat) { diff --git a/src/mongo/util/duration.cpp b/src/mongo/util/duration.cpp index b53e55b52ae..44239275d4d 100644 --- a/src/mongo/util/duration.cpp +++ b/src/mongo/util/duration.cpp @@ -31,130 +31,25 @@ #include "mongo/util/duration.h" -#include <iostream> - -#include "mongo/bson/util/builder.h" +#include "mongo/bson/bsonobjbuilder.h" namespace mongo { -namespace { -template <typename Stream> -Stream& streamPut(Stream& os, Nanoseconds ns) { - return os << ns.count() << "ns"; -} - -template <typename Stream> -Stream& streamPut(Stream& os, Microseconds us) { - return os << us.count() << "\xce\xbcs"; -} - -template <typename Stream> -Stream& streamPut(Stream& os, Milliseconds ms) { - return os << ms.count() << "ms"; -} - -template <typename Stream> -Stream& streamPut(Stream& os, Seconds s) { - return os << s.count() << 's'; -} - -template <typename Stream> -Stream& streamPut(Stream& os, Minutes min) { - return os << min.count() << "min"; -} -template <typename Stream> -Stream& streamPut(Stream& os, Hours hrs) { - return os << hrs.count() << "hr"; -} - -template <typename Stream> -Stream& streamPut(Stream& os, Days days) { - return os << days.count() << "d"; -} - -} // namespace - -std::ostream& operator<<(std::ostream& os, Nanoseconds ns) { - return streamPut(os, ns); -} - -std::ostream& operator<<(std::ostream& os, Microseconds us) { - return streamPut(os, us); -} - -std::ostream& operator<<(std::ostream& os, Milliseconds ms) { - return streamPut(os, ms); -} -std::ostream& operator<<(std::ostream& os, Seconds s) { - return streamPut(os, s); +template <typename Period> +BSONObj Duration<Period>::toBSON() const { + BSONObjBuilder builder; + builder.append("units", unit_short()); + builder.append("value", count()); + builder.done(); + return builder.obj(); } -std::ostream& operator<<(std::ostream& os, Minutes m) { - return streamPut(os, m); -} - -std::ostream& operator<<(std::ostream& os, Hours h) { - return streamPut(os, h); -} - -std::ostream& operator<<(std::ostream& os, Days d) { - return streamPut(os, d); -} - -template <typename Allocator> -StringBuilderImpl<Allocator>& operator<<(StringBuilderImpl<Allocator>& os, Nanoseconds ns) { - return streamPut(os, ns); -} - -template <typename Allocator> -StringBuilderImpl<Allocator>& operator<<(StringBuilderImpl<Allocator>& os, Microseconds us) { - return streamPut(os, us); -} - -template <typename Allocator> -StringBuilderImpl<Allocator>& operator<<(StringBuilderImpl<Allocator>& os, Milliseconds ms) { - return streamPut(os, ms); -} - -template <typename Allocator> -StringBuilderImpl<Allocator>& operator<<(StringBuilderImpl<Allocator>& os, Seconds s) { - return streamPut(os, s); -} - -template <typename Allocator> -StringBuilderImpl<Allocator>& operator<<(StringBuilderImpl<Allocator>& os, Minutes m) { - return streamPut(os, m); -} - -template <typename Allocator> -StringBuilderImpl<Allocator>& operator<<(StringBuilderImpl<Allocator>& os, Hours h) { - return streamPut(os, h); -} - -template <typename Allocator> -StringBuilderImpl<Allocator>& operator<<(StringBuilderImpl<Allocator>& os, Days d) { - return streamPut(os, d); -} +template BSONObj Nanoseconds::toBSON() const; +template BSONObj Microseconds::toBSON() const; +template BSONObj Milliseconds::toBSON() const; +template BSONObj Seconds::toBSON() const; +template BSONObj Minutes::toBSON() const; +template BSONObj Hours::toBSON() const; +template BSONObj Days::toBSON() const; -template StringBuilderImpl<StackAllocator>& operator<<(StringBuilderImpl<StackAllocator>&, - Nanoseconds); -template StringBuilderImpl<StackAllocator>& operator<<(StringBuilderImpl<StackAllocator>&, - Microseconds); -template StringBuilderImpl<StackAllocator>& operator<<(StringBuilderImpl<StackAllocator>&, - Milliseconds); -template StringBuilderImpl<StackAllocator>& operator<<(StringBuilderImpl<StackAllocator>&, Seconds); -template StringBuilderImpl<StackAllocator>& operator<<(StringBuilderImpl<StackAllocator>&, Minutes); -template StringBuilderImpl<StackAllocator>& operator<<(StringBuilderImpl<StackAllocator>&, Hours); -template StringBuilderImpl<SharedBufferAllocator>& operator<<( - StringBuilderImpl<SharedBufferAllocator>&, Nanoseconds); -template StringBuilderImpl<SharedBufferAllocator>& operator<<( - StringBuilderImpl<SharedBufferAllocator>&, Microseconds); -template StringBuilderImpl<SharedBufferAllocator>& operator<<( - StringBuilderImpl<SharedBufferAllocator>&, Milliseconds); -template StringBuilderImpl<SharedBufferAllocator>& operator<<( - StringBuilderImpl<SharedBufferAllocator>&, Seconds); -template StringBuilderImpl<SharedBufferAllocator>& operator<<( - StringBuilderImpl<SharedBufferAllocator>&, Minutes); -template StringBuilderImpl<SharedBufferAllocator>& operator<<( - StringBuilderImpl<SharedBufferAllocator>&, Hours); } // namespace mongo diff --git a/src/mongo/util/duration.h b/src/mongo/util/duration.h index 65cfc59ae1a..4e947ccd4ed 100644 --- a/src/mongo/util/duration.h +++ b/src/mongo/util/duration.h @@ -30,6 +30,7 @@ #pragma once #include <cstdint> +#include <fmt/format.h> #include <iosfwd> #include <limits> #include <ratio> @@ -43,6 +44,8 @@ namespace mongo { +class BSONObj; + template <typename Allocator> class StringBuilderImpl; @@ -65,36 +68,6 @@ using Days = Duration<std::ratio<86400>>; // 5min // -std::ostream& operator<<(std::ostream& os, Nanoseconds ns); -std::ostream& operator<<(std::ostream& os, Microseconds us); -std::ostream& operator<<(std::ostream& os, Milliseconds ms); -std::ostream& operator<<(std::ostream& os, Seconds s); -std::ostream& operator<<(std::ostream& os, Minutes m); -std::ostream& operator<<(std::ostream& os, Hours h); -std::ostream& operator<<(std::ostream& os, Days h); - -template <typename Allocator> -StringBuilderImpl<Allocator>& operator<<(StringBuilderImpl<Allocator>& os, Nanoseconds ns); - -template <typename Allocator> -StringBuilderImpl<Allocator>& operator<<(StringBuilderImpl<Allocator>& os, Microseconds us); - -template <typename Allocator> -StringBuilderImpl<Allocator>& operator<<(StringBuilderImpl<Allocator>& os, Milliseconds ms); - -template <typename Allocator> -StringBuilderImpl<Allocator>& operator<<(StringBuilderImpl<Allocator>& os, Seconds s); - -template <typename Allocator> -StringBuilderImpl<Allocator>& operator<<(StringBuilderImpl<Allocator>& os, Minutes m); - -template <typename Allocator> -StringBuilderImpl<Allocator>& operator<<(StringBuilderImpl<Allocator>& os, Hours h); - -template <typename Allocator> -StringBuilderImpl<Allocator>& operator<<(StringBuilderImpl<Allocator>& os, Days h); - - template <typename Duration1, typename Duration2> using HigherPrecisionDuration = typename std::conditional<!Duration1::template IsLowerPrecisionThan<Duration2>::value, @@ -155,6 +128,24 @@ inline long long durationCount(const stdx::chrono::duration<RepIn, PeriodIn>& d) template <typename Period> class Duration { public: + static constexpr StringData unit_short() { + if constexpr (std::is_same_v<Duration, Nanoseconds>) { + return "ns"_sd; + } else if constexpr (std::is_same_v<Duration, Microseconds>) { + return "\xce\xbcs"_sd; + } else if constexpr (std::is_same_v<Duration, Milliseconds>) { + return "ms"_sd; + } else if constexpr (std::is_same_v<Duration, Seconds>) { + return "s"_sd; + } else if constexpr (std::is_same_v<Duration, Minutes>) { + return "min"_sd; + } else if constexpr (std::is_same_v<Duration, Hours>) { + return "hr"_sd; + } else if constexpr (std::is_same_v<Duration, Days>) { + return "d"_sd; + } + return StringData{}; + } MONGO_STATIC_ASSERT_MSG(Period::num > 0, "Duration::period's numerator must be positive"); MONGO_STATIC_ASSERT_MSG(Period::den > 0, "Duration::period's denominator must be positive"); @@ -367,6 +358,13 @@ public: return *this; } + BSONObj toBSON() const; + + std::string toString() const { + return fmt::format("{} {}", count(), unit_short()); + } + + private: rep _count = {}; }; @@ -451,4 +449,25 @@ Duration<Period> operator/(Duration<Period> d, const Rep2& scale) { return d; } +template <typename Stream, typename Period> +Stream& streamPut(Stream& os, const Duration<Period>& dp) { + MONGO_STATIC_ASSERT_MSG(!Duration<Period>::unit_short().empty(), + "Only standard Durations can logged"); + return os << dp.count() << dp.unit_short(); +} + +template <typename Period> +std::ostream& operator<<(std::ostream& os, Duration<Period> dp) { + MONGO_STATIC_ASSERT_MSG(!Duration<Period>::unit_short().empty(), + "Only standard Durations can logged"); + return streamPut(os, dp); +} + +template <typename Allocator, typename Period> +StringBuilderImpl<Allocator>& operator<<(StringBuilderImpl<Allocator>& os, Duration<Period> dp) { + MONGO_STATIC_ASSERT_MSG(!Duration<Period>::unit_short().empty(), + "Only standard Durations can logged"); + return streamPut(os, dp); +} + } // namespace mongo |