format: add a custom formatter for std::type_info
[lttng-tools.git] / src / common / format.hpp
1 /*
2 * Copyright (C) 2022 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7 #ifndef LTTNG_FORMAT_H
8 #define LTTNG_FORMAT_H
9
10 #include <common/macros.hpp>
11
12 #include <string>
13 #include <cxxabi.h>
14
15 DIAGNOSTIC_PUSH
16 DIAGNOSTIC_IGNORE_SUGGEST_ATTRIBUTE_FORMAT
17 DIAGNOSTIC_IGNORE_DUPLICATED_BRANCHES
18 #define FMT_HEADER_ONLY
19 #include <vendor/fmt/core.h>
20 DIAGNOSTIC_POP
21
22 template <>
23 struct fmt::formatter<std::type_info> : fmt::formatter<std::string> {
24 template <typename FormatCtx>
25 typename FormatCtx::iterator format(const std::type_info& type_info, FormatCtx& ctx)
26 {
27 int status;
28 auto demangled_name = abi::__cxa_demangle(type_info.name(), nullptr, 0, &status);
29 auto it = status == 0 ? fmt::formatter<std::string>::format(demangled_name, ctx) :
30 fmt::formatter<std::string>::format(type_info.name(), ctx);
31
32 free(demangled_name);
33 return it;
34 }
35 };
36
37 #endif /* LTTNG_FORMAT_H */
This page took 0.030265 seconds and 4 git commands to generate.