由于上一次的版本是只支持UNICODE编码的,这次经过改进让这个函数可以同时支持UNICODE字符集和多字节字符集,希,望大家多多指教……
/***
* debuglog.h - interface of the debugprint
*
* Copyright (c) 2009 by zfwolf <AsmSdk@hotmail.com>.
*
* This program is free software: you can redistribute it and modify it under
* the terms of the GNU General Public License as published by the Free Soft-
* ware foundation, either version 3 of the License, or any later version.
*
* This program is distributed in the hope that it will be useful, but WITH-
* OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*************************************************************************/
#ifndef __DEBUG_LOG_H__
#define __DEBUG_LOG_H__
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#include <windows.h>
#include <tchar.h>
#if !defined(_DEBUG_LOG_)
#define __DEBUG_PRINT_LOG__
#endif /* _DEBUG_LOG_ */
extern void DebugPrint(LPCTSTR fmt, ...);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* __DEBUG_LOG_H__ */
其具体实现如下:
/***
* debugprint.c - implementation of the debugprint
*
* Copyright (c) 2009 by zfwolf <AsmSdk@hotmail.com>.
*
* This program is free software: you can redistribute it and modify it under
* the terms of the GNU General Public License as published by the Free Soft-
* ware foundation, either version 3 of the License, or any later version.
*
* This program is distributed in the hope that it will be useful, but WITH-
* OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*************************************************************************/
#include "debuglog.h"
#include <stdio.h>
#include <sys/timeb.h>
#include <time.h>
static BOOL g_bTakeControl = FALSE; /* take control of the display */
/***
* extern void DebugPrint(LPCTSTR fmt, ...)
*
* Purpose:
* this function to display the debug information into the file of
* 'debug.log'
*
* Entry:
* @param1 LPCTSTR fmt
* @param2 ...
*
* Exit: <void>
*
* Exceptions:
*************************************************************************/
extern void DebugPrint(LPCTSTR fmt, ...)
{
#ifdef __DEBUG_PRINT_LOG__
TCHAR* lpBuffer = NULL; /* buffer to display */
int size = 0; /* bytes of buffer */
int len = 0; /* len of buffer */
FILE* pFile = NULL;
va_list pArgs = NULL;
struct tm* pTm = NULL;
struct timeb tmb;
while (g_bTakeControl)
Sleep(100);
g_bTakeControl = TRUE;
va_start(pArgs, fmt);
len = _vsctprintf(fmt, pArgs) + 1;
size = len * sizeof(TCHAR);
if (NULL == (lpBuffer =
HeapAlloc(GetProcessHeap(), 0, size)))
return;
memset(lpBuffer, 0, size);
_vstprintf(lpBuffer, fmt, pArgs);
va_end(pArgs);
if (NULL == (pFile = _tfopen(_T("debug.log"), _T("a+"))))
return;
memset(&tmb, 0, sizeof(struct timeb));
ftime(&tmb);
pTm = localtime(&tmb.time);
_ftprintf(pFile, _T("(%.4d-%.2d-%.2d %.2d:%.2d:%.2d:%.3d) --- %s/n"),
pTm->tm_year + 1900, pTm->tm_mon + 1, pTm->tm_mday,
pTm->tm_hour, pTm->tm_min, pTm->tm_sec, tmb.millitm,
lpBuffer);
if (NULL != pFile) {
fclose(pFile);
pFile = NULL;
}
if (NULL != lpBuffer) {
HeapFree(GetProcessHeap(), 0, lpBuffer);
lpBuffer = NULL;
}
g_bTakeControl = FALSE;
#endif /* __DEBUG_PRINT_LOG__ */
}