针对上一个版本的debug调试函数做出相应的修改, 这样这个调试信息不光是写到了调试信息文件当中, 如果是Console平台的程序, 则调试信息可以实时的显示出来, 如果是运用VC编写的程序, 在调试运行的状态下, 会将调试信息现实在输出窗口当中。
该版本还有一个修改就是, 上一个版本的接口会将调试信息出现的文件的全路径显示出来, 这个版本里只会显示该文件名, 对打印信息作了相应的简化调整。
该调试信息接口的头文件如下:
extern void
dbgPrintA(const char* fname, const char* func, int line, const char* format, ...);
#if _WIN32 || _WIN64
#if _DEBUG
#define dbgPrint(_format, ...) dbgPrintA(__FILE__, __FUNCTION__, __LINE__, _format, __VA_ARGS__)
#else
#define dbgPrint(_format, ...)
#endif
#else
#if NDEBUG
#define dbgPrint(_format, ...)
#else
#define dbgPrint(_format, ...) dbgPrintA(__FILE__, __FUNCTION__, __LINE__, _format, __VA_ARGS__)
#endif
#endif
则具体接口实现如下所示:
/*
* Copyright (c) 2010,
* ASMlove. All rights reserved.
*
* http://blog.csdn.net/zfxfcx/
*
* Use, modification and distribution are subject to the
* "GNU GPL" at listed at <http://www.gnu.org/licenses/>.
*/
#if _WIN32 || _WIN64
#include <windows.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <time.h>
#include <sys/timeb.h>
#include "cl_dbg.h"
#if _WIN32 || _WIN64
#define inline __inline
#pragma warning(disable:4996)
#define __sleep(_millitm) Sleep(_millitm)
#else
#define __sleep(_millitm) usleep(_millitm * 1E3)
#endif
struct _DebugTime {
unsigned char _hour;
unsigned char _min;
unsigned char _sec;
unsigned short _usec;
};
static int g_isTake = 0;
static inline void _getDebugTime(struct _DebugTime* _dtp) {
struct timeb tb = {0};
struct tm* local = 0;
if (0 != _dtp) {
ftime(&tb);
local = localtime(&tb.time);
_dtp->_hour = local->tm_hour;
_dtp->_min = local->tm_min;
_dtp->_sec = local->tm_sec;
_dtp->_usec = tb.millitm;
}
}
static inline int _debugPrintA(const char* _fname,
const char* _func, int _line, const char* _string)
{
FILE* _File = fopen("debug.log", "a+");
struct _DebugTime dt = {0};
char string[1024] = {0};
if (NULL == _File || NULL == _fname ||
NULL == _func || NULL == _string)
return -1;
_getDebugTime(&dt);
sprintf(string, "[%.2d:%.2d:%.2d:%.3d]%s, %s[%d]: %s/n",
dt._hour, dt._min, dt._sec, dt._usec,
strrchr(_fname, '//') + 1, _func, _line, _string);
fprintf(_File, "%s", string);
fflush(_File);
printf("%s", string);
#if _WIN32 || _WIN64
OutputDebugStringA(string);
#endif
if (NULL != _File) {
fclose(_File);
_File = NULL;
}
return 0;
}
static inline int _debugInternalA(const char* _fname,
const char* _func, int _line, const char* _format, va_list _argList)
{
char buffer[1024] = {0};
if (NULL == _fname || NULL == _func ||
NULL == _format || NULL == _argList)
return -1;
vsprintf(buffer, _format, _argList);
return _debugPrintA(_fname, _func, _line, buffer);
}
void
dbgPrintA(const char* fname, const char* func, int line, const char* format, ...) {
va_list argList = NULL;
while (g_isTake)
__sleep(100);
g_isTake = 1;
va_start(argList, format);
_debugInternalA(fname, func, line, format, argList);
va_end(argList);
g_isTake = 0;
}