From: Ronen Friedman Date: Mon, 7 Aug 2023 15:21:20 +0000 (-0500) Subject: commom,osd: support for default fmtlib formatters X-Git-Tag: v19.0.0~637^2~7 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=27e073c935d42ada3ffd1d551ccebacfb5ab067f;p=ceph.git commom,osd: support for default fmtlib formatters 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 --- diff --git a/src/common/fmt_common.h b/src/common/fmt_common.h new file mode 100644 index 00000000000..d68d6457dcb --- /dev/null +++ b/src/common/fmt_common.h @@ -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 + +/** + * 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 +concept has_fmt_print = requires(T t) { + { t.fmt_print() } -> std::same_as; +}; +template +concept has_alt_fmt_print = requires(T t) { + { t.alt_fmt_print(bool{}) } -> std::same_as; +}; + +namespace fmt { + +template +struct formatter { + constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); } + template + auto format(const T& k, FormatContext& ctx) const { + return fmt::format_to(ctx.out(), "{}", k.fmt_print()); + } +}; + +template +struct formatter { + template + constexpr auto parse(ParseContext& ctx) { + auto it = ctx.begin(); + if (it != ctx.end() && *it == 's') { + verbose = false; + ++it; + } + return it; + } + template + 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 diff --git a/src/osd/osd_types.h b/src/osd/osd_types.h index 2bd3bd85c8a..64c337a5b24 100644 --- a/src/osd/osd_types.h +++ b/src/osd/osd_types.h @@ -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"