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

来源:互联网 发布:java中单引号和双引号 编辑:程序博客网 时间:2024/05/16 04:34

在朗讯的几年总是习惯通过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();

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

}