Chinaunix首页 | 论坛 | 博客
  • 博客访问: 627777
  • 博文数量: 603
  • 博客积分: 4000
  • 博客等级: 上校
  • 技术积分: 4940
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-17 11:04
文章分类

全部博文(603)

文章存档

2011年(1)

2008年(602)

我的朋友

分类:

2008-09-17 11:04:35

#include "stdafx.h"
#include "log.h"

CLog::CLog()  //构造函数,设置日志文件的默认路径
{
 ::InitializeCriticalSection(&m_crit);   //初始化临界区
}


CLog::~CLog()
{
 ::DeleteCriticalSection(&m_crit);    //释放里临界区
}

 
/*================================================================
* 函数名:    InitLog
* 参数:      LPCTST lpszLogPath
* 功能描述:   初始化日志(设置日志文件的路径)
* 返回值:    void
* 作 者:     程红秀 2005年01月06日
================================================================*/
void CLog::InitLog(LPCTSTR lpszLogPath)  

 m_strLogPath=lpszLogPath;
}

void CLog::Add(const char* fmt, ...)
{
 if (m_strLogPath.IsEmpty())
  return ;
 
 if (!AfxIsValidString(fmt, -1))
  return ;
/*-----------------------进入临界区(写文件)------------------------------------*/ 
 ::EnterCriticalSection(&m_crit);  
 try     
 {
  va_list argptr;          //分析字符串的格式
  va_start(argptr, fmt);
  _vsnprintf(m_tBuf, BUFSIZE, fmt, argptr);
  va_end(argptr);
 }
 catch (...)
 {
  m_tBuf[0] = 0;
 }
 
 FILE *fp = fopen(m_strLogPath, "a"); //以添加的方式输出到文件
 if (fp)
 {
  fprintf(fp,"%s:  ", AfxGetApp()->m_pszExeName);  //加入当前程序名
  
  CTime ct ;                          //加入当前时间
  ct = CTime::GetCurrentTime();
  fprintf(fp,"%s : ",ct.Format("%m/%d/%Y %H:%M:%S"));
  fprintf(fp, "%s\n", m_tBuf);  
  fclose(fp);  
 } 
 ::LeaveCriticalSection(&m_crit); 
/*-------------------退出临界区----------------------------------------*/ 
}


 


--------------------next---------------------

阅读(776) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~