From db4d75bb1cdb916103681f1b7138070afa638e02 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Wed, 21 Feb 2024 14:52:50 +1000 Subject: [PATCH] backtrace: use fmtlib formatters for output --- backtrace_stackwalk.cpp | 24 +++++++++++++++++------- backtrace_win32.cpp | 30 ++++++++++++++++++++++-------- 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/backtrace_stackwalk.cpp b/backtrace_stackwalk.cpp index 134e98cf..def243a3 100644 --- a/backtrace_stackwalk.cpp +++ b/backtrace_stackwalk.cpp @@ -17,7 +17,7 @@ #include "win32/windows.hpp" #include -#include +#include using cruft::backtrace; @@ -80,6 +80,17 @@ backtrace::backtrace () std::ostream& cruft::operator<< (std::ostream &os, backtrace const &obj) { + fmt::print (os, "{}", obj); + return os; +} + + +/////////////////////////////////////////////////////////////////////////////// +fmt::format_context::iterator +fmt::formatter::format ( + cruft::backtrace const &obj, + fmt::format_context &ctx +) { const auto process = GetCurrentProcess (); struct { @@ -91,21 +102,20 @@ cruft::operator<< (std::ostream &os, backtrace const &obj) sym.info.SizeOfStruct = sizeof (sym.info); sym.info.MaxNameLen = std::size (sym.name); - os << "[ "; + fmt::format_to (ctx.out (), "[ "); for (auto const frame: obj.frames ()) { - os << "{ addr: " << frame << ", name: "; + fmt::format_to (ctx.out (), "{{ addr: {}, name: ", frame); // Find the symbol name sym.info.Name[0] = '\0'; memset (sym.name, 0, sizeof (sym.name)); if (FALSE == SymFromAddr (process, reinterpret_cast (frame), nullptr, &sym.info)) { - os << "null }, "; + fmt::format_to (ctx.out (), "null }}, "); } else { - os << '\'' << sym.info.Name << "' }, "; + fmt::format_to (ctx.out (), "'{}' }}, ", sym.info.Name); } } - os << " ]"; - return os; + return fmt::format_to (ctx.out (), " ]"); } diff --git a/backtrace_win32.cpp b/backtrace_win32.cpp index 22635ec3..6025a8b7 100644 --- a/backtrace_win32.cpp +++ b/backtrace_win32.cpp @@ -45,9 +45,11 @@ backtrace::backtrace (void) /////////////////////////////////////////////////////////////////////////////// -std::ostream& -debug::operator <<(std::ostream &os, ::cruft::backtrace const &rhs) -{ +fmt::format_context::iterator +fmt::formatter::format ( + cruft::backtrace const &obj, + fmt::format_context &ctx +) { static auto process = GetCurrentProcess (); CHECK (ready); @@ -60,17 +62,29 @@ debug::operator <<(std::ostream &os, ::cruft::backtrace const &rhs) symbol.info.MaxNameLen = MAX_LENGTH; symbol.info.SizeOfStruct = sizeof (SYMBOL_INFO); - os << "[ "; + fmt::format_to (ctx.out (), "[ "); for (void *frame: rhs.frames ()) { symbol.name[0] = '\0'; SymFromAddr (process, (DWORD64)frame, 0, &symbol.info); symbol.name[MAX_LENGTH] = '\0'; - os << "{ addr: " << frame - << ", name: '" << symbol.name '\'' - << " }, "; + fmt::format_to ( + ctx.out (), + "{{ addr: {}, name: '{}' }}, ", + frame, + symbol.name + ); } - os << " ]"; + return fmt::format_to (ctx.out (), " ]"); +} + + + +/////////////////////////////////////////////////////////////////////////////// +std::ostream& +debug::operator <<(std::ostream &os, ::cruft::backtrace const &rhs) +{ + fmt::print (os, "{}", rhs); return os; }