X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=src%2Fcommon%2Fformat.hpp;h=2ba6cef125ab735ae8977c88dde7298c0c889f93;hb=HEAD;hp=b586fd489f8f934f8ace25808c7e19ce2acd4fe9;hpb=cd9adb8b829564212158943a0d279bb35322ab30;p=lttng-tools.git diff --git a/src/common/format.hpp b/src/common/format.hpp index b586fd489..2ba6cef12 100644 --- a/src/common/format.hpp +++ b/src/common/format.hpp @@ -9,8 +9,9 @@ #include -#include #include +#include +#include DIAGNOSTIC_PUSH DIAGNOSTIC_IGNORE_SUGGEST_ATTRIBUTE_FORMAT @@ -19,6 +20,8 @@ DIAGNOSTIC_IGNORE_DUPLICATED_BRANCHES #include DIAGNOSTIC_POP +#include + /* * Due to a bug in g++ < 7.1, this specialization must be enclosed in the fmt namespace, * see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56480. @@ -26,19 +29,35 @@ DIAGNOSTIC_POP namespace fmt { template <> struct formatter : formatter { - template - typename FormatCtx::iterator format(const std::type_info& type_info, FormatCtx& ctx) + template + typename FormatContextType::iterator format(const std::type_info& type_info, + FormatContextType& ctx) { int status; - auto demangled_name = - abi::__cxa_demangle(type_info.name(), nullptr, nullptr, &status); - auto it = status == 0 ? formatter::format(demangled_name, ctx) : - formatter::format(type_info.name(), ctx); + /* + * The documentation of __cxa_demangle mentions the returned string is allocated + * using malloc (not new), hence the use of lttng::memory::free. + */ + const auto demangled_name = lttng::make_unique_wrapper( + abi::__cxa_demangle(type_info.name(), nullptr, nullptr, &status)); - free(demangled_name); + auto it = status == 0 ? formatter::format(demangled_name.get(), ctx) : + formatter::format(type_info.name(), ctx); return it; } }; } /* namespace fmt */ +namespace lttng { +template +std::string format(FormattingArguments&&...args) +{ + try { + return fmt::format(std::forward(args)...); + } catch (const fmt::format_error& ex) { + return std::string("Failed to format string: ") += ex.what(); + } +} +} /* namespace lttng */ + #endif /* LTTNG_FORMAT_H */