summaryrefslogtreecommitdiff
path: root/src/mongo/util/duration.h
diff options
context:
space:
mode:
authorGabriel Russell <gabriel.russell@mongodb.com>2019-12-30 20:28:42 +0000
committerevergreen <evergreen@mongodb.com>2019-12-30 20:28:42 +0000
commit5155b5258ed92394d5416327cc7fec937bb11362 (patch)
tree7808f962e69faab038d29bd5c864c504cd30d786 /src/mongo/util/duration.h
parent65b13aa0a585a7b1630faca5c6f3a1895853a35d (diff)
downloadmongo-5155b5258ed92394d5416327cc7fec937bb11362.tar.gz
SERVER-44932 support logging the duration type in logv2
Diffstat (limited to 'src/mongo/util/duration.h')
-rw-r--r--src/mongo/util/duration.h79
1 files changed, 49 insertions, 30 deletions
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