1
1
#include < cpptrace/cpptrace.hpp>
2
+ #include < cpptrace/formatting.hpp>
2
3
3
4
#include < cstddef>
4
5
#include < cstdint>
9
10
#include < string>
10
11
#include < vector>
11
12
13
+ #include " cpptrace/basic.hpp"
12
14
#include " symbols/symbols.hpp"
13
15
#include " unwind/unwind.hpp"
14
16
#include " demangle/demangle.hpp"
@@ -129,41 +131,13 @@ namespace cpptrace {
129
131
return detail::get_frame_object_info (raw_address);
130
132
}
131
133
132
- static std::string frame_to_string (
133
- bool color,
134
- const stacktrace_frame& frame
135
- ) {
136
- const auto reset = color ? RESET : " " ;
137
- const auto green = color ? GREEN : " " ;
138
- const auto yellow = color ? YELLOW : " " ;
139
- const auto blue = color ? BLUE : " " ;
140
- std::string str;
141
- if (frame.is_inline ) {
142
- str += microfmt::format (" {<{}}" , 2 * sizeof (frame_ptr) + 2 , " (inlined)" );
143
- } else {
144
- str += microfmt::format (" {}0x{>{}:0h}{}" , blue, 2 * sizeof (frame_ptr), frame.raw_address , reset);
145
- }
146
- if (!frame.symbol .empty ()) {
147
- str += microfmt::format (" in {}{}{}" , yellow, frame.symbol , reset);
148
- }
149
- if (!frame.filename .empty ()) {
150
- str += microfmt::format (" at {}{}{}" , green, frame.filename , reset);
151
- if (frame.line .has_value ()) {
152
- str += microfmt::format (" :{}{}{}" , blue, frame.line .value (), reset);
153
- if (frame.column .has_value ()) {
154
- str += microfmt::format (" :{}{}{}" , blue, frame.column .value (), reset);
155
- }
156
- }
157
- }
158
- return str;
159
- }
160
-
161
134
std::string stacktrace_frame::to_string () const {
162
135
return to_string (false );
163
136
}
164
137
165
138
std::string stacktrace_frame::to_string (bool color) const {
166
- return frame_to_string (color, *this );
139
+ // return frame_to_string(color, *this);
140
+ return get_default_formatter ().format (*this , color);
167
141
}
168
142
169
143
std::ostream& operator <<(std::ostream& stream, const stacktrace_frame& frame) {
@@ -195,89 +169,34 @@ namespace cpptrace {
195
169
}
196
170
197
171
void stacktrace::print () const {
198
- print (std::cerr, true );
172
+ get_default_formatter (). print (* this );
199
173
}
200
174
201
175
void stacktrace::print (std::ostream& stream) const {
202
- print (stream, true );
176
+ get_default_formatter (). print (stream, * this );
203
177
}
204
178
205
179
void stacktrace::print (std::ostream& stream, bool color) const {
206
- print (stream, color, true , nullptr );
207
- }
208
-
209
- static void print_frame (
210
- std::ostream& stream,
211
- bool color,
212
- unsigned frame_number_width,
213
- std::size_t counter,
214
- const stacktrace_frame& frame
215
- ) {
216
- std::string line = microfmt::format (" #{<{}} {}" , frame_number_width, counter, frame.to_string (color));
217
- stream << line;
180
+ get_default_formatter ().print (stream, *this , color);
218
181
}
219
182
220
- void stacktrace::print (std::ostream& stream, bool color, bool newline_at_end, const char * header) const {
221
- if (
222
- color && (
223
- (&stream == &std::cout && isatty (stdout_fileno)) || (&stream == &std::cerr && isatty (stderr_fileno))
224
- )
225
- ) {
226
- detail::enable_virtual_terminal_processing_if_needed ();
227
- }
228
- stream << (header ? header : " Stack trace (most recent call first):" ) << ' \n ' ;
229
- std::size_t counter = 0 ;
230
- if (frames.empty ()) {
231
- stream << " <empty trace>\n " ;
232
- return ;
233
- }
234
- const auto frame_number_width = detail::n_digits (static_cast <int >(frames.size ()) - 1 );
235
- for (const auto & frame : frames) {
236
- print_frame (stream, color, frame_number_width, counter, frame);
237
- if (newline_at_end || &frame != &frames.back ()) {
238
- stream << ' \n ' ;
239
- }
240
- counter++;
183
+ namespace detail {
184
+ const formatter& get_default_snippet_formatter () {
185
+ static formatter snippet_formatter = formatter{}.set_snippets (true );
186
+ return snippet_formatter;
241
187
}
242
188
}
243
189
244
190
void stacktrace::print_with_snippets () const {
245
- print_with_snippets (std::cerr, true );
191
+ detail::get_default_snippet_formatter (). print (* this );
246
192
}
247
193
248
194
void stacktrace::print_with_snippets (std::ostream& stream) const {
249
- print_with_snippets ( stream, true );
195
+ detail::get_default_snippet_formatter (). print ( stream, * this );
250
196
}
251
197
252
198
void stacktrace::print_with_snippets (std::ostream& stream, bool color) const {
253
- print_with_snippets (stream, color, true , nullptr );
254
- }
255
-
256
- void stacktrace::print_with_snippets (std::ostream& stream, bool color, bool newline_at_end, const char * header) const {
257
- if (
258
- color && (
259
- (&stream == &std::cout && isatty (stdout_fileno)) || (&stream == &std::cerr && isatty (stderr_fileno))
260
- )
261
- ) {
262
- detail::enable_virtual_terminal_processing_if_needed ();
263
- }
264
- stream << (header ? header : " Stack trace (most recent call first):" ) << ' \n ' ;
265
- std::size_t counter = 0 ;
266
- if (frames.empty ()) {
267
- stream << " <empty trace>" << ' \n ' ;
268
- return ;
269
- }
270
- const auto frame_number_width = detail::n_digits (static_cast <int >(frames.size ()) - 1 );
271
- for (const auto & frame : frames) {
272
- print_frame (stream, color, frame_number_width, counter, frame);
273
- if (newline_at_end || &frame != &frames.back ()) {
274
- stream << ' \n ' ;
275
- }
276
- if (frame.line .has_value () && !frame.filename .empty ()) {
277
- stream << detail::get_snippet (frame.filename , frame.line .value (), 2 , color);
278
- }
279
- counter++;
280
- }
199
+ detail::get_default_snippet_formatter ().print (stream, *this , color);
281
200
}
282
201
283
202
void stacktrace::clear () {
@@ -289,13 +208,12 @@ namespace cpptrace {
289
208
}
290
209
291
210
std::string stacktrace::to_string (bool color) const {
292
- std::ostringstream oss;
293
- print (oss, color, false , nullptr );
294
- return std::move (oss).str ();
211
+ return get_default_formatter ().format (*this , color);
295
212
}
296
213
297
214
std::ostream& operator <<(std::ostream& stream, const stacktrace& trace) {
298
- return stream << trace.to_string ();
215
+ get_default_formatter ().print (stream, trace);
216
+ return stream;
299
217
}
300
218
301
219
CPPTRACE_FORCE_NO_INLINE
0 commit comments