Commit | Line | Data |
---|---|---|
05aa7e19 JG |
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 | ||
5bb4ff54 | 12 | #include <cxxabi.h> |
28f23191 | 13 | #include <string> |
f9a41357 | 14 | #include <utility> |
5bb4ff54 | 15 | |
05aa7e19 JG |
16 | DIAGNOSTIC_PUSH |
17 | DIAGNOSTIC_IGNORE_SUGGEST_ATTRIBUTE_FORMAT | |
18 | DIAGNOSTIC_IGNORE_DUPLICATED_BRANCHES | |
19 | #define FMT_HEADER_ONLY | |
20 | #include <vendor/fmt/core.h> | |
21 | DIAGNOSTIC_POP | |
22 | ||
11bcbf89 JG |
23 | /* |
24 | * Due to a bug in g++ < 7.1, this specialization must be enclosed in the fmt namespace, | |
25 | * see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56480. | |
26 | */ | |
e2c2bec2 | 27 | namespace fmt { |
5bb4ff54 | 28 | template <> |
e2c2bec2 | 29 | struct formatter<std::type_info> : formatter<std::string> { |
31375c42 JG |
30 | template <typename FormatContextType> |
31 | typename FormatContextType::iterator format(const std::type_info& type_info, | |
32 | FormatContextType& ctx) | |
5bb4ff54 JG |
33 | { |
34 | int status; | |
cd9adb8b JG |
35 | auto demangled_name = |
36 | abi::__cxa_demangle(type_info.name(), nullptr, nullptr, &status); | |
e2c2bec2 JR |
37 | auto it = status == 0 ? formatter<std::string>::format(demangled_name, ctx) : |
38 | formatter<std::string>::format(type_info.name(), ctx); | |
5bb4ff54 JG |
39 | |
40 | free(demangled_name); | |
41 | return it; | |
42 | } | |
43 | }; | |
e2c2bec2 | 44 | } /* namespace fmt */ |
5bb4ff54 | 45 | |
f9a41357 JG |
46 | namespace lttng { |
47 | template <typename... FormattingArguments> | |
48 | std::string format(FormattingArguments&&...args) | |
49 | { | |
50 | try { | |
51 | return fmt::format(std::forward<FormattingArguments>(args)...); | |
52 | } catch (const fmt::format_error& ex) { | |
53 | return std::string("Failed to format string: ") += ex.what(); | |
54 | } | |
55 | } | |
56 | } /* namespace lttng */ | |
57 | ||
05aa7e19 | 58 | #endif /* LTTNG_FORMAT_H */ |