简单的C程序调试日志

上传人:pu****.1 文档编号:492051614 上传时间:2022-10-14 格式:DOC 页数:15 大小:46.51KB
返回 下载 相关 举报
简单的C程序调试日志_第1页
第1页 / 共15页
简单的C程序调试日志_第2页
第2页 / 共15页
简单的C程序调试日志_第3页
第3页 / 共15页
简单的C程序调试日志_第4页
第4页 / 共15页
简单的C程序调试日志_第5页
第5页 / 共15页
点击查看更多>>
资源描述

《简单的C程序调试日志》由会员分享,可在线阅读,更多相关《简单的C程序调试日志(15页珍藏版)》请在金锄头文库上搜索。

1、简单的C 程序调试日志想了解更多请登陆中山大学新华学院This can be solved by letting the log class store astatic integer holding the stack depth.虽然仍有可能推断出该调用链,输出格式不到任何竭尽全力使用户很容易这样做。这是解决让log类存储静态整数堆栈深度举行。By converting the stack depth to an indentation the output can be formatted to show the call chain.通过转换堆栈压痕深度的一链的输出的格式可以显示呼叫。

2、Since it is stored in astatic member,each instance of the log class simply increments it in its constructor,and decrements it in its destructor.因为它是存储在一个静态成员,每个实例的log类的构造简单增量它,它和它的析构递减研究。Using this technique,the output becomes more readable:使用这种技术,输出变得更加清晰可辨:想了解更多请登陆中山大学新华学院论坛The logging statements

3、required to get the output above would be something like this:该记录必须得到上面的输出会是这样的语句:Collapse Copy Code int bar()诠释栏()(DEBUG_METHOD(bar);DEBUG_METHOD(酒吧);/Do bar logic/做酒吧逻辑return 1;返回1;)void foo()无效富()(DEBUG_METHOD(foo);DEBUG_METHOD(富);switch(bar()开关(巴()(case 0:案件0:/Do 0stuff,and return immediately/做0

4、的东西,并立即返回return;返回;case 1:案例一:/Do 1stuff and continue/做第一件东西,并继续break;打破;default:默认值:throw std:exception(failed);扔性病:例外(失败);)/additional logic/额外的逻辑)int main(int/*argc*/,char*/*argv*/)国际主要(诠释/*argc*/,焦炭*/*argv中获取*/)(DEBUG_METHOD(main);DEBUG_METHOD(主);foo();美孚();return 0;返回0;)Notice that regardless o

5、f how or where the foo method exists,the log output is the same as the exit output is written on scope completion of the log object instanciated at the top of every method using the DEBUG_METHOD macro.请注意,不论如何或在foo法存在,日志输出是输出相同的出口范围是写在完成log对象的实例化以后,在使用前法每DEBUG_METHOD宏。Execution time执行时间In this debug

6、 log,it is also possible to get the method exit output to include the execution time of the logged method(including any of its children).This is done by storing the current time when the log object is instanciated,and then subtracting this from the time of destruction.在此调试日志,也有可能退出该方法得到的输出包括其子女的执行时间

7、的记录方法(包括任何)。这是通过存储当前的时候log对象实例化,然后减去这从毁灭的时间。There are many ways to get atimestamp in C+,Ive decided to go with the clock()method found in time.h.As including that perticular header might not be part of the logged application,it is possible to disable the timing completely,or rather,it has to be expl

8、icitly enabled by defining DEBUG_LOG_ENABLE_TIMING.有很多方法可以得到一个在C+时间戳,我决定去与clock()中找到方法time.h应用。由于包括perticular头可能没有被记录的一部分,它可以禁用时间完全,或者更确切地说,它必须明确地启用界定DEBUG_LOG_ENABLE_TIMING。This yields aconstructor/destructor pair for the log class that looks like this:这就产生一个构造/析构函数为对log类,这是这样的:Collapse Copy Code i

9、nt log:indentation=0;诠释日志:缩进=0;std:ostream*log:stream=&std:cout;性病:ostream中*日志:流=&标准:法院;log:log(const std:string&ctx)日志:日志(常量标准:字符串和环磷酰胺):context(ctx):背景(环磷酰胺)#ifdef DEBUG_LOG_ENABLE_TIMING#ifdef DEBUG_LOG_ENABLE_TIMING,start_time(clock(),start_time(时钟()#endif#endif(write_indentation();write_indenta

10、tion();*stream-context std:endl;*流-中性病:恩德;+indentation;+缩进;)log:log()日志:?日志()(-indentation;-压痕;write_indentation(std:uncaught_exception()?*:);write_indentation(标准:uncaught_exception()?*:);*stream-context;*流-背景;#ifdef DEBUG_LOG_ENABLE_TIMING#ifdef DEBUG_LOG_ENABLE_TIMING*streamin(double)(clock()-star

11、t_time)/CLOCKS_PER_SEC)s;*流中的(双)(时钟()-start_time)/CLOCKS_PER_SEC)S型;#endif#endif*stream std:endl;*流性病:恩德;)The std:ostream*log:stream is another static member holding areference to the output method,by default set to std:cout.在std:ostream*log:stream是另一种静态成员持有方法引用到输出,通过对默认设置std:cout。Indentation by sta

12、ck depth is written using the private helper method write_indentation(),which simply uses the value stored in the static indentation variable to write acouple of spaces to log:stream.压痕深度的堆栈书,用的私人辅助方法write_indentation()它只是使用了静态的值存储indentation变量写一夫妇向空间log:stream。Exception logging异常记录When an exception

13、 is thrown,its good to know how it bubbled up the call stack,especially if it is one that is not going to be caught at all by the application(in the case of an unexpected exception).当异常被抛出,其良好知道如何调用堆栈向上冒泡,尤其是如果它是一个不会意外的异常被捕获在所有的应用程序的案件(在)。A neat function included in the STL is the std:uncaught_excep

14、tion()which returns true if theres currently an uncaught exception alive.一个整洁的STL函数是包含在std:uncaught_exception()它返回true,如果有未捕获的异常现为活着。In the destructor of the log class this is called to use an asterisk to indicate that the current method exited because of an exception(as shown in the code example ab

15、ove).在析构函数的log类,这是要求使用星号显示,目前上述方法退出因为有一个例外(例如在代码中所示)。So in the previous application example,if the bar()method had returned 2for example,then the output would have looked like this instead(provided that the exception was caught and handeled in the main method):因此,在以前的应用程序的例子,如果bar()方法返回了例子2,那么输出将有这个样子,而不是(前提是该异常被抓住的handeled的main方法):The*character is in this example indicating that when the foo()method completed,there was alive exception still uncaught,but as the main()method exit log is not preceded by a*,that exception must have been caught in that method.在*字符是

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 建筑/环境 > 综合/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号