osd: add fmtlib formatting for some OSD types

Signed-off-by: Ronen Friedman <rfriedma@redhat.com>
This commit is contained in:
Ronen Friedman 2021-06-15 12:20:31 +03:00
parent ca906d0d7a
commit 3ab5a54e1c
4 changed files with 212 additions and 0 deletions

52
src/common/hobject_fmt.h Normal file
View File

@ -0,0 +1,52 @@
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#pragma once
/**
* \file fmtlib formatters for some hobject.h classes
*/
#include <fmt/format.h>
#include "common/hobject.h"
#include "include/types_fmt.h"
#include "msg/msg_fmt.h"
// \todo reimplement
static inline void append_out_escaped(const std::string& in, std::string* out)
{
for (auto i = in.cbegin(); i != in.cend(); ++i) {
if (*i == '%' || *i == ':' || *i == '/' || *i < 32 || *i >= 127) {
char buf[4];
snprintf(buf, sizeof(buf), "%%%02x", (int)(unsigned char)*i);
out->append(buf);
} else {
out->push_back(*i);
}
}
}
template <> struct fmt::formatter<hobject_t> {
constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
template <typename FormatContext> auto format(const hobject_t& ho, FormatContext& ctx)
{
if (ho == hobject_t{}) {
return fmt::format_to(ctx.out(), "MIN");
}
if (ho.is_max()) {
return fmt::format_to(ctx.out(), "MAX");
}
std::string v;
append_out_escaped(ho.nspace, &v);
v.push_back(':');
append_out_escaped(ho.get_key(), &v);
v.push_back(':');
append_out_escaped(ho.oid.name, &v);
return fmt::format_to(ctx.out(), "{}:{:08x}:{}:{}", static_cast<uint64_t>(ho.pool),
ho.get_bitwise_key_u32(), v, ho.snap);
}
};

28
src/include/types_fmt.h Normal file
View File

@ -0,0 +1,28 @@
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#pragma once
/**
* \file fmtlib formatters for some types.h classes
*/
#include <fmt/format.h>
#include <string_view>
#include "include/types.h"
template <class A, class B, class Comp, class Alloc>
struct fmt::formatter<std::map<A, B, Comp, Alloc>> {
constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
template <typename FormatContext>
auto format(const std::map<A, B, Comp, Alloc>& m, FormatContext& ctx)
{
std::string_view sep = "{";
for (const auto& [k, v] : m) {
fmt::format_to(ctx.out(), "{}{}={}", sep, k, v);
sep = ",";
}
return fmt::format_to(ctx.out(), "}}");
}
};

26
src/msg/msg_fmt.h Normal file
View File

@ -0,0 +1,26 @@
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#pragma once
/**
* \file fmtlib formatters for some msg_types.h classes
*/
#include <fmt/format.h>
#include "include/types_fmt.h"
#include "msg/msg_types.h"
template <>
struct fmt::formatter<entity_name_t> {
constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
template <typename FormatContext>
auto format(const entity_name_t& addr, FormatContext& ctx)
{
if (addr.is_new() || addr.num() < 0) {
return fmt::format_to(ctx.out(), "{}.?", addr.type_str());
}
return fmt::format_to(ctx.out(), "{}.{}", addr.type_str(), addr.num());
}
};

106
src/osd/osd_types_fmt.h Normal file
View File

@ -0,0 +1,106 @@
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#pragma once
/**
* \file fmtlib formatters for some types.h classes
*/
#include "common/hobject_fmt.h"
#include "osd/osd_types.h"
template <>
struct fmt::formatter<osd_reqid_t> {
constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
template <typename FormatContext>
auto format(const osd_reqid_t& req_id, FormatContext& ctx)
{
return fmt::format_to(ctx.out(), "{}.{}:{}", req_id.name, req_id.inc,
req_id.tid);
}
};
template <>
struct fmt::formatter<pg_shard_t> {
constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
template <typename FormatContext>
auto format(const pg_shard_t& shrd, FormatContext& ctx)
{
if (shrd.is_undefined()) {
return fmt::format_to(ctx.out(), "?");
}
if (shrd.shard == shard_id_t::NO_SHARD) {
return fmt::format_to(ctx.out(), "{}", shrd.get_osd());
}
return fmt::format_to(ctx.out(), "{}({})", shrd.get_osd(), shrd.shard);
}
};
template <>
struct fmt::formatter<eversion_t> {
constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
template <typename FormatContext>
auto format(const eversion_t& ev, FormatContext& ctx)
{
return fmt::format_to(ctx.out(), "{}'{}", ev.epoch, ev.version);
}
};
template <>
struct fmt::formatter<chunk_info_t> {
constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
template <typename FormatContext>
auto format(const chunk_info_t& ci, FormatContext& ctx)
{
return fmt::format_to(ctx.out(), "(len: {} oid: {} offset: {} flags: {})",
ci.length, ci.oid, ci.offset,
ci.get_flag_string(ci.flags));
}
};
template <>
struct fmt::formatter<object_manifest_t> {
constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
template <typename FormatContext>
auto format(const object_manifest_t& om, FormatContext& ctx)
{
fmt::format_to(ctx.out(), "manifest({}", om.get_type_name());
if (om.is_redirect()) {
fmt::format_to(ctx.out(), " {}", om.redirect_target);
} else if (om.is_chunked()) {
fmt::format_to(ctx.out(), " {}", om.chunk_map);
}
return fmt::format_to(ctx.out(), ")");
}
};
template <>
struct fmt::formatter<object_info_t> {
constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
template <typename FormatContext>
auto format(const object_info_t& oi, FormatContext& ctx)
{
fmt::format_to(ctx.out(), "{}({} {} {} s {} uv {}", oi.soid, oi.version,
oi.last_reqid, (oi.flags ? oi.get_flag_string() : ""), oi.size,
oi.user_version);
if (oi.is_data_digest()) {
fmt::format_to(ctx.out(), " dd {:x}", oi.data_digest);
}
if (oi.is_omap_digest()) {
fmt::format_to(ctx.out(), " od {:x}", oi.omap_digest);
}
fmt::format_to(ctx.out(), " alloc_hint [{} {} {}]", oi.expected_object_size,
oi.expected_write_size, oi.alloc_hint_flags);
if (oi.has_manifest()) {
fmt::format_to(ctx.out(), " {}", oi.manifest);
}
return fmt::format_to(ctx.out(), ")");
}
};