/*用于调试 */
#include "stdafx.h"
#include "stdlib.h"
#include <iostream>
#include "strsafe.h"
/*包含文件 C:/Program Files/Microsoft Visual Studio 8/VC/include/FengDebugOutput.h*/
#pragma comment(lib,"strsafe.lib")
#pragma warning (disable : 4995 )/*关闭wsprintf*/
#pragma warning (disable :4996 )/*关闭strcat*/
#define _PREDEF_LABEL
/*预定义为空符号,可在后来重定义这个宏,用做条件开关,例如定义为 _DEBUG,则表示只能在DEBUG下才可使用*/
#ifdef _PREDEF_LABEL
#pragma once
#define PAUSE system("pause"); /*暂停*/
#ifndef EXIT
#define EXIT exit(0);
#endif
#ifdef _X86_
#define BREAKHERE __asm {int 3 }/*设置断点*/
#endif
/* 废弃的,原因是没有多线程安全
#define PA(x) std::cout<<std::dec<<(x)<<"/0";
#define PW(x) std::wcout<<std::dec<<(x)<<L"/0";
#ifdef UNICODE
#define _P PW
#else
#define _P PA
#endif
#ifndef P
#define P _P
#endif
#define PLA(x) std::cout<<std::dec<<(x)<<std::endl;
#define PLW(x) std::cout<<std::dec<<(x)<<std::endl;
#ifdef UNICODE
#define PL PLW
#else
#define PL PLA
#endif
#define PHEXA(x) std::cout<<std::hex<<(x)<<"/0";
#define PHEXW(x) std::wcout<<std::hex<<(x)<<L"/0";
#ifdef UNICODE
#define PHEX PHEXW
#else
#define PHEX PHEXA
#endif
#define PHEXLA(x) std::cout<<std::hex<<(x)<<std::endl;
#define PHEXLW(x) std::cout<<std::hex<<(x)<<std::endl;
#ifdef UNICODE
#define PHEXL PHEXLW
#else
#define PHEXL PHEXLA
#endif
*/
inline void __stdcall ConsoleShowLastError(){
printf("GetLastError = %u/n/0",GetLastError());
}
inline void WINAPI DebugShowLastError(){
WCHAR errstr[32];
wsprintfW(errstr,L"GetLastError : %u/n/0",(unsigned int )GetLastError());
OutputDebugStringW(errstr);
}
/*输出错误值的描述*/
inline void WINAPI ConsoleShowLastErrorInfo(){
DWORD dwerr=GetLastError();
char errstr[128];
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM,
NULL
,dwerr,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
errstr,
128,
NULL
);
printf("GetLastError = %u , Error Detail : %s/n/0",dwerr,errstr);
}
inline void WINAPI DebugShowLastErrorInfo(){
DWORD dwerr=GetLastError();
WCHAR errstr[128];
FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM,
NULL
,dwerr,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
errstr,
128,
NULL
);
WCHAR strout[160];
memset(strout,0,160);
wsprintfW(strout,L"GetLastError = %u ,Error Detail : %s/n/0",dwerr,errstr);
OutputDebugStringW(strout);
}
#define CERROR ConsoleShowLastError();
#define DERROR DebugShowLastError();
#define CERRINFO ConsoleShowLastErrorInfo();
#define DERRINFO DebugShowLastErrorInfo();
inline void __stdcall ConsoleShowLastErrorInfoTimeFileLine(__in char* using__TIMESTAMP__,__in char* using__FILE__,__in int using__LINE__,__in_opt char* szMsg/*最大长度为128*/){
DWORD dwerr=GetLastError();
char errstr[512];
memset(errstr,0,512);
strcat(errstr,"-------------------Error Information-------------------/n");
if(using__TIMESTAMP__){
strcat(errstr,"MODIFIED TIME : ");
strcat(errstr,using__TIMESTAMP__);
strcat (errstr,"/n");
}
if(using__FILE__){
strcat(errstr,"FILE : ");
strcat(errstr,using__FILE__);
strcat(errstr,"/n");
}
if(using__LINE__){
char szline[4];
_itoa(using__LINE__,szline,10);
strcat(errstr,"LINE : ");
strcat(errstr,szline);
strcat(errstr,"/n");
}
char szerrinfo[128];
char szOutput[1024];
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM,
NULL
,dwerr,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
szerrinfo,
128,
NULL
);
memset(szOutput,0,1024);
wsprintfA(szOutput,
"%sGetLastError = %u/nError Detail : %s",
errstr, dwerr,szerrinfo
);
char sz1[129];
memset(sz1,0,sizeof(sz1));
if(szMsg){
int a=(int)strlen(szMsg)+1;
int b =(a<128?a:128);
memcpy((char*)sz1,szMsg,b);
strcat(szOutput,"Additional Message : ");
strcat(szOutput,szMsg);
strcat(szOutput,"/n");
}
strcat(szOutput,"--------------------------End--------------------------/n/0");
printf(szOutput);
}
inline void __stdcall DebugShowLastErrorInfoTimeFileLine(__in char* using__TIMESTAMP__,__in char* using__FILE__,__in int using__LINE__,__in_opt char* szMsg/*最大长度为128*/){
DWORD dwerr=GetLastError();
char errstr[512];
memset(errstr,0,512);
strcat(errstr,"-------------------Error Information-------------------/n");
if(using__TIMESTAMP__){
strcat(errstr,"MODIFIED TIME : ");
strcat(errstr,using__TIMESTAMP__);
strcat (errstr,"/n");
}
if(using__FILE__){
strcat(errstr,"FILE : ");
strcat(errstr,using__FILE__);
strcat(errstr,"/n");
}
if(using__LINE__){
char szline[4];
_itoa(using__LINE__,szline,10);
strcat(errstr,"LINE : ");
strcat(errstr,szline);
strcat(errstr,"/n");
}
char szerrinfo[128];
char szOutput[1024];
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM,
NULL
,dwerr,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
szerrinfo,
128,
NULL
);
memset(szOutput,0,1024);
wsprintfA(szOutput,
"%sGetLastError = %u/nError Detail : %s",
errstr, dwerr,szerrinfo
);
char sz1[129];
memset(sz1,0,sizeof(sz1));
if(szMsg){
int a=(int)strlen(szMsg)+1;
int b =(a<128?a:128);
memcpy((char*)sz1,szMsg,b);
strcat(szOutput,"Additional Message : ");
strcat(szOutput,szMsg);
strcat(szOutput,"/n");
}
strcat(szOutput,"--------------------------End--------------------------/n/0");
OutputDebugStringA(szOutput);
}
#define CERRINFOTFL(t,f,l,msg) ConsoleShowLastErrorInfoTimeFileLine(t,f,l,msg);
#define DERRINFOTFL(t,f,l,msg) DebugShowLastErrorInfoTimeFileLine(t,f,l,msg);
#ifndef FENG_DEF_DEBUG_CLASS
#define FENG_DEF_DEBUG_CLASS
/*注意:由于字符集(ascii和unicode)并不能直接通过函数重载自动识别,因此只能直接调用函数原型,而不是通过宏定义调用,否则会出错*/
class MyDebugOutPut{
public:
inline static void WriteLine(int value){
char outstr[13];
wsprintfA(outstr,"%d/n/0",value);
OutputDebugStringA(outstr);
}
inline static void WriteLine(unsigned int value){
char outstr[13];
memset(outstr,0,12*sizeof(char));
wsprintfA(outstr,"%u/n/0",value);
OutputDebugStringA(outstr);
}
inline static void WriteLine(long value){
MyDebugOutPut::WriteLine((int)value);
}
inline static void WriteLine(unsigned long value){
MyDebugOutPut::WriteLine((unsigned int)value);
}
inline static void WriteLine(__int64 value){
MyDebugOutPut::WriteLine((unsigned __int64)value);
}
inline static void WriteLine(unsigned __int64 value){
char str[24];
wsprintfA(str,"%I64d/n/0",value);
OutputDebugStringA(str);
}
inline static void WriteLine(double value){
char str[24];
StringCbPrintfA(str,20*sizeof(char),"%lf/n/0",value);/*使用printf的风格,所以不能使wsprintf函数*/
OutputDebugStringA(str);
}
inline static void WriteLine(char* value){
if(value){
OutputDebugStringA(value);//注意多线程,可能被分割
OutputDebugStringA("/n/0");
}
}
inline static void WriteLine(wchar_t* value){
if(value){
OutputDebugStringW(value);//注意多线程,可能被分割
OutputDebugStringW(L"/n/0");
}
}
inline static void WriteLine( char value){
char temp[4];
wsprintfA(temp,"%c/n/0",value);
OutputDebugStringA(temp);
}
inline static void WriteLine(wchar_t value){
wchar_t temp[4];
wsprintfW(temp,L"%c/n/0",value);
OutputDebugStringW(temp);
}
inline static void Write(int value){
char outstr[13];
wsprintfA(outstr,"%d",value);
OutputDebugStringA(outstr);
}
inline static void Write(unsigned int value){
char outstr[13];
wsprintfA(outstr,"%u",value);
OutputDebugStringA(outstr);
}
inline static void Write(__int64 value){
char str[24];
wsprintfA(str,"%I64d",value);
OutputDebugStringA(str);
}
inline static void Write(unsigned __int64 value){
char str[24];
wsprintfA(str,"%I64d",value);
OutputDebugStringA(str);
}
inline static void Write(char* value){
if(value){
OutputDebugStringA(value);
}
}
inline static void Write(wchar_t* value){
if(value){
OutputDebugStringW(value);
}
}
inline static void Write(char value){
char temp[3];
wsprintfA(temp,"%c",value);
OutputDebugStringA(temp);
}
inline static void Write(wchar_t value){
wchar_t temp[3];
wsprintfW(temp,L"%c",value);
OutputDebugStringW(temp);
}
inline static void Write(double value){
char str[24];
StringCbPrintfA(str,20*sizeof(char),"%lf",value);/*使用printf的风格,所以不能使用wsprintf函数 f */
OutputDebugStringA(str);
}
/*下面为十六进制 调试 输出,其他非unsigned int 类型的,调用时需转换*/
inline static void WriteHex(unsigned int value){
char str[11];
wsprintfA(str,"%x/0",value);
OutputDebugStringA(str);
}
inline static void WriteHexLine(unsigned int value){
char str[11];
wsprintfA(str,"%x/n/0",value);
OutputDebugStringA(str);
}
/*下面为控制台输出*/
inline static void Printf(int value){
printf("%i/0",value);
}
inline static void Printf(long value){
printf("%ld/0",value);
}
inline static void Printf(unsigned int value){
printf("%u/0",value);
}
inline static void Printf(unsigned long value){
printf("%lu/0",value);
}
inline static void Printf(char value){
printf("%c/0",value);
}
inline static void Printf(wchar_t value){
printf("%C/0",value);
}
inline static void Printf(double value){
printf("%lf/0",value);
}
inline static void Printf(long long value){
printf("%I64d/0",value);
}
inline static void Printf(unsigned long long value){
printf("%I64d/0",value);
}
inline static void Printf(char* value){
printf(value);
}
inline static void Printf(wchar_t* value){
wprintf(value);
}
inline static void PrintfLine(int value){
printf("%i/n/0",value);
}
inline static void PrintfLine(long value){
printf("%ld/n/0",value);
}
inline static void PrintfLine(unsigned int value){
printf("%u/n/0",value);
}
inline static void PrintfLine(unsigned long value){
printf("%lu/n/0",value);
}
inline static void PrintfLine(char value){
printf("%c/n/0",value);
}
inline static void PrintfLine(wchar_t value){
printf("%C/n/0",value);
}
inline static void PrintfLine(double value){
printf("%lf/n/0",value);
}
inline static void PrintfLine(long long value){
printf("%I64d/n/0",value);
}
inline static void PrintfLine(unsigned long long value){
printf("%I64d/n/0",value);
}
inline static void PrintfLine(char* value){
printf("%s/n/0",value);
}
inline static void PrintSLine(wchar_t* value){
wprintf(L"%s/n/0",value);
}
virtual ~MyDebugOutPut(){}
};
#define DbgWriteline(x) MyDebugOutPut::WriteLine(x);
#define DbgWrite(x) MyDebugOutPut::Write(x);
/*简化调试输出*/
#ifndef PD
#define PD DbgWrite
#endif
#ifndef PDL
#define PDL DbgWriteline
#endif
/*打印标识符 vs的调试框输出*/
#define DbgWriteIDentLine(x) char str[260]; /
wsprintfA(str,"%s/n/0",#x); /
OutputDebugStringA(str);
#define DbgWriteIDent(x) char str[260]; /
wsprintfA(str,"%s/0",#x); /
OutputDebugStringA(str);
/*简化调试输出 标识符*/
#ifndef PDID
#define PDID DbgWriteIDent
#endif
#ifndef PDIDL
#define PDIDL DbgWriteIDentLine
#endif
/*打印标识符 控制台输出*/
#define PCIDENTL(x) printf("%s/n/0",#x);
#define PCIDENT(x) printf("%s/0",#x);
#define PConsole(x) MyDebugOutPut::Printf(x) ;
#define PConsoleLine(x) MyDebugOutPut::PrintfLine(x);
/*简化控制台输出*/
#ifndef PC
#define PC PConsole
#endif
#ifndef PCL
#define PCL PConsoleLine
#endif
/*十六进制 控制台 输出,仅表示无符号的32位int ,这样就能够有效的表示地址*/
#define PConsoleHex(x) printf("%x/0",(unsigned int)x);
#define PConsoleHexLine(x) printf("%x/n/0",(unsigned int)x);
/*简化 十六进制 控制台 输出*/
#ifndef PCHEX
#define PCHEX PConsoleHex
#endif
#ifndef PCHEXL
#define PCHEXL PConsoleHexLine
#endif
/*十六进制 调试输出 */
#ifndef PDHEX
#define PDHEX MyDebugOutPut::WriteHex
#endif
#ifndef PDHEXL
#define PDHEXL MyDebugOutPut::WriteHexLine
#endif
#endif //FENG_DEF_DEBUG_CLASS
/*摘自windows核心编程(第五版)附录A,p722*/
inline void chMB(const char* szMsg){
char szTitle[MAX_PATH];
GetModuleFileNameA(NULL,szTitle,_countof(szTitle));
MessageBoxA(GetActiveWindow(),szMsg,szTitle,MB_OK);
}
inline void chFAIL(char* szMsg){
chMB(szMsg);
/*取消断点
#ifdef _X86_
__asm int 3
#endif*/
}
inline void cdASSERTFAIL(char* file,int line,const char* expr){
char sz[2*MAX_PATH];
wsprintfA(sz,"文件: %s, 行: %d ,结果: %s",file,line,expr);
chFAIL(sz);
}
#define chASSERT(x) if((!x)) cdASSERTFAIL(__FILE__,__LINE__,#x)
/*任何类型转换到char* ,因为是宏,没有类型检查*/
#define chVERIFY(x) chASSERT(x)
#endif //_PREDEF_LABEL
//
/*获得符号名称(不是该符号的值),例如win32定义的宏,变量等*/
/************************************************************************/
/* 示例代码:要求调用方需要先确定缓冲区,由宏的第二个参数输出
char str5[260];
char str6[260];
GetIdentfifierA(WM_CLEAR,str5);
GetIdentfifierA(WM_CREATE,str6);
PL(str6);
PL(str5); */
/************************************************************************/
#define GetIdentfifierW(src,dest) wsprintfW(dest,L"%s/0",#src); //获得符号,保存在dest
#define GetIdentfifierA(src,dest) wsprintfA(dest,"%s/0",#src);
/*
#ifdef UNICODE 宏无法判断 字符集
#define GetIdentfifier GetIdentfifierW
#else
#define GetIdentfifier GetIdentfifierA
#endif
*/