Skip to content

Commit 0ff56a6

Browse files
committed
Replace prettify_symbols and full_symbol with a symbol_modes enum
1 parent 6b7edeb commit 0ff56a6

File tree

6 files changed

+56
-54
lines changed

6 files changed

+56
-54
lines changed

README.md

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ into `std::string`.
307307
308308
`cpptrace::prune_symbol` is a helper for custom formatters that prunes demangled symbols by removing return types,
309309
template arguments, and function parameters. It also does some minimal normalization. For example, it prunes
310-
`ns::S<int, float>::~S()` to `ns::S::~S`.
310+
`ns::S<int, float>::~S()` to `ns::S::~S`. If cpptrace is unable to parse the symbol it will return the original symbol.
311311
312312
`cpptrace::get_snippet` gets a text snippet, if possible, from for the given source file for +/- `context_size` lines
313313
around `line`.
@@ -370,7 +370,8 @@ namespace cpptrace {
370370
formatter& snippets(bool);
371371
formatter& snippet_context(int);
372372
formatter& columns(bool);
373-
formatter& prettify_symbols(bool);
373+
enum class symbol_mode { full, pretty, pruned };
374+
formatter& symbols(symbol_mode);
374375
formatter& filtered_frame_placeholders(bool);
375376
formatter& filter(std::function<bool(const stacktrace_frame&)>);
376377
formatter& transform(std::function<stacktrace_frame(stacktrace_frame)>);
@@ -399,28 +400,32 @@ namespace cpptrace {
399400
```
400401
401402
Options:
402-
| Setting | Description | Default |
403-
| ----------------------------- | -------------------------------------------------------------- | ------------------------------------------------------------------------ |
404-
| `header` | Header line printed before the trace | `Stack trace (most recent call first):` |
405-
| `colors` | Default color mode for the trace | `automatic`, which attempts to detect if the target stream is a terminal |
406-
| `addresses` | Raw addresses, object addresses, or no addresses | `raw` |
407-
| `paths` | Full paths or just filenames | `full` |
408-
| `snippets` | Whether to include source code snippets | `false` |
409-
| `snippet_context` | How many lines of source context to show in a snippet | `2` |
410-
| `columns` | Whether to include column numbers if present | `true` |
411-
| `prettify_symbols` | Whether to attempt to clean up long symbol names | `false` |
412-
| `filtered_frame_placeholders` | Whether to still print filtered frames as just `#n (filtered)` | `true` |
413-
| `filter` | A predicate to filter frames with | None |
414-
| `transform` | A transformer which takes a stacktrace frame and modifies it | None |
403+
| Setting | Description | Default |
404+
| ----------------------------- | ------------------------------------------------------------------ | ------------------------------------------------------------------------ |
405+
| `header` | Header line printed before the trace | `Stack trace (most recent call first):` |
406+
| `colors` | Default color mode for the trace | `automatic`, which attempts to detect if the target stream is a terminal |
407+
| `addresses` | Raw addresses, object addresses, or no addresses | `raw` |
408+
| `paths` | Full paths or just filenames | `full` |
409+
| `snippets` | Whether to include source code snippets | `false` |
410+
| `snippet_context` | How many lines of source context to show in a snippet | `2` |
411+
| `columns` | Whether to include column numbers if present | `true` |
412+
| `symbols` | Full demangled symbols, pruned symbol names, or prettified symbols | `full` |
413+
| `filtered_frame_placeholders` | Whether to still print filtered frames as just `#n (filtered)` | `true` |
414+
| `filter` | A predicate to filter frames with | None |
415+
| `transform` | A transformer which takes a stacktrace frame and modifies it | None |
415416
416417
The `automatic` color mode attempts to detect if a stream that may be attached to a terminal. As such, it will not use
417418
colors for the `formatter::format` method and it may not be able to detect if some ostreams correspond to terminals or
418419
not. For this reason, `formatter::format` and `formatter::print` methods have overloads taking a color parameter. This
419420
color parameter will override configured color mode.
420421
421-
The `prettify_symbols` option applies a number of simple rewrite rules to symbols in an attempt to clean them up, e.g.
422-
it rewrites `foo(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >)`
423-
as `foo(std::vector<std::string>)`.
422+
The `symbols` option provides a few settings for pretty-printing symbol names. By default the full name is printed.
423+
- `symbol_mode::pretty` applies a number of transformations to clean up long symbol names. For example, it turns
424+
`std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >` into `std::string`. This is
425+
equivalent to `cpptrace::prettify_symbol`.
426+
- `symbol_mode::prune` prunes demangled symbols by removing return types, template arguments, and function parameters.
427+
It also does some minimal normalization. For example, it prunes `ns::S<int, float>::~S()` to `ns::S::~S`. If cpptrace
428+
is unable to parse the symbol it will uses the full symbol. This is equivalent to `cpptrace::prune_symbol`.
424429
425430
Recommended practice with formatters: It's generally preferable to create formatters objects that are long-lived rather
426431
than to create them on the fly every time a trace needs to be formatted.

include/cpptrace/basic.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,6 @@ CPPTRACE_BEGIN_NAMESPACE
164164

165165
object_frame get_object_info() const;
166166

167-
std::string name() const;
168-
169167
std::string to_string() const;
170168
std::string to_string(bool color) const;
171169
friend CPPTRACE_EXPORT std::ostream& operator<<(std::ostream& stream, const stacktrace_frame& frame);

include/cpptrace/formatting.hpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,15 @@ CPPTRACE_BEGIN_NAMESPACE
4444
formatter& snippets(bool);
4545
formatter& snippet_context(int);
4646
formatter& columns(bool);
47-
formatter& prettify_symbols(bool);
48-
formatter& full_symbol(bool);
47+
enum class symbol_mode {
48+
// full demangled symbol
49+
full,
50+
// symbols are prettified to clean up some especially long template argument lists
51+
pretty,
52+
// template arguments and function parameters are pruned
53+
pruned
54+
};
55+
formatter& symbols(symbol_mode);
4956
formatter& filtered_frame_placeholders(bool);
5057
formatter& filter(std::function<bool(const stacktrace_frame&)>);
5158
formatter& transform(std::function<stacktrace_frame(stacktrace_frame)>);

src/cpptrace.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,6 @@ CPPTRACE_BEGIN_NAMESPACE
121121
return detail::get_frame_object_info(raw_address);
122122
}
123123

124-
std::string stacktrace_frame::name() const {
125-
return prune_symbol(symbol);
126-
}
127-
128124
std::string stacktrace_frame::to_string() const {
129125
return to_string(false);
130126
}

src/formatting.cpp

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ CPPTRACE_BEGIN_NAMESPACE
6666
bool snippets = false;
6767
int context_lines = 2;
6868
bool columns = true;
69-
bool prettify_symbols = false;
70-
bool full_symbol = true;
69+
symbol_mode symbols = symbol_mode::full;
7170
bool show_filtered_frames = true;
7271
std::function<bool(const stacktrace_frame&)> filter;
7372
std::function<stacktrace_frame(stacktrace_frame)> transform;
@@ -95,11 +94,8 @@ CPPTRACE_BEGIN_NAMESPACE
9594
void columns(bool columns) {
9695
options.columns = columns;
9796
}
98-
void prettify_symbols(bool prettify) {
99-
options.prettify_symbols = prettify;
100-
}
101-
void full_symbol(bool full) {
102-
options.full_symbol = full;
97+
void symbols(symbol_mode mode) {
98+
options.symbols = mode;
10399
}
104100
void filtered_frame_placeholders(bool show) {
105101
options.show_filtered_frames = show;
@@ -285,16 +281,20 @@ CPPTRACE_BEGIN_NAMESPACE
285281
if(!frame.symbol.empty()) {
286282
detail::optional<std::string> maybe_stored_string;
287283
detail::string_view symbol;
288-
if(options.full_symbol) {
289-
if(options.prettify_symbols) {
284+
switch(options.symbols) {
285+
case symbol_mode::full:
286+
symbol = frame.symbol;
287+
break;
288+
case symbol_mode::pruned:
289+
maybe_stored_string = prune_symbol(frame.symbol);
290+
symbol = maybe_stored_string.unwrap();
291+
break;
292+
case symbol_mode::pretty:
290293
maybe_stored_string = prettify_symbol(frame.symbol);
291294
symbol = maybe_stored_string.unwrap();
292-
} else {
293-
symbol = frame.symbol;
294-
}
295-
} else {
296-
maybe_stored_string = frame.name();
297-
symbol = maybe_stored_string.unwrap();
295+
break;
296+
default:
297+
PANIC("Unhandled symbol mode");
298298
}
299299
microfmt::print(stream, "in {}{}{}", yellow, symbol, reset);
300300
}
@@ -367,12 +367,8 @@ CPPTRACE_BEGIN_NAMESPACE
367367
pimpl->columns(columns);
368368
return *this;
369369
}
370-
formatter& formatter::prettify_symbols(bool prettify) {
371-
pimpl->prettify_symbols(prettify);
372-
return *this;
373-
}
374-
formatter& formatter::full_symbol(bool full) {
375-
pimpl->full_symbol(full);
370+
formatter& formatter::symbols(symbol_mode mode) {
371+
pimpl->symbols(mode);
376372
return *this;
377373
}
378374
formatter& formatter::filtered_frame_placeholders(bool show) {

test/unit/lib/formatting.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ TEST(FormatterTest, CopySemantics) {
375375

376376
TEST(FormatterTest, PrettySymbols) {
377377
auto normal_formatter = cpptrace::formatter{}
378-
.prettify_symbols(false);
378+
.symbols(cpptrace::formatter::symbol_mode::full);
379379
cpptrace::stacktrace trace;
380380
trace.frames.push_back({0x1, 0x1001, {20}, {30}, "foo.cpp", "foo(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)", false});
381381
auto res = split(normal_formatter.format(trace), "\n");
@@ -387,7 +387,7 @@ TEST(FormatterTest, PrettySymbols) {
387387
)
388388
);
389389
auto pretty_formatter = cpptrace::formatter{}
390-
.prettify_symbols(true);
390+
.symbols(cpptrace::formatter::symbol_mode::pretty);
391391
res = split(pretty_formatter.format(trace), "\n");
392392
EXPECT_THAT(
393393
res,
@@ -398,9 +398,9 @@ TEST(FormatterTest, PrettySymbols) {
398398
);
399399
}
400400

401-
TEST(FormatterTest, FullSymbols) {
401+
TEST(FormatterTest, PrunedSymbols) {
402402
auto normal_formatter = cpptrace::formatter{}
403-
.full_symbol(true);
403+
.symbols(cpptrace::formatter::symbol_mode::full);
404404
cpptrace::stacktrace trace;
405405
trace.frames.push_back({0x1, 0x1001, {20}, {30}, "foo.cpp", "ns::S<float, int>::foo(int, int, int)", false});
406406
auto res = split(normal_formatter.format(trace), "\n");
@@ -411,9 +411,9 @@ TEST(FormatterTest, FullSymbols) {
411411
"#0 0x0000000000000001 in ns::S<float, int>::foo(int, int, int) at foo.cpp:20:30"
412412
)
413413
);
414-
auto pretty_formatter = cpptrace::formatter{}
415-
.full_symbol(false);
416-
res = split(pretty_formatter.format(trace), "\n");
414+
auto pruned_formatter = cpptrace::formatter{}
415+
.symbols(cpptrace::formatter::symbol_mode::pruned);
416+
res = split(pruned_formatter.format(trace), "\n");
417417
EXPECT_THAT(
418418
res,
419419
ElementsAre(

0 commit comments

Comments
 (0)