14
14
15
15
namespace cpptrace {
16
16
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;
24
26
25
27
public:
26
28
void set_header (std::string header) {
27
- this -> header = std::move (header);
29
+ options. header = std::move (header);
28
30
}
29
31
void set_color_mode (formatter::color_mode mode) {
30
- this -> color = mode;
32
+ options. color = mode;
31
33
}
32
34
void set_address_mode (formatter::address_mode mode) {
33
- this -> addresses = mode;
35
+ options. addresses = mode;
34
36
}
35
37
void set_snippets (bool snippets) {
36
- this -> snippets = snippets;
38
+ options. snippets = snippets;
37
39
}
38
40
void set_snippet_context (int lines) {
39
- this -> context_lines = lines;
41
+ options. context_lines = lines;
40
42
}
41
43
void include_column (bool columns) {
42
- this -> columns = columns;
44
+ options. columns = columns;
43
45
}
44
46
void set_filter (std::function<bool (const stacktrace_frame&)> filter) {
45
- this -> filter = filter;
47
+ options. filter = filter;
46
48
}
47
49
48
50
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));
50
52
}
51
53
52
54
std::string format (const stacktrace& trace, detail::optional<bool > color_override = detail::nullopt) const {
@@ -107,9 +109,9 @@ namespace cpptrace {
107
109
}
108
110
109
111
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 );
111
113
if (
112
- (color == color_mode::automatic || color == color_mode::always) &&
114
+ (options. color == color_mode::automatic || options. color == color_mode::always) &&
113
115
(!color_override || color_override.unwrap () != false ) &&
114
116
stream_is_tty (stream)
115
117
) {
@@ -126,8 +128,8 @@ namespace cpptrace {
126
128
}
127
129
128
130
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 ' ;
131
133
}
132
134
std::size_t counter = 0 ;
133
135
const auto & frames = trace.frames ;
@@ -141,8 +143,8 @@ namespace cpptrace {
141
143
if (newline_at_end || &frame != &frames.back ()) {
142
144
stream << ' \n ' ;
143
145
}
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);
146
148
}
147
149
counter++;
148
150
}
@@ -178,7 +180,7 @@ namespace cpptrace {
178
180
if (frame.is_inline ) {
179
181
str += microfmt::format (" {<{}}" , 2 * sizeof (frame_ptr) + 2 , " (inlined)" );
180
182
} 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 ;
182
184
str += microfmt::format (" {}0x{>{}:0h}{}" , blue, 2 * sizeof (frame_ptr), address, reset);
183
185
}
184
186
if (!frame.symbol .empty ()) {
@@ -188,7 +190,7 @@ namespace cpptrace {
188
190
str += microfmt::format (" at {}{}{}" , green, frame.filename , reset);
189
191
if (frame.line .has_value ()) {
190
192
str += microfmt::format (" :{}{}{}" , blue, frame.line .value (), reset);
191
- if (frame.column .has_value () && columns) {
193
+ if (frame.column .has_value () && options. columns ) {
192
194
str += microfmt::format (" :{}{}{}" , blue, frame.column .value (), reset);
193
195
}
194
196
}
0 commit comments