How to make `boost::stacktrace` formattable with `fmt::format`

Viewed 183

I'm struggling to get boost's stack trace library to interoperate with fmt, the trouble seems to be that I can't quite specialize a boost::stacktrace::basic_stacktrace. Anybody know what the underlying issue is?

#include <fmt/core.h>
#include <boost/stacktrace.hpp>

using BoostTraceStackFrame = std::remove_reference_t<decltype(boost::stacktrace::stacktrace().as_vector().front())>;
template <> struct fmt::formatter<BoostTraceStackFrame> : formatter<string_view>
{
    template <typename FORMAT_CONTEXT> auto format(const BoostTraceStackFrame &rhs, FORMAT_CONTEXT &ctx)
    {
        return fmt::format(ctx.out(), "{}:{}\n", rhs.source_file(), rhs.source_line());
    }
};

using BoostTraceType = std::remove_reference_t<decltype(boost::stacktrace::stacktrace())>;
template <> struct fmt::formatter<BoostTraceType> : formatter<string_view>
{
    //possibly BoostTraceType = boost::stacktrace::basic_stacktrace ?
    template <typename FORMAT_CONTEXT> auto format(const BoostTraceType &rhs, FORMAT_CONTEXT &ctx)
    {
        return fmt::format(ctx.out(), "{}", fmt::join(rhs.as_vector(), "\n"));
    }
};

int main()
{
 auto trace = boost::stacktrace::stacktrace();
 fmt::print("Foo trace {}",trace);
}

On godbolt

1 Answers

Just three small things:

  1. Missing/wrong headers. You need to include fmt/ranges.h in order to support formatting ranges.

  2. Wrong overload. The function for formatting into an iterator is fmt::format_to, not fmt::format.

  3. These:

using BoostTraceStackFrame = std::remove_reference_t<decltype(boost::stacktrace::stacktrace().as_vector().front())>;
using BoostTraceType = std::remove_reference_t<decltype(boost::stacktrace::stacktrace())>;

are fairly long-winded ways of writing:

using BoostTraceStackFrame = boost::stacktrace::frame;
using BoostTraceType = boost::stacktrace::stacktrace;

And in particular, the problem was that the way you wrote the first one it was boost::stacktrace::frame const when it needed to not be const.

Related