www.pudn.com > mysee.zip > DebugTrace.h


/* 
 *  Openmysee 
 * 
 *  This program is free software; you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation; either version 2 of the License, or 
 *  (at your option) any later version. 
 * 
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details. 
 * 
 *  You should have received a copy of the GNU General Public License 
 *  along with this program; if not, write to the Free Software 
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 * 
 */ 
// $_FILEHEADER_BEGIN **************************** 
//  
//            
// 文件名称: DebugTrace.h 
// 创建日期: 2005-04-04 
// 创 建 人: 张宏涛 
// 文件说明: 用于程序调试的宏定义,主要包括ASSERT和TRACE语句 
/* 
对外接口函数说明: 
 
	//断言函数.若传入参数不为true则出现断言失败对话框, 并终止程序运行; 
	//发行版中则什么也不做 
	//eg:  ASSERT(apStrVal != NULL);	 
1、void ASSERT(bool)  
	 
	//记录日志 level为日志等级,args为日志内容	 
	//eg:  RecordLog(1,"姓名:" << lszName<<"年龄:"<< 26 << "性别" << true);	 
	//TRACE在发行版中为空函数 
2、void TRACE/RecordLog(level, args)  
	 
	//打印调试信息(0-6表示调试信息的级别,0级最高),发行版中什么也不做 
	//eg:TRACE2("姓名:%s,年龄:%d,性别:%d",lszName,26,true); 
3、TRACE0 - TRACE6	 
 
	//设置打印日值的级别(静态函数) 
4、	void CDebugTrace::SetTraceLevel(int aiTraceLevel); 
	 
	//设置日志文件名(静态函数) 
5、	void CDebugTrace::SetLogFileName(char *aszLogFile); 
	 
	// 设置TRACE选项 .注意函数可以 OR 各选项(静态函数) 
6、	void CDebugTrace::SetTraceOptions(unsigned options); 
 
	//取得TRACE选项(静态函数) 
7、	unsigned CDebugTrace::GetTraceOptions(void);	 
 
典型用法:   
 
	CDebugTrace::SetTraceLevel(6);	//设置可打印的日志级别 
    //设置日志选项 
	CDebugTrace::SetTraceOptions(CDebugTrace::GetTraceOptions() \ 
		| CDebugTrace::Timestamp | CDebugTrace::LogLevel\ 
		& ~CDebugTrace::FileAndLine | CDebugTrace::AppendToFile\ 
		| CDebugTrace::PrintToConsole); 
 
	//设置日志保存文件名 
	CDebugTrace::SetLogFileName("d:\\test.log"); 
 
    TRACE0("姓名:%s,年龄:%d,性别:%d",lszName,26,true); 
	RecordLog(1,"姓名:" << lszName<<"年龄:"<< 26 << "性别" << true);	 
 
	ASSERT(apStrVal != NULL);	 
*/ 
// $_FILEHEADER_END ****************************** 
 
#ifndef _DEBUGTRACE_HEAD_ 
#define _DEBUGTRACE_HEAD_ 
 
#ifdef WIN32 
#include  
#endif 
#include "CriticalSection.h" 
//#include "TypeDeff.h" 
////////////////////////////////////////////////////////////////////// 
//有关ASSERT的定义 
 
#undef ASSERT		//取消ASSERT宏 
 
//  重新定义ASSERT宏   
#ifdef _DEBUG		//测试版 
 
	#define ASSERT(f)               \ 
	if(f)							\ 
		;							\ 
	else                            \ 
		CDebugTrace::AssertFail(#f,__FILE__, __LINE__) 
 
#else			//发行版 
 
    #define ASSERT(f) 
 
#endif 
 
////////////////////////////////////////////////////////////////////// 
//有关TRACE的定义 
 
//RecordLog是流方式的日志输出 
//eg: RecordLog(1,"姓名:" << lszName<<"年龄:"<< 26 << "性别" << true);	 
#define RecordLog(level, args) \ 
if (!CDebugTrace::CanTrace(level)) 	;  else\ 
	CDebugTrace::EndTrace(CDebugTrace::BeginTrace(level,__FILE__,__LINE__) << args << '\n') 
 
//TraceLogX系列是类似printf方式输出,X表示日志级别 
//eg:TRACE0("姓名:%s,年龄:%d,性别:%d",lszName,26,true); 
#define TraceLog0 \ 
	if (!CDebugTrace::CanTrace(0)) 	;  else\ 
		CDebugTrace::BeginTrace(0,__FILE__,__LINE__).TraceFormat 
 
#define TraceLog1 \ 
    if (!CDebugTrace::CanTrace(1)) 	;  else\ 
        CDebugTrace::BeginTrace(1,__FILE__,__LINE__).TraceFormat 
 
#define TraceLog2 \ 
    if (!CDebugTrace::CanTrace(2)) 	;  else\ 
        CDebugTrace::BeginTrace(2,__FILE__,__LINE__).TraceFormat 
 
#define TraceLog3 \ 
    if (!CDebugTrace::CanTrace(3)) 	;  else\ 
        CDebugTrace::BeginTrace(3,__FILE__,__LINE__).TraceFormat 
 
#define TraceLog4 \ 
    if (!CDebugTrace::CanTrace(4)) 	;  else\ 
        CDebugTrace::BeginTrace(4,__FILE__,__LINE__).TraceFormat 
 
#define TraceLog5 \ 
    if (!CDebugTrace::CanTrace(5)) 	;  else\ 
        CDebugTrace::BeginTrace(5,__FILE__,__LINE__).TraceFormat 
 
#define TraceLog6 \ 
    if (!CDebugTrace::CanTrace(6)) 	;  else\ 
        CDebugTrace::BeginTrace(6,__FILE__,__LINE__).TraceFormat 
 
//取消TRACE定义 
#undef TRACE 
#undef TRACE0 
#undef TRACE1 
#undef TRACE2 
#undef TRACE3 
#undef TRACE4 
#undef TRACE5 
#undef TRACE6 
 
//重新定义TRACE语句 
#ifdef _DEBUG			//调试版 
 
	#define TRACE RecordLog	//打印日志 
 
	#define TRACE0 TraceLog0	 
 
	#define TRACE1 TraceLog1 
	 
	#define TRACE2 TraceLog2 
	 
	#define TRACE3 TraceLog3 
	 
	#define TRACE4 TraceLog4 
	 
	#define TRACE5 TraceLog5 
	 
	#define TRACE6 TraceLog6 
	 
#else					//发行版 
 
	#define TRACE(level, args) //TRACE函数为空 
 
	#define TRACE0 ; 
	#define TRACE1 ; 
	#define TRACE2 ; 
	#define TRACE3 ; 
	#define TRACE4 ; 
	#define TRACE5 ; 
	#define TRACE6 ; 
 
#endif //_DEBUG 
 
////////////////////////////////////////////////////////////////// 
//下面是CDebugTrace类的定义 
#define DEF_TRACE_BUFF_LEN 2048 
class CDebugTrace 
{ 
private: 
	long		mlDataLen;			 //数据长度 
    char		mszPrintBuff[DEF_TRACE_BUFF_LEN];	 //打印数据缓存 
	 
    UC::CCriticalSection moCriticalSection;     //临界区 
private: 
    static int		   mnLogLevel;			 //日志等级 
	static char		   mszLogFileName[512]; //日志文件名称 
	static unsigned    muTraceOptions;		 //打印日志选项	 
 
public: 
	//打印选项 
	enum Options  
	{ 
		/// 打印时间 
		Timestamp = 1, 
		/// 打印日志级别 
		LogLevel = 2, 
		/// 打印源文件名和行号 
		FileAndLine = 4, 
		/// 把日志追加到文件中 
		AppendToFile = 8, 
		///输出日志到控制台 
		PrintToConsole = 16 
	};     
 
	//构造函数	 
	CDebugTrace(); 
	//析够函数 
	~CDebugTrace(); 
public: 
	//设置日志级别(0级最高,1其次,依次类推,小于该等级的日志不打印) 
	static void SetTraceLevel(int aiTraceLevel); 
	 
	//设置日志文件名 
	static void SetLogFileName(char *aszLogFile); 
 
	// 设置TRACE选项 .注意函数可以 OR 各选项 
	static void SetTraceOptions(unsigned options /** New level for trace */ ); 
 
	//取得TRACE选项 
	static unsigned GetTraceOptions(void); 
 
	//判断给定级别是否可以打印 
	static bool CanTrace(int aiLogLevel);					 
 
	//开始打印 
	static CDebugTrace& BeginTrace(int aiLogLevel,char *apSrcFile,int aiSrcLine);	 
		 
	//结束打印 
	static void EndTrace(CDebugTrace &aoDebugTrace); 
 
	//断言失败处理函数 
	static void AssertFail(char * strCond,char *strFile, unsigned uLine); 
 
public: 
    //按照函数printf的类似格式打印日志 
	void TraceFormat(const char * pFmt,...);	 
 
	//以下分别输出各种数据类型 
	CDebugTrace& operator << (char acCharVal);	 
	CDebugTrace& operator << (bool abBoolVal); 
	CDebugTrace& operator << (short asShortVal); 
	CDebugTrace& operator << (unsigned short asShortVal); 
	CDebugTrace& operator << (int aiIntVal); 
	CDebugTrace& operator << (unsigned int aiIntVal); 
	CDebugTrace& operator << (long alLongVal); 
	CDebugTrace& operator << (unsigned long alLongVal); 
	CDebugTrace& operator << (float afFloatVal); 
	CDebugTrace& operator << (double afdoubleVal); 
	CDebugTrace& operator << (__int64 aiInt64Val); 
	CDebugTrace& operator << (const char *apStrVal);	 
}; 
 
#endif//_DEBUGTRACE_HEAD_