2 * Copyright (C) 2022 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
10 #include <common/macros.hpp>
17 DIAGNOSTIC_IGNORE_SUGGEST_ATTRIBUTE_FORMAT
18 DIAGNOSTIC_IGNORE_DUPLICATED_BRANCHES
19 #define FMT_HEADER_ONLY
20 #include <vendor/fmt/core.h>
23 #include <common/make-unique-wrapper.hpp>
26 * Due to a bug in g++ < 7.1, this specialization must be enclosed in the fmt namespace,
27 * see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56480.
31 struct formatter<std::type_info> : formatter<std::string> {
32 template <typename FormatContextType>
33 typename FormatContextType::iterator format(const std::type_info& type_info,
34 FormatContextType& ctx)
38 * The documentation of __cxa_demangle mentions the returned string is allocated
39 * using malloc (not new), hence the use of lttng::free.
41 const auto demangled_name = lttng::make_unique_wrapper<char, lttng::free>(
42 abi::__cxa_demangle(type_info.name(), nullptr, nullptr, &status));
44 auto it = status == 0 ? formatter<std::string>::format(demangled_name.get(), ctx) :
45 formatter<std::string>::format(type_info.name(), ctx);
52 template <typename... FormattingArguments>
53 std::string format(FormattingArguments&&...args)
56 return fmt::format(std::forward<FormattingArguments>(args)...);
57 } catch (const fmt::format_error& ex) {
58 return std::string("Failed to format string: ") += ex.what();
61 } /* namespace lttng */
63 #endif /* LTTNG_FORMAT_H */