您的位置:首页 > 编程语言 > C语言/C++

c++程序调试加入进出函数跟踪模块

2011-08-16 18:35 423 查看
在朗讯的几年总是习惯通过log找程序问题;写了这个小小的跟踪模块。

还是非常ugly的,以后加入log level.

头文件:traceService.h

/*copyright drbinzhao@hotmail.com edit in 2010 */

#ifndef TRACESERVICE_H

#define TRACESERVICE_H

#include <stdio.h>

#ifdef TRACESERVICE

#define TRACE_SERVICE_ENTRY_EXIT() \

const TraceService TraceService(__LINE__,__func__,__FILE__)

#else

#define TRACE_SERVICE_ENTRY_EXIT()

#endif

class TraceService

{

public:

TraceService( const unsigned int Line, const char * Function_Name, const char * File_name);

~TraceService();

protected:

private:

const char * _m_function_name;

unsigned int _m_line;

const char * _m_file_name;

};

char *GetFileName(const char *path);

#endif // TRACESERVICE_H

traceService.cpp

/*copyright drbinzhao@hotmail.com edit in 2010*/

#include "traceservice.h"

#include <string.h>

TraceService::TraceService(const unsigned int Line, const char * Function_Name,const char * File_name)

{

_m_line=Line;

_m_file_name = GetFileName(File_name);

_m_function_name= Function_Name;

printf("Enter function %-20s (%s, %d)\n",_m_function_name,_m_file_name,_m_line);

}

TraceService::~TraceService()

{

printf("Exit function %-20s (%s, %d)\n",_m_function_name,_m_file_name,_m_line);

}

char *GetFileName(const char *path)

{

char *p;

p=strrchr(path, '/');

if (p)

return p+1;

else

return NULL;

}

用法:

xxx.cpp

#include "traceService.h"

func1()

{

TRACE_SERVICE_ENTRY_EXIT();

..........

}

func2()

{

TRACE_SERVICE_ENTRY_EXIT();

.............

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: