在实际的项目开发过程中我们经常需要打印出当前的程序运行流程,需要知道程序运行到什么地方了?以便于我们调试程序,查找出BUG。在这里我给出一个自己写的追踪程序流程的打印调试函数,供大家参考,同时我也相信我的程序写的不怎么样?希望高手可以提供更加完美的调试打印函数……
下面是这个函数的头文件:
/***
* debuglog.h - interface of the debugprint
*
* Copyright (c) 2009 by zfwolf < zfeagles@163.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__
* debuglog.h - interface of the debugprint
*
* Copyright (c) 2009 by zfwolf < zfeagles@163.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 */
extern "C" {
#endif /* __cplusplus */
#include <windows.h>
#include <tchar.h>
#include <tchar.h>
#if !defined(_DEBUG_LOG_)
#define __DEBUG_PRINT_LOG__
#endif /* _DEBUG_LOG_ */
#define __DEBUG_PRINT_LOG__
#endif /* _DEBUG_LOG_ */
extern void DebugPrint(LPCWSTR fmt, ... );
#ifdef __cplusplus
}
#endif /* __cplusplus */
}
#endif /* __cplusplus */
#endif /* __DEBUG_LOG_H__ */
下面是这个函数的具体实现文件:
/***
* debugprint.c - implementation of the debugprint
*
* Copyright (c) 2009 by zfwolf < zfeagles@163.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>
* debugprint.c - implementation of the debugprint
*
* Copyright (c) 2009 by zfwolf < zfeagles@163.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(LPCWSTR fmt, ... )
*
* Purpose:
* this function to display the debug information into the file of
* 'debug.log'
*
* Entry:
* @param1 LPCWSTR fmt
* @param2 ...
*
* Exit: <void>
*
* Exceptions:
*************************************************************************/
extern void DebugPrint(LPCWSTR fmt, ... )
{
#ifdef __DEBUG_PRINT_LOG__
TCHAR* lpBuffer = NULL; /* buffer to display */
int len = 0; /* len of buffer */
FILE* pFile = NULL;
va_list pArgs = NULL;
struct tm* pTm = NULL;
struct timeb tmb;
* extern void DebugPrint(LPCWSTR fmt, ... )
*
* Purpose:
* this function to display the debug information into the file of
* 'debug.log'
*
* Entry:
* @param1 LPCWSTR fmt
* @param2 ...
*
* Exit: <void>
*
* Exceptions:
*************************************************************************/
extern void DebugPrint(LPCWSTR fmt, ... )
{
#ifdef __DEBUG_PRINT_LOG__
TCHAR* lpBuffer = NULL; /* buffer to display */
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;
Sleep(100);
g_bTakeControl = TRUE;
va_start(pArgs, fmt);
len = _vscwprintf(fmt, pArgs) + 1;
if (NULL == (lpBuffer = (TCHAR*)malloc(sizeof(TCHAR) * len)))
return;
_vstprintf(lpBuffer, fmt, pArgs);
va_end(pArgs);
len = _vscwprintf(fmt, pArgs) + 1;
if (NULL == (lpBuffer = (TCHAR*)malloc(sizeof(TCHAR) * len)))
return;
_vstprintf(lpBuffer, fmt, pArgs);
va_end(pArgs);
if (NULL == (pFile = _wfopen(L"debug.log", L"a+")))
return;
memset(&tmb, 0, sizeof(struct timeb));
ftime(&tmb);
pTm = localtime(&tmb.time);
fwprintf(pFile, L"(%.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,
return;
memset(&tmb, 0, sizeof(struct timeb));
ftime(&tmb);
pTm = localtime(&tmb.time);
fwprintf(pFile, L"(%.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 != lpBuffer) {
free(lpBuffer);
lpBuffer = NULL;
}
if (NULL != pFile) {
fclose(pFile);
pFile = NULL;
}
g_bTakeControl = FALSE;
#endif /* __DEBUG_PRINT_LOG__ */
}
free(lpBuffer);
lpBuffer = NULL;
}
if (NULL != pFile) {
fclose(pFile);
pFile = NULL;
}
g_bTakeControl = FALSE;
#endif /* __DEBUG_PRINT_LOG__ */
}