|
#include "StdAfx.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include "LogFile.h"
//////////////////////////////////////////////////////
COneLogFile::COneLogFile(char* pFileName, bool bOverWrite)
{
m_bOverWrite=bOverWrite;
m_pSavbuf[0]=0;
m_Savbuflen =0;
strcpy(m_logfilename,pFileName);
NewLogFile(bOverWrite);
}
COneLogFile::~COneLogFile()
{
CloseLogFile();
}
void COneLogFile::NewLogFile(bool bOverWrite)
{
if (!bOverWrite) return;
time_t Timestamp;
struct tm *pTime;
time(&Timestamp);
pTime=localtime(&Timestamp);
sprintf(m_pSavbuf,"LOG BEGIN : %02d-%02d-%02d %02d:%02d:%02d\n\n",
pTime->tm_year % 100,pTime->tm_mon+1,pTime->tm_mday,
pTime->tm_hour,pTime->tm_min,pTime->tm_sec);
SaveDataToFile(m_pSavbuf,bOverWrite);
}
void COneLogFile::Appends(const char* pszFormat, ...)
{
OTSTR str;
va_list arglist;
va_start(arglist, pszFormat);
str.Format_(pszFormat, arglist);
va_end(arglist);
Append(str.GetBuffer(0));
}
void COneLogFile::Append(LPCTSTR lpstrLog)
{
int n=strlen(lpstrLog);
if ( m_Savbuflen+n > ONELOG_SAVBUFSIZE )
{
SaveDataToFile(m_pSavbuf);
strcpy(m_pSavbuf,lpstrLog);
m_Savbuflen=n;
}
else
{
strcat(m_pSavbuf,lpstrLog);
m_Savbuflen+=n;
}
}
void COneLogFile::Flush()
{
if (m_Savbuflen>0) SaveDataToFile(m_pSavbuf);
}
void COneLogFile::CloseLogFile()
{
if (m_bOverWrite)
{
time_t Timestamp;
struct tm *pTime;
time(&Timestamp);
pTime=localtime(&Timestamp);
sprintf(m_pSavbuf+m_Savbuflen,"\nLOG END : %02d-%02d-%02d %02d:%02d:%02d\n",
pTime->tm_year % 100,pTime->tm_mon+1,pTime->tm_mday,
pTime->tm_hour,pTime->tm_min,pTime->tm_sec);
}
SaveDataToFile(m_pSavbuf);
}
void COneLogFile::SaveDataToFile(LPCTSTR pbuf,bool bOverWrite)
{
FILE* fp;
if (pbuf != NULL)
{
fp=fopen(m_logfilename,(bOverWrite)? "w":"a"); //
if (fp!=NULL)
{
fwrite(pbuf,strlen(pbuf),1,fp);
fclose(fp); //
}
}
m_pSavbuf[0]=0;
m_Savbuflen =0;
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.