以下代码可用在程序调试中,很方便的 //Debug.h /// //Debug.h #ifndef _DEBUG_H_ #define _DEBUG_H_ #include <assert.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #if defined(_UNICODE) || defined(UNICODE) #define YTEXT L void YLibTrace(const wchar_t* lpszFormat, ...); #else #define YTEXT void YLibTrace(const char* lpszFormat, ...); #endif //模拟断言 #define YLIB_ASSERT(x,y) do{ if(!(y)) {YLibTrace(x);assert(0);} }while(0) //模拟 MFC里面的TRACE #define YLIB_TRACE0(x1) YLibTrace(x1) #define YLIB_TRACE1(x1,x2) YLibTrace(x1,x2) #define YLIB_TRACE2(x1,x2,x3) YLibTrace(x1,x2,x3) #define YLIB_TRACE3(x1,x2,x3,x4) YLibTrace(x1,x2,x3,x4) #define YLIB_TRACE4(x1,x2,x3,x4,x5) YLibTrace(x1,x2,x3,x4,x5) #define YLIB_TRACE5(x1,x2,x3,x4,x5,x6) YLibTrace(x1,x2,x3,x4,x5,x6) #define YLIB_TRACE6(x1,x2,x3,x4,x5,x6,x7) YLibTrace(x1,x2,x3,x4,x5,x6,x7) #define YLIB_TRACE7(x1,x2,x3,x4,x5,x6,x7,x8) YLibTrace(x1,x2,x3,x4,x5,x6,x7,x8) //分别输出整形数、浮点数和字符串 #define TRACE_PARAMETER(prefix,number) YLibTrace(prefix,#number,number) #define TRACE_INTEGER(number) TRACE_PARAMETER(YTEXT("%s = %d/n"),number) #define TRACE_FLOAT(number) TRACE_PARAMETER(YTEXT("%s = %12.6f/n"),number) #define TRACE_DOUBLE(number) TRACE_PARAMETER(YTEXT("%s = %12.6f/n"),number) #define TRACE_STRING(number) TRACE_PARAMETER(YTEXT("%s = %s/n"),number) //将二进制缓冲区打印成字符串 //比如unsigned char buf[]={0x23,0xDC,0x00,0x45}; //TRACE_BUFFER(buf,5) 即显示"23 DC 00 45" #if defined(_UNICODE) || defined(UNICODE) #define TRACE_BUFFER(buffer,bufferLen) / { / wchar_t* lp##buffer=(wchar_t*)malloc(sizeof(wchar_t)*bufferLen*3); / memset(lp##buffer,0,bufferLen*3*sizeof(wchar_t)); / wchar_t sz##buffer[4]=YTEXT("/0"); / for(int i=0;i<(int)bufferLen;i++) / { / swprintf(sz##buffer,YTEXT("%02X"),(unsigned char)buffer[i]); / if(i<bufferLen-1) wcscat(sz##buffer,YTEXT(" ")); / wcscat(lp##buffer,sz##buffer); / } / YLibTrace(L("%s = %s/n"),#buffer,lp##buffer); / free(lp##buffer); / } #else #define TRACE_BUFFER(buffer,bufferLen) / { / char* lp##buffer=(char*)malloc(sizeof(char)*bufferLen*3); / memset(lp##buffer,0,bufferLen*3*sizeof(char)); / char sz##buffer[4]="/0"; / for(int i=0;i<(int)bufferLen;i++) / { / sprintf(sz##buffer,"%02X",(unsigned char)buffer[i]); / if(i<bufferLen-1) strcat(sz##buffer,(" ")); / strcat(lp##buffer,sz##buffer); / } / YLibTrace("%s = %s/n",#buffer,lp##buffer); / free(lp##buffer); / } #endif #endif //Debug.cpp /// //Debug.cpp #include "debug.h" #include <stdio.h> #include <stdarg.h> #if defined(_UNICODE) || defined(UNICODE) void YLibTrace(const wchar_t* lpszFormat, ...) { int nBuf; wchar_t szBuffer[1024]={0}; va_list args; va_start(args, lpszFormat); nBuf = vswprintf((wchar_t*)szBuffer,1024,lpszFormat, args); assert(nBuf >= 0); wprintf(szBuffer); va_end(args); } #else void YLibTrace(const char* lpszFormat, ...) { int nBuf; char szBuffer[1024]="/0"; va_list args; va_start(args, lpszFormat); nBuf = vsprintf(szBuffer,lpszFormat, args); assert(nBuf >= 0); printf(szBuffer); va_end(args); } #endif //测试代码 // 使用方法 //test.cpp #include "debug.h" bool trueFunc() {return true;} bool falseFunc() {return false;} int main() { int i=0; double d=0.123456; unsigned char buffer[]={0xC0,0x00,0x12,0xDD,0xEE,0xFF,0xC0}; TRACE_INTEGER(i); TRACE_DOUBLE(d); TRACE_BUFFER(buffer,7); YLIB_TRACE0("Trace0"); YLIB_TRACE1("Trace1:%d",i); YLIB_TRACE3("Trace3:%d,%d,%d",i,i+1,i+2); YLIB_ASSERT("Assert:Error!",trueFunc()); YLIB_ASSERT("Assert:Error!",falseFunc()); return 0; } 可以自己看看结果。