-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_debugging_usage.cpp
More file actions
337 lines (277 loc) · 9.63 KB
/
example_debugging_usage.cpp
File metadata and controls
337 lines (277 loc) · 9.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/**
* NUT Debugging 模块使用示例
*
* 演示如何使用 debugging 目录中的各个 API:
* - Exception: 带调试信息的异常类
* - SourceLocation: 源码位置追踪
* - Backtrace: 调用栈追踪
* - DestroyChecker: 对象生命周期检查器
* - ProcAddrMaps: 进程地址映射(仅Linux)
*/
#include <iostream>
#include <string>
#include <fstream>
// 包含 NUT debugging 模块
#include <nut/debugging/exception.h>
#include <nut/debugging/source_location.h>
#include <nut/debugging/backtrace.h>
#include <nut/debugging/destroy_checker.h>
#if NUT_PLATFORM_OS_LINUX
#include <nut/debugging/proc_addr_maps.h>
#endif
using namespace std;
using namespace nut;
// ============================================================================
// 1. Exception 使用示例
// ============================================================================
void example_exception_basic()
{
cout << "\n=== Exception 基本用法 ===" << endl;
try
{
// 最简单的异常
throw Exception(100, "简单的错误消息");
}
catch (const Exception& e)
{
cout << "错误码: " << e.get_code() << endl;
cout << "错误消息: " << e.get_message() << endl;
cout << "源文件: " << (e.get_source_file() ? e.get_source_file() : "未指定") << endl;
}
}
void example_exception_detailed()
{
cout << "\n=== Exception 详细用法(带调试信息)===" << endl;
try
{
throw Exception(
200,
"文件操作失败",
__FILE__, // 自动获取当前文件
__LINE__, // 自动获取当前行号
__FUNCTION__ // 自动获取当前函数名
);
}
catch (const Exception& e)
{
cout << "错误码: " << e.get_code() << endl;
cout << "错误消息: " << e.get_message() << endl;
cout << "源文件: " << (e.get_source_file() ? e.get_source_file() : "未指定") << endl;
cout << "完整路径: " << (e.get_source_path() ? e.get_source_path() : "未指定") << endl;
cout << "行号: " << e.get_source_line() << endl;
cout << "函数: " << (e.get_source_func() ? e.get_source_func() : "未指定") << endl;
}
}
void example_throw_on_false()
{
cout << "\n=== THROW_ON_FALSE 宏用法 ===" << endl;
int x = 5;
int y = 10;
try
{
// 如果条件为 false,自动抛出异常
THROW_ON_FALSE(x > y, 300); // x > y 为 false,会抛出异常
}
catch (const Exception& e)
{
cout << "捕获到异常!" << endl;
cout << "错误码: " << e.get_code() << endl;
cout << "条件表达式: " << e.get_message() << endl;
cout << "位置: " << e.get_source_file() << ":" << e.get_source_line() << endl;
}
}
// ============================================================================
// 2. SourceLocation 使用示例
// ============================================================================
void example_source_location()
{
cout << "\n=== SourceLocation 使用示例 ===" << endl;
// 方式1: 使用宏自动获取当前位置
SourceLocation loc = NUT_SOURCE_LOCATION;
cout << "当前位置: " << loc.to_string() << endl;
cout << "文件名: " << loc.get_file_name() << endl;
cout << "行号: " << loc.get_line_number() << endl;
cout << "函数: " << (loc.get_function_name() ? loc.get_function_name() : "未指定") << endl;
// 方式2: 手动构造
SourceLocation loc2("example.cpp", 42, "example_function");
cout << "\n手动构造的位置: " << loc2.to_string() << endl;
// 方式3: 比较
SourceLocation loc3("file.cpp", 10, "func");
SourceLocation loc4("file.cpp", 10, "func");
cout << "\n位置比较: " << (loc3 == loc4 ? "相同" : "不同") << endl;
}
void log_with_location(const string& message)
{
SourceLocation loc = NUT_SOURCE_LOCATION;
cout << "[" << loc.get_file_name()
<< ":" << loc.get_line_number()
<< "] " << message << endl;
}
// ============================================================================
// 3. Backtrace 使用示例
// ============================================================================
void function_level_3()
{
cout << "\n=== Backtrace 使用示例 ===" << endl;
cout << "当前位置: function_level_3" << endl;
// 方式1: 直接打印调用栈
cout << "\n调用栈(直接打印):" << endl;
Backtrace::print_stack();
// 方式2: 获取调用栈字符串
cout << "\n调用栈(字符串形式):" << endl;
string stack = Backtrace::backtrace();
cout << stack << endl;
// 方式3: 跳过顶层几帧
cout << "\n调用栈(跳过2层):" << endl;
string stack2 = Backtrace::backtrace(2);
cout << stack2 << endl;
}
void function_level_2()
{
function_level_3();
}
void function_level_1()
{
function_level_2();
}
// ============================================================================
// 4. DestroyChecker 使用示例
// ============================================================================
class SafeResource
{
NUT_DEBUGGING_DESTROY_CHECKER; // 添加生命周期检查器
private:
string resource_name_;
public:
SafeResource(const string& name) : resource_name_(name)
{
cout << "创建资源: " << resource_name_ << endl;
}
void use()
{
NUT_DEBUGGING_ASSERT_ALIVE; // 检查资源是否仍然有效
cout << "使用资源: " << resource_name_ << endl;
}
~SafeResource()
{
cout << "销毁资源: " << resource_name_ << endl;
}
};
void example_destroy_checker()
{
cout << "\n=== DestroyChecker 使用示例 ===" << endl;
{
SafeResource resource("test_resource");
resource.use(); // 正常使用,对象有效
// 资源会在作用域结束时自动销毁
}
// resource.use(); // 如果取消注释,在 Debug 模式下会触发 assert
}
// ============================================================================
// 5. ProcAddrMaps 使用示例(仅Linux)
// ============================================================================
#if NUT_PLATFORM_OS_LINUX
void example_proc_addr_maps()
{
cout << "\n=== ProcAddrMaps 使用示例(Linux)===" << endl;
ProcAddrMaps& maps = ProcAddrMaps::instance();
if (!maps.is_valid())
{
cout << "ProcAddrMaps 实例无效" << endl;
return;
}
// 获取可执行文件路径
const string& exec_path = maps.get_exec_path();
cout << "可执行文件路径: " << exec_path << endl;
// 加载所有模块映射
maps.load();
// 尝试查找一些常见的库
const char* libs[] = {
"libc.so",
"libstdc++.so",
"libpthread.so"
};
for (size_t i = 0; i < sizeof(libs) / sizeof(libs[0]); ++i)
{
ProcAddrMaps::addr_type addr = 0;
if (maps.find(libs[i], &addr))
{
cout << libs[i] << " 加载地址: 0x" << hex << addr << dec << endl;
}
}
}
#endif
// ============================================================================
// 6. 综合示例:完整的错误处理
// ============================================================================
class DataProcessor
{
public:
void process_file(const string& filename)
{
SourceLocation loc = NUT_SOURCE_LOCATION;
cout << "\n=== 综合示例:处理文件 ===" << endl;
cout << "调用位置: " << loc.to_string() << endl;
// 使用 THROW_ON_FALSE 进行参数验证
THROW_ON_FALSE(!filename.empty(), 1001);
ifstream file(filename);
if (!file.is_open())
{
throw Exception(
1002,
"无法打开文件: " + filename,
__FILE__, __LINE__, __FUNCTION__
);
}
// 模拟处理
cout << "文件处理成功: " << filename << endl;
}
void process_with_error_handling(const string& filename)
{
try
{
process_file(filename);
}
catch (const Exception& e)
{
// 记录详细的错误信息
cout << "\n错误报告:" << endl;
cout << " 错误码: " << e.get_code() << endl;
cout << " 错误消息: " << e.get_message() << endl;
cout << " 位置: " << e.get_source_file()
<< ":" << e.get_source_line() << endl;
cout << " 函数: " << e.get_source_func() << endl;
// 打印调用栈
cout << "\n调用栈:" << endl;
Backtrace::print_stack();
}
}
};
// ============================================================================
// 主函数
// ============================================================================
int main()
{
cout << "NUT Debugging 模块使用示例" << endl;
cout << "============================" << endl;
// 1. Exception 示例
example_exception_basic();
example_exception_detailed();
example_throw_on_false();
// 2. SourceLocation 示例
example_source_location();
log_with_location("这是一条带位置信息的日志");
// 3. Backtrace 示例
function_level_1();
// 4. DestroyChecker 示例
example_destroy_checker();
#if NUT_PLATFORM_OS_LINUX
// 5. ProcAddrMaps 示例(仅Linux)
example_proc_addr_maps();
#endif
// 6. 综合示例
DataProcessor processor;
processor.process_with_error_handling("test.txt");
processor.process_with_error_handling(""); // 会触发异常
return 0;
}