Add formatter for std::stacktrace and std::stacktrace_entry (C++23) by siribeesu · Pull Request #4983 · fmtlib/fmt · GitHub
Skip to content

Add formatter for std::stacktrace and std::stacktrace_entry (C++23) - #4983

Open
siribeesu wants to merge 1 commit into
fmtlib:mainfrom
siribeesu:update
Open

siribeesu wants to merge 1 commit into
fmtlib:mainfrom
siribeesu:update

Conversation

@siribeesu

Copy link
Copy Markdown

This PR adds formatting support for std::stacktrace and std::stacktrace_entry from the C++23 <stacktrace> header.

Motivation

There is currently no way to format std::stacktrace or std::stacktrace_entry with {fmt}. These types are widely useful for diagnostic and logging output, and are a natural fit alongside the existing std::exception and std::source_location formatters already in fmt/std.h.

Example usage this PR enables:

auto trace = std::stacktrace::current();
fmt::print("{}\n", trace);
// Output:
// #0 main at main.cc:10
// #1 __libc_start_main at libc-start.c:308

fmt::print("{:>60}\n", trace[0]);
// Right-aligned: "                    main at main.cc:10"

Changes

include/fmt/std.h

  • Added formatter<std::stacktrace_entry> — formats as function at file:line when debug symbols are available, or as a hex address otherwise
  • Added formatter<std::stacktrace> — formats one indexed entry per line as #N description
  • Both formatters inherit from formatter<string_view> to support fill, align, and width specs
  • Include of <stacktrace> is guarded by FMT_CPLUSPLUS >= 202302L && FMT_HAS_INCLUDE(<stacktrace>) && defined(__cpp_lib_stacktrace)
  • Both formatters marked FMT_EXPORT for correct C++20 module support

test/std-test.cc

  • TEST(std_test, stacktrace_entry) — verifies description, source location formatting, and width/alignment specs
  • TEST(std_test, stacktrace) — verifies indexed multi-line output, newline count, and alignment
  • TEST(std_test, stacktrace_empty) — verifies an empty stacktrace formats to "" without crashing

doc/api.md

  • Documented std::stacktrace and std::stacktrace_entry under the standard library formatters section with a description of the output format

Compatibility

  • No impact on builds without C++23 stacktrace support — all new code is guarded by #ifdef __cpp_lib_stacktrace
  • Works with: MSVC 2022 (17.x+), GCC 12+, Clang 14+
  • No changes to existing formatters or public API

Adds support for formatting std::stacktrace and std::stacktrace_entry
from <stacktrace> (C++23, __cpp_lib_stacktrace).
- stacktrace_entry formats as 'function at file:line' when symbols
  are available, or as a hex address otherwise
- stacktrace formats one indexed entry per line: '#N description'
- Both formatters inherit string_view specs (width, fill, align)
- Guarded by __cpp_lib_stacktrace for portability
@siribeesu
siribeesu requested a review from vitaut as a code owner October 11, 2026 14:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant