]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
commom,osd: support for default fmtlib formatters
authorRonen Friedman <rfriedma@redhat.com>
Mon, 7 Aug 2023 15:21:20 +0000 (10:21 -0500)
committerRonen Friedman <rfriedma@redhat.com>
Tue, 15 Aug 2023 12:32:46 +0000 (07:32 -0500)
Declaring formatters for all classes that supply
either fmt_print() or alt_fmt_print() member functions
(both must adhere to a specific signature).

Signed-off-by: Ronen Friedman <rfriedma@redhat.com>
src/common/fmt_common.h [new file with mode: 0644]
src/osd/osd_types.h

diff --git a/src/common/fmt_common.h b/src/common/fmt_common.h
new file mode 100644 (file)
index 0000000..d68d645
--- /dev/null
@@ -0,0 +1,64 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+#pragma once
+
+/**
+ * \file default fmtlib formatters for specifically-tagged types
+ */
+#include <fmt/format.h>
+
+/**
+ * an implementation note:
+ * not including fmt/ranges.h here because it grabs every structure that
+ * has a begin()/end() method pair. This is a problem because we have
+ * such classes in Crimson.
+ */
+/**
+ * Tagging classes that provide support for default fmtlib formatting,
+ * by having either
+ * std::string fmt_print() const
+ * *or*
+ * std::string alt_fmt_print(bool short_format) const
+ * as public member functions.
+ */
+template<class T>
+concept has_fmt_print = requires(T t) {
+  { t.fmt_print() } -> std::same_as<std::string>;
+};
+template<class T>
+concept has_alt_fmt_print = requires(T t) {
+  { t.alt_fmt_print(bool{}) } -> std::same_as<std::string>;
+};
+
+namespace fmt {
+
+template <has_fmt_print T>
+struct formatter<T> {
+  constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
+  template <typename FormatContext>
+  auto format(const T& k, FormatContext& ctx) const {
+    return fmt::format_to(ctx.out(), "{}", k.fmt_print());
+  }
+};
+
+template <has_alt_fmt_print T>
+struct formatter<T> {
+  template <typename ParseContext>
+  constexpr auto parse(ParseContext& ctx) {
+    auto it = ctx.begin();
+    if (it != ctx.end() && *it == 's') {
+      verbose = false;
+      ++it;
+    }
+    return it;
+  }
+  template <typename FormatContext>
+  auto format(const T& k, FormatContext& ctx) const {
+    if (verbose) {
+      return fmt::format_to(ctx.out(), "{}", k.alt_fmt_print(true));
+    }
+    return fmt::format_to(ctx.out(), "{}", k.alt_fmt_print(false));
+  }
+  bool verbose{true};
+};
+}  // namespace fmt
index 2bd3bd85c8a6e485654930002d63515309b0a5b9..64c337a5b24345293d4782fdac31babdedb16a94 100644 (file)
@@ -31,6 +31,7 @@
 
 #include "include/rados/rados_types.hpp"
 #include "include/mempool.h"
+#include "common/fmt_common.h"
 
 #include "msg/msg_types.h"
 #include "include/compat.h"