]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
crimson/osd: add formatter for std::optional<>
authorKefu Chai <tchaikov@gmail.com>
Tue, 22 Nov 2022 04:03:15 +0000 (12:03 +0800)
committerKefu Chai <tchaikov@gmail.com>
Tue, 22 Nov 2022 05:54:41 +0000 (13:54 +0800)
in `PG::do_update_log_missing()`, `std::optional<eversion_t>` is printed
using {fmt}, but {fmt} does not support formatting `std::optional<>`. so
we need to swing our own formatter. we can promote this formatter to
a more common place if it is proved to be useful otherwhere in the
source tree.

Signed-off-by: Kefu Chai <tchaikov@gmail.com>
src/crimson/osd/pg.cc

index a1808427ac3fb58ad168cc7d28962a66b057012c..248b510cfbc101197490b8f17a177167505e8dc6 100644 (file)
@@ -58,6 +58,17 @@ std::ostream& operator<<(std::ostream& out, const signedspan& d)
 }
 }
 
+template <typename T>
+struct fmt::formatter<std::optional<T>> : fmt::formatter<T> {
+  template <typename FormatContext>
+  auto format(const std::optional<T>& v, FormatContext& ctx) const {
+    if (v.has_value()) {
+      return fmt::formatter<T>::format(*v, ctx);
+    }
+    return fmt::format_to(ctx.out(), "<null>");
+  }
+};
+
 namespace crimson::osd {
 
 using crimson::common::local_conf;