现在把改良后的debug调试程序放在这里。针对以前曾经给出的唆实现的功能并没有什么变化, 只是把程序修改得更加简洁罢了。或许这也是体现自己的成长之路的吧, 可以看出这几个版本之间, 我个人的编程风格的一些变化, 以及自己的设计哲学的逐步形成吧。
各位同学, 有什么高见尽请提出, 我一直都希望大家可以多多交流, 这也可以更快、更直接地促进我们的进步……
好了, 先说头文件吧, 还是和以前的版本一样, 利用ifdef来控制只在debug版本打印出相应的调试信息:
#if _WIN32 || _WIN64
#define inline __inline
#endif
extern void dbgPrintA(const char* format, ... );
#ifdef _DEBUG
#define dbgPrint dbgPrintA
#else
#define dbgPrint
#endif /* _DEBUG */
在内部我们添加两个函数来处理内部的过程:
static int _debugInternalA(const char* _format, va_list _argList);
static int _debugPrintA(const char* _debugSTR);
这样整个内部实现如下:
static int g_isTake = 0;
void dbgPrintA(const char* format, ... ) {
va_list argList = NULL;
__try {
while (g_isTake) Sleep(10);
g_isTake = 1;
va_start(argList, format);
_debugInternalA(format, argList);
} __finally {
va_end(argList);
g_isTake = 0;
}
}
static inline int
_debugInternalA(const char* _format, va_list _argList) {
char dbgSTR[1024] = {0};
if (NULL == _format || NULL == _argList) return 0;
_vsnprintf(dbgSTR, 1024, _format, _argList);
return _debugPrintA(dbgSTR);
}
static inline int
_debugPrintA(const char* _debugSTR) {
FILE* _File = NULL;
SYSTEMTIME st = {0};
if (NULL == _debugSTR) return 0;
_File = fopen("debug.log", "a+");
if (NULL == _File) return 0;
GetLocalTime(&st);
fprintf(_File, "[%.2d:%.2d:%.2d:%.3d] - %s/n",
st.wHour, st.wMinute, st.wSecond,
st.wMilliseconds, _debugSTR);
fflush(_File);
if (NULL != _File) {
fclose(_File); _File = NULL;
}
return 1;
}