DebugPrint

       在实际的项目开发过程中我们经常需要打印出当前的程序运行流程,需要知道程序运行到什么地方了?以便于我们调试程序,查找出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__
 
#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(LPCWSTR fmt, ... );
 
#ifdef  __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>
 
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;
 
     while (g_bTakeControl)
         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);
 
     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, 
              lpBuffer);
 
     if (NULL != lpBuffer) {
         free(lpBuffer);
         lpBuffer = NULL;
     }
     if (NULL != pFile) {
         fclose(pFile);
         pFile = NULL;
     }
     g_bTakeControl = FALSE;
#endif /* __DEBUG_PRINT_LOG__ */
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值