Trace.Indent

C#
[ConditionalAttribute("TRACE")] 
public static void Indent ()
示例:
Trace.WriteLine("List of errors:");
 Trace.Indent();
 Trace.WriteLine("Error 1: File not found");
 Trace.WriteLine("Error 2: Directory not found");
 Trace.Unindent();
 Trace.WriteLine("End of list of errors");
该示例产生下面的输出: 
List of errors:
     Error 1: File not found
     Error 2: Directory not found
End of list of errors
class Colorize { public: Colorize(std::FILE*) {} void init() {} void set_color(Color::type) {} }; #endif // BACKWARD_SYSTEM_LINUX class Printer { public: bool snippet; bool color; bool address; bool object; Printer(): snippet(true), color(true), address(false), object(false) {} template <typename ST> FILE* print(ST& st, FILE* os = stderr) { Colorize colorize(os); if (color) { colorize.init(); } print_header(os, st.thread_id()); _resolver.load_stacktrace(st); for (size_t trace_idx = st.size(); trace_idx > 0; --trace_idx) { print_trace(os, _resolver.resolve(st[trace_idx-1]), colorize); } return os; } template <typename IT> FILE* print(IT begin, IT end, FILE* os = stderr, size_t thread_id = 0) { Colorize colorize(os); if (color) { colorize.init(); } print_header(os, thread_id); for (; begin != end; ++begin) { print_trace(os, *begin, colorize); } return os; } private: TraceResolver _resolver; SnippetFactory _snippets; void print_header(FILE* os, unsigned thread_id) { fprintf(os, "Stack trace (most recent call last)"); if (thread_id) { fprintf(os, " in thread %u:\n", thread_id); } else { fprintf(os, ":\n"); } } void print_trace(FILE* os, const ResolvedTrace& trace, Colorize& colorize) { fprintf(os, "#%-2u", trace.idx); bool already_indented = true; if (!trace.source.filename.size() || object) { fprintf(os, " Object \"%s\", at %p, in %s\n", trace.object_filename.c_str(), trace.addr, trace.object_function.c_str()); already_indented = false; } for (size_t inliner_idx = trace.inliners.size(); inliner_idx > 0; --inliner_idx) { if (!already_indented) { fprintf(os, " "); } const ResolvedTrace::SourceLoc& inliner_loc = trace.inliners[inliner_idx-1]; print_source_loc(os, " | ", inliner_loc); if (snippet) { print_snippet(os, " | ", inliner_loc, colorize, Color::purple, 5); } already_indented = false; } if (trace.source.filename.size()) { if (!already_indented) { fprintf(os, " "); } print_source_loc(os, " ", trace.source, trace.addr); if (snippet) { print_snippet(os, " ", trace.source, colorize, Color::yellow, 7); } } } void print_snippet(FILE* os, const char* indent, const ResolvedTrace::SourceLoc& source_loc, Colorize& colorize, Color::type color_code, int context_size) { using namespace std; typedef SnippetFactory::lines_t lines_t; lines_t lines = _snippets.get_snippet(source_loc.filename, source_loc.line, context_size); for (lines_t::const_iterator it = lines.begin(); it != lines.end(); ++it) { if (it-> first == source_loc.line) { colorize.set_color(color_code); fprintf(os, "%s>", indent); } else { fprintf(os, "%s ", indent); } fprintf(os, "%4u: %s\n", it->first, it->second.c_str()); if (it-> first == source_loc.line) { colorize.set_color(Color::reset); } } } void print_source_loc(FILE* os, const char* indent, const ResolvedTrace::SourceLoc& source_loc, void* addr=0) { fprintf(os, "%sSource \"%s\", line %i, in %s", indent, source_loc.filename.c_str(), (int)source_loc.line, source_loc.function.c_str()); if (address && addr != 0) { fprintf(os, " [%p]\n", addr); } else { fprintf(os, "\n"); } } }; 详细注释上述代码
最新发布
09-29
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值