]>
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> | |
1e59de90 | 9 | #include <fmt/ranges.h> |
20effc67 TL |
10 | |
11 | #include "common/hobject.h" | |
1e59de90 | 12 | #include "include/object_fmt.h" |
20effc67 TL |
13 | #include "msg/msg_fmt.h" |
14 | ||
15 | // \todo reimplement | |
16 | static inline void append_out_escaped(const std::string& in, std::string* out) | |
17 | { | |
18 | for (auto i = in.cbegin(); i != in.cend(); ++i) { | |
19 | if (*i == '%' || *i == ':' || *i == '/' || *i < 32 || *i >= 127) { | |
20 | char buf[4]; | |
21 | snprintf(buf, sizeof(buf), "%%%02x", (int)(unsigned char)*i); | |
22 | out->append(buf); | |
23 | } else { | |
24 | out->push_back(*i); | |
25 | } | |
26 | } | |
27 | } | |
28 | ||
29 | template <> struct fmt::formatter<hobject_t> { | |
30 | ||
31 | constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); } | |
32 | ||
33 | template <typename FormatContext> auto format(const hobject_t& ho, FormatContext& ctx) | |
34 | { | |
35 | if (ho == hobject_t{}) { | |
36 | return fmt::format_to(ctx.out(), "MIN"); | |
37 | } | |
38 | ||
39 | if (ho.is_max()) { | |
40 | return fmt::format_to(ctx.out(), "MAX"); | |
41 | } | |
42 | ||
43 | std::string v; | |
44 | append_out_escaped(ho.nspace, &v); | |
45 | v.push_back(':'); | |
46 | append_out_escaped(ho.get_key(), &v); | |
47 | v.push_back(':'); | |
48 | append_out_escaped(ho.oid.name, &v); | |
49 | ||
50 | return fmt::format_to(ctx.out(), "{}:{:08x}:{}:{}", static_cast<uint64_t>(ho.pool), | |
51 | ho.get_bitwise_key_u32(), v, ho.snap); | |
52 | } | |
53 | }; |