]>
Commit | Line | Data |
---|---|---|
20effc67 TL |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | #pragma once | |
4 | ||
5 | /** | |
6 | * \file fmtlib formatters for some hobject.h classes | |
7 | */ | |
8 | #include <fmt/format.h> | |
9 | ||
10 | #include "common/hobject.h" | |
11 | #include "include/types_fmt.h" | |
12 | #include "msg/msg_fmt.h" | |
13 | ||
14 | // \todo reimplement | |
15 | static inline void append_out_escaped(const std::string& in, std::string* out) | |
16 | { | |
17 | for (auto i = in.cbegin(); i != in.cend(); ++i) { | |
18 | if (*i == '%' || *i == ':' || *i == '/' || *i < 32 || *i >= 127) { | |
19 | char buf[4]; | |
20 | snprintf(buf, sizeof(buf), "%%%02x", (int)(unsigned char)*i); | |
21 | out->append(buf); | |
22 | } else { | |
23 | out->push_back(*i); | |
24 | } | |
25 | } | |
26 | } | |
27 | ||
28 | template <> struct fmt::formatter<hobject_t> { | |
29 | ||
30 | constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); } | |
31 | ||
32 | template <typename FormatContext> auto format(const hobject_t& ho, FormatContext& ctx) | |
33 | { | |
34 | if (ho == hobject_t{}) { | |
35 | return fmt::format_to(ctx.out(), "MIN"); | |
36 | } | |
37 | ||
38 | if (ho.is_max()) { | |
39 | return fmt::format_to(ctx.out(), "MAX"); | |
40 | } | |
41 | ||
42 | std::string v; | |
43 | append_out_escaped(ho.nspace, &v); | |
44 | v.push_back(':'); | |
45 | append_out_escaped(ho.get_key(), &v); | |
46 | v.push_back(':'); | |
47 | append_out_escaped(ho.oid.name, &v); | |
48 | ||
49 | return fmt::format_to(ctx.out(), "{}:{:08x}:{}:{}", static_cast<uint64_t>(ho.pool), | |
50 | ho.get_bitwise_key_u32(), v, ho.snap); | |
51 | } | |
52 | }; |