Skip to content

Commit e06951a

Browse files
committed
Fixes
1 parent 061ed96 commit e06951a

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

src/formatting.cpp

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,41 @@
1414

1515
namespace cpptrace {
1616
class formatter::impl {
17-
std::string header = "Stack trace (most recent call first):";
18-
color_mode color = color_mode::automatic;
19-
address_mode addresses = address_mode::raw;
20-
bool snippets = false;
21-
int context_lines = 2;
22-
bool columns = true;
23-
std::function<bool(const stacktrace_frame&)> filter = [] (const stacktrace_frame&) { return true; };
17+
struct {
18+
std::string header = "Stack trace (most recent call first):";
19+
color_mode color = color_mode::automatic;
20+
address_mode addresses = address_mode::raw;
21+
bool snippets = false;
22+
int context_lines = 2;
23+
bool columns = true;
24+
std::function<bool(const stacktrace_frame&)> filter = [] (const stacktrace_frame&) { return true; };
25+
} options;
2426

2527
public:
2628
void set_header(std::string header) {
27-
this->header = std::move(header);
29+
options.header = std::move(header);
2830
}
2931
void set_color_mode(formatter::color_mode mode) {
30-
this->color = mode;
32+
options.color = mode;
3133
}
3234
void set_address_mode(formatter::address_mode mode) {
33-
this->addresses = mode;
35+
options.addresses = mode;
3436
}
3537
void set_snippets(bool snippets) {
36-
this->snippets = snippets;
38+
options.snippets = snippets;
3739
}
3840
void set_snippet_context(int lines) {
39-
this->context_lines = lines;
41+
options.context_lines = lines;
4042
}
4143
void include_column(bool columns) {
42-
this->columns = columns;
44+
options.columns = columns;
4345
}
4446
void set_filter(std::function<bool(const stacktrace_frame&)> filter) {
45-
this->filter = filter;
47+
options.filter = filter;
4648
}
4749

4850
std::string format(const stacktrace_frame& frame, detail::optional<bool> color_override = detail::nullopt) const {
49-
return frame_to_string(frame, color_override.value_or(color == color_mode::always));
51+
return frame_to_string(frame, color_override.value_or(options.color == color_mode::always));
5052
}
5153

5254
std::string format(const stacktrace& trace, detail::optional<bool> color_override = detail::nullopt) const {
@@ -107,9 +109,9 @@ namespace cpptrace {
107109
}
108110

109111
bool should_do_color(std::ostream& stream, detail::optional<bool> color_override) const {
110-
bool do_color = color == color_mode::always || color_override.value_or(false);
112+
bool do_color = options.color == color_mode::always || color_override.value_or(false);
111113
if(
112-
(color == color_mode::automatic || color == color_mode::always) &&
114+
(options.color == color_mode::automatic || options.color == color_mode::always) &&
113115
(!color_override || color_override.unwrap() != false) &&
114116
stream_is_tty(stream)
115117
) {
@@ -126,8 +128,8 @@ namespace cpptrace {
126128
}
127129

128130
void print_internal(std::ostream& stream, const stacktrace& trace, bool newline_at_end, bool color) const {
129-
if(!header.empty()) {
130-
stream << header << '\n';
131+
if(!options.header.empty()) {
132+
stream << options.header << '\n';
131133
}
132134
std::size_t counter = 0;
133135
const auto& frames = trace.frames;
@@ -141,8 +143,8 @@ namespace cpptrace {
141143
if(newline_at_end || &frame != &frames.back()) {
142144
stream << '\n';
143145
}
144-
if(frame.line.has_value() && !frame.filename.empty() && snippets) {
145-
stream << detail::get_snippet(frame.filename, frame.line.value(), context_lines, color);
146+
if(frame.line.has_value() && !frame.filename.empty() && options.snippets) {
147+
stream << detail::get_snippet(frame.filename, frame.line.value(), options.context_lines, color);
146148
}
147149
counter++;
148150
}
@@ -178,7 +180,7 @@ namespace cpptrace {
178180
if(frame.is_inline) {
179181
str += microfmt::format("{<{}}", 2 * sizeof(frame_ptr) + 2, "(inlined)");
180182
} else {
181-
auto address = addresses == address_mode::raw ? frame.raw_address : frame.object_address;
183+
auto address = options.addresses == address_mode::raw ? frame.raw_address : frame.object_address;
182184
str += microfmt::format("{}0x{>{}:0h}{}", blue, 2 * sizeof(frame_ptr), address, reset);
183185
}
184186
if(!frame.symbol.empty()) {
@@ -188,7 +190,7 @@ namespace cpptrace {
188190
str += microfmt::format(" at {}{}{}", green, frame.filename, reset);
189191
if(frame.line.has_value()) {
190192
str += microfmt::format(":{}{}{}", blue, frame.line.value(), reset);
191-
if(frame.column.has_value() && columns) {
193+
if(frame.column.has_value() && options.columns) {
192194
str += microfmt::format(":{}{}{}", blue, frame.column.value(), reset);
193195
}
194196
}

0 commit comments

Comments
 (0)