如何写错误log

使用C++记录日志并输出信息


#include <iostream.h>
#include <afx.h>
#include <afxdisp.h>
char g_szLogFileName[20] ="d:\\abc.txt";

//typedef  char* LPCTSTR;
void WriteLog(int nLevel,LPCTSTR lpszFormat,...)
{
 if(nLevel<0)
  return;
 
 CString str;
 switch(nLevel)
 {
 case 1:
  str="More debugging";
  break;
 case 2:
  str="Simple Debugging";
  break;
 case 3:
  str="General Tips";
  break;
 case 4:
  str="Important info";
  break;
 case 5:
  str="Minor error";
  break;
 case 6:
  str="General Error";
  break;
 case 7:
  str="Serious Error";
  break;
 case 8:
  str="Run info";
  break;
 default:
  str="Tip info";
  break;
 }
 
 CString szMsg;
 va_list argList;
 va_start(argList, lpszFormat);
 szMsg.FormatV(lpszFormat, argList);
 va_end(argList);
 
 COleDateTime dt=COleDateTime::GetCurrentTime();
 CString szInfo;
 szInfo.Format("[%s]%s:%s\r\n",dt.Format("%Y-%m-%d %H:%M:%S"),str,szMsg);
 CFile fe;
 try
 {
  if(!fe.Open(g_szLogFileName,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite))
   return;
  fe.SeekToEnd();
  fe.Write(szInfo.GetBuffer(0),szInfo.GetLength());
  fe.Close();
 }
 catch(...)
 {
 }
}

int main()
{
 WriteLog(1,"这是一个非常严重的错误");
 cout<<"ok"<<endl;
//  int j;
//  cin>>j;
 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值