📄 ylflog.cpp
字号:
// ylfLog.cpp: implementation of the ylfLog class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "ylfLog.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
CString ylfLog::strPath="d:\\";
int ylfLog::nSaveDay=30;
char ylfLog::TBuf[2000];
CTime ylfLog::ctime=CTime::GetCurrentTime();
CTime ylfLog::NextDeleteFileTime=CTime::GetCurrentTime();
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
ylfLog::ylfLog()
{
strPath="d:\\";
nSaveDay=1;
}
ylfLog::~ylfLog()
{
}
bool ylfLog::SetLogPath(const char *filename)
{
strPath=filename;
return true;
}
bool ylfLog::ReadConfig()
{
// ylfProfile pf(ylfCommon::IniFile);
// strPath=pf.GetProfileString("日志文件参数","文件存放目录","d:\\");
// nSaveDay=pf.GetProfileInt("日志文件参数","保存日志天数",30);
return true;
}
void ylfLog::AddLog(LPCTSTR lpszFormat, ...)
{
#ifndef _LOG
return;
#endif
va_list args;
va_start(args, lpszFormat);
int nBuf;
nBuf = _vsntprintf(TBuf, sizeof(TBuf), lpszFormat, args);
// was there an error? was the expanded string too long?
ASSERT(nBuf >= 0);
AddLine(TBuf);
va_end(args);
}
void ylfLog::AddLine(LPCTSTR ctstr)
{
#ifndef _LOG
return;
#endif
CStdioFile savef;
CString strFile;
ctime=CTime::GetCurrentTime();
strFile.Format("%s\\%d-%02d-%02d.log",strPath.GetBuffer(0),ctime.GetYear(),ctime.GetMonth(),ctime.GetDay());
if(!savef.Open(strFile.GetBuffer(0),CFile::modeCreate|CFile::modeWrite|CFile::modeNoTruncate))
{
return;
}
savef.SeekToEnd();
CString str;
str.Format("<%d-%02d-%02d %02d:%02d:%02d>%s\r\n",ctime.GetYear(),ctime.GetMonth(),ctime.GetDay()
,ctime.GetHour(),ctime.GetMinute(),ctime.GetSecond(),ctstr
);
savef.WriteString(str);
savef.Close();
OnTimer();
}
void ylfLog::AddLine(CString str)
{
AddLine(str.GetBuffer(0));
}
void ylfLog::OnTimer()
{
ctime=CTime::GetCurrentTime();
if(ctime>NextDeleteFileTime)
{
NextDeleteFileTime=ctime+CTimeSpan(1,0,0,0);
DelFileHandle();
}
}
void ylfLog::DelFileHandle()
{
CString strFile;
CTime t;
for(int i=nSaveDay;i<nSaveDay+10;i++)
{
t=CTime::GetCurrentTime()-CTimeSpan(i,0,0,0);
strFile.Format("%s\\%d-%02d-%02d.log",strPath.GetBuffer(0),t.GetYear(),t.GetMonth(),t.GetDay());
DeleteFile(strFile);
}
}
void ylfLog::AddBinText(BYTE *buf, int len)
{
#ifndef _LOG
return;
#endif
CString str,strText;
for(int i=0;i<len;i++)
{
if(i%16==0)
{
str="\r\n";
strText+=str;
}
str.Format("%02X ",buf[i]);
strText+=str;
if(i%8==7)
{
strText+=" ";
}
}
AddLine(strText);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -