//DBTrace.h
#pragma once
/************************************************************************
*1.如果在VC6中使用该头文件,需要define VC6宏,VC6不能输出函数名,不支持long long类型
* (也就是64位整数)
*2.如果需要输出函数参数,需要define _FUNCSIG宏
*3.使用 #DBMsg(str) 可以使编译器编译时在输出窗口输出一条提示语句,双击提示语句可转到
* #DBMsg(str)所在的地方
*4.DBASSERT断言在调试的时候如果出现断言错误,则在输出窗口输出文件名,行号,函数名等信息
* 双击该输出语句可以直接转到出错语句相应的代码所在的地方
*5.其他宏是为了方便调试使用,输出窗口为调试时候的输出窗口,也可以通过修改后面的
* _DBTraceStr__函数修改默认输出窗口,DBTrace(val)调试的时候可在输出窗口同时输出
* 文件名,行号,函数名等信息,双击该输出语句可以直接转到相应的代码所在的地方
*6.带有Ex的宏会使输出语句后面自动换行
*7.如果一个类实现了Trace(char*valname)函数,则可以直接输出对象的调试信息
*8.相应例子可以看test.cpp文件
************************************************************************/
#define VC6
#define __FUNCSIG
#ifdef VC6
# define _FUNC_ " "
#else
# ifndef __FUNCSIG
# define _FUNC_ __FUNCTION__
# else
# define _FUNC_ __FUNCSIG__
# endif
#endif
#define DBMsg(msg) message(_FILE_LINE_FUNC msg "/n")
#ifndef _DEBUG
#define DBASSERT(exp)
#define DBTraceStr(str)
#define DBTraceStrEx(str)
#define DBTraceVal(val)
#define DBTraceValEx(val)
#define DBTrace(val)
#define DBTraceEx(val)
#define DBTRaceObj(val)
#else
#include "DBTrace.HPP"
#endif
#define _LINE_(line) #line
#define _LINE(line) _LINE_(line)
#define _FILE_LINE __FILE__"(" _LINE(__LINE__) ")"
#define _FILE_LINE_FUNC _FILE_LINE ":" _FUNC_ " "
// "DBTrace.HPP"
#pragma once
#include<string>
#include <cstdio>
#include<cassert>
#pragma warning(disable:4127)
#ifndef ASSERT
#define ASSERT(exp) assert(exp)
#endif
typedef unsigned char _CHARU;
typedef short _SHORT;
typedef unsigned short _SHORTU;
typedef int _INT;
typedef unsigned int _INTU;
typedef long _LONG;
typedef unsigned long _LONGU;
#ifdef VC6
typedef long _INTMAX;
typedef unsigned long _INTMAXU;
#else
typedef long long _INTMAX;
typedef unsigned long long _INTMAXU;
#endif
#ifdef WIN32
#include <windows.h>
inline void _DBTraceStr__(char const * const str) {OutputDebugStringA(str);}
inline void _DBTraceStr__(wchar_t const *const str) {OutputDebugStringW(str);}
#else
#include <iostream>
inline void _DBTraceStr__(char const * const str) {std::cout<<str;}
inline void _DBTraceStr__(wchar_t const *const str) {std::wcout<<str;}
#endif
#define DBASSERT(exp) do{if(!exp){_DBTrace__(__FILE__,__LINE__,_FUNC_);/
_DBTraceStr__("Assert Failed: ");_DBTraceStr__(#exp); /
_DBTraceStr__("/n");ASSERT(exp);}}while(0)
#define DBTraceStr(str) _DBTraceStr__(str)
#define DBTraceStrEx(str) do{ _DBTraceStr__(str);_DBTraceStr__("/n");}while(0)
#define DBTraceVal(val) _DBTraceVal__(#val,val)
#define DBTraceValEx(val) do{ _DBTraceVal__(#val,val);_DBTraceStr__("/n");} /
while(0)
#define DBTrace(val) do{ _DBTrace__(__FILE__,__LINE__,_FUNC_); /
_DBTraceVal__(#val,val);} while(0)
#define DBTraceEx(val) do{ _DBTrace__(__FILE__,__LINE__,_FUNC_); /
_DBTraceVal__(#val,val);_DBTraceStr__("/n");}while(0)
//字符串处理
inline void _DBTraceVal__(char *valname,char const* const val){
_DBTraceStr__(" ");
_DBTraceStr__(valname);
_DBTraceStr__("=");
_DBTraceStr__(val);
}
inline void _DBTraceVal__(char *valname,wchar_t const* const val){
_DBTraceStr__(" ");
_DBTraceStr__(valname);
_DBTraceStr__("=");
_DBTraceStr__(val);
}
inline void _DBTraceVal__(char *valname,std::string const & val){
_DBTraceStr__(" ");
_DBTraceStr__(valname);
_DBTraceStr__("=");
_DBTraceStr__(val.c_str());
}
inline void _DBTraceVal__(char *valname,std::wstring const& val){
_DBTraceStr__(" ");
_DBTraceStr__(valname);
_DBTraceStr__("=");
_DBTraceStr__(val.c_str());
}
//指针处理
inline void _DBTraceVal__(char *valname,void const * const val){
_DBTraceStr__(" ");
char szBuf[16];
sprintf(szBuf,"0X%p",val);
std::string str=valname;
str+="=";
str+=szBuf;
_DBTraceStr__(str.c_str());
}
//字符处理
inline void _DBTraceVal__(char *valname,char val){
_DBTraceStr__(" ");
std::string str=valname;
str+="=";
str+=val;
_DBTraceStr__(str.c_str());
}
/************************************************************************/
/* */
/************************************************************************/
inline void _DBTraceVal__(char *valname,_INTMAX val){
_DBTraceStr__(" ");
char buf[24];
#ifdef VC6
sprintf(buf,"=%ld",val);
#else
sprintf(buf,"=%I64d",val);
#endif
std::string str=valname;
str+=buf;
_DBTraceStr__(str.c_str());
}
inline void _DBTraceVal__(char *valname,_INTMAXU val){
_DBTraceStr__(" ");
char buf[24];
#ifdef VC6
sprintf(buf,"=%lu",val);
#else
sprintf(buf,"=%I64u",val);
#endif
std::string str=valname;
str+=buf;
_DBTraceStr__(str.c_str());
}
inline void _DBTraceVal__(char *valname,double val){
_DBTraceStr__(" ");
char buf[24];
sprintf(buf,"=%g",val);
std::string str=valname;
str+=buf;
_DBTraceStr__(str.c_str());
}
#define FUNC_INIT(src,dest) /
inline void _DBTraceVal__(char *valname,src val) /
{ _DBTraceVal__(valname,(dest)val);}
#define FUNC_INITEX(src,dest) FUNC_INIT(src,dest) FUNC_INIT(src##U,dest##U)
FUNC_INIT(_CHARU,char)
FUNC_INIT(_SHORT,_INTMAX)
FUNC_INITEX(_INT,_INTMAX)
#ifndef VC6
FUNC_INITEX(_LONG,_INTMAX)
#endif
FUNC_INIT(float,double)
/************************************************************************/
/* */
/************************************************************************/
void _DBTrace__(char*file,long line, char* func){
char buf[24];
sprintf(buf,"%ld",line);
std::string str=file;
str+="(";
str+=buf;
#ifndef VC6
str+=") :in ";
str+=func;
#else
str+="): ";
#endif
_DBTraceStr__(str.c_str());
}
void _DBTrace__(char*file,long line, char* func,char * op){
DBASSERT(op!=NULL);
char buf[24];
sprintf(buf,"%ld",line);
std::string str=file;
str+="(";
str+=buf;
str+=") : ";
#ifndef VC6
str+=op;
str+=func;
#endif
_DBTraceStr__(str.c_str());
}
#define DBTRaceObj(val) val.Trace(#val)
//test
#include "stdafx.h"
#include "DBTrace.h"
#include <typeinfo>
#include <string>
using namespace std;
class Test{
string _str;
public:
Test(char*str):_str(str){}
#ifdef _DEBUG
void Trace(char *valname){
DBTraceStr(valname);
DBTraceValEx(_str);
}
#endif
};
int main(){
#pragma DBMsg("warning")
char ch='a';
unsigned char uch='b';
int i=-100;
unsigned int ui=100;
long l=-1000;
unsigned long ul=1000;
char *sz="nihao";
string str=string("str");
float f=100.885f;
DBTraceValEx(ch);
DBTraceValEx(uch);
DBTraceValEx(i);
DBTraceValEx(ui);
DBTraceValEx(l);
DBTraceValEx(ul);
DBTraceValEx(sz);
DBTraceValEx(str);
DBTraceValEx(f);
DBTraceEx(ch);
DBTraceEx(uch);
DBTraceEx(i);
DBTraceEx(ui);
DBTraceEx(l);
DBTraceEx(ul);
DBTraceEx(sz);
DBTraceEx(str);
DBTraceEx(f);
DBASSERT(sz==NULL);
return 0;
}