针对上一个版本的debug print又做了一些相应的改进, 主要的改进功能是整个工程的代码完全是运用的标准CRT的函数来实现的, 这为可移植性做了相应的推进; 同时, 这个版本还在打印用户自己相应的调试信息的时候, 附带打印出了某局调试语句出现在那个文件、所在的函数以及具体的行数。
同样的话就不多说了, 请各位同学多多指教……
同时需要指出的是, 可变参数宏, 在windows平台下, 从VC2005才开始支持, 在windows平台下使用的同学需要注意了。还有便是并没有进行linux平台的测试, 相应在linux平台使用的同学可能需要进行对应的更改。
其头文件主要声明如下:
#if _WIN32 || _WIN64
#define inline __inline
#define DEBUG _DEBUG
#endif
extern void dbgPrintA(const char* file, const char* func, int line, const char* format, ...);
#ifdef DEBUG
#define dbgPrint(_format, ...) dbgPrintA(__FILE__, __FUNCTION__, __LINE__, _format, __VA_ARGS__)
#else
#define dbgPrint(_format, ...)
#endif /* !DEBUG */
这个调试函数的具体接口实现如下:
/*
* Copyright (c) 2010,
* ASMlove. All rights reserved.
*
* Use, modification and distribution are subject to the
* "GNU GPL" at listed at <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <time.h>
#include <sys/timeb.h>
#if _WIN32 || _WIN64
#include <windows.h>
#pragma warning(disable:4996)
#define sleep(s) Sleep(s)
#endif
#include "dbg.h"
struct _LogTime {
unsigned char _hour;
unsigned char _min;
unsigned char _sec;
unsigned short _usec;
};
static int g_isTake = 0;
static inline void
_getLogTime(struct _LogTime* _tm) {
struct timeb tb = {0};
struct tm* local = NULL;
if (NULL == _tm) return;
ftime(&tb);
local = localtime(&(tb.time));
_tm->_hour = local->tm_hour;
_tm->_min = local->tm_min;
_tm->_sec = local->tm_sec;
_tm->_usec = tb.millitm;
}
static inline int
_debugPrintA(const char* _debugSTR,
const char* _file, const char* _func, int _line) {
FILE* _File = fopen("debug.log", "a+");
struct _LogTime lt = {0};
if (NULL == _debugSTR || NULL == _File ||
NULL == _file || NULL == _func)
return 0;
_getLogTime(<);
fprintf(_File, "[%.2d:%.2d:%.2d:%.3d]%s, %s[%d]: %s/n",
lt._hour, lt._min, lt._sec, lt._usec, _file, _func, _line, _debugSTR);
fflush(_File);
if (NULL != _File) {
fclose(_File);
_File = NULL;
}
return 1;
}
static inline int
_debugInternalA(const char* _format, va_list _argList,
const char* _file, const char* _func, int _line) {
char buffer[1024] = {0};
if (NULL == _format || NULL == _argList ||
NULL == _file || NULL == _func)
return 0;
vsprintf(buffer, _format, _argList);
return _debugPrintA(buffer, _file, _func, _line);
}
void
dbgPrintA(const char* file, const char* func,
int line, const char* format, ...) {
va_list argList = NULL;
while (g_isTake) sleep(10);
g_isTake = 1;
va_start(argList, format);
_debugInternalA(format, argList, file, func, line);
va_end(argList);
g_isTake = 0;
}