// LogRecord.cpp: implementation of the LogRecord class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
//#include "FtpClient.h"
#include "LogRecord.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
LogRecord::LogRecord()
{
::CoInitialize(NULL);
try{
// To create a stance for pXMLDoc
HRESULT hr=pXMLDoc.CreateInstance(__uuidof(FreeThreadedDOMDocument));
}catch(_com_error& e)
{
char pszTemp[_MAX_PATH];
sprintf(pszTemp,"Exception occured in LogRecord::LogRecord:%s",(TCHAR*)e.Description());
#ifdef _DEBUG
TRACE(pszTemp);
#endif
}catch(...)
{
char pszTemp[_MAX_PATH];
sprintf(pszTemp,"Unknown exception occured in LogRecord::~LogRecord.");
#ifdef _DEBUG
TRACE(pszTemp);
#endif
}
}
LogRecord::~LogRecord()
{
if(pXMLDoc)
{
try{
// Added by Phips Xue(pxue@gwcom.com) Ju.29 2001@SHA OFFICE
// To release the pXMLDoc object
pXMLDoc.Release();
}catch(_com_error& e)
{
char pszTemp[_MAX_PATH];
sprintf(pszTemp,"Exception occured in LogRecord::~LogRecord:%s",(TCHAR*)e.Description());
#ifdef _DEBUG
TRACE(pszTemp);
#endif
}catch(...)
{
char pszTemp[_MAX_PATH];
sprintf(pszTemp,"Unknown exception occured in LogRecord::~LogRecord.");
#ifdef _DEBUG
TRACE(pszTemp);
#endif
}
}
CoUninitialize();
}
LogRecord::LogRecord(CString sFile)
{
// ---Jul.4 2001---
::CoInitialize(NULL);
try{
// To create a stance for pXMLDoc
HRESULT hr=pXMLDoc.CreateInstance(__uuidof(FreeThreadedDOMDocument));
}catch(_com_error& e)
{
char pszTemp[_MAX_PATH];
sprintf(pszTemp,"Exception occured in LogRecord::LogRecord:%s",(TCHAR*)e.Description());
#ifdef _DEBUG
TRACE(pszTemp);
#endif
}catch(...)
{
char pszTemp[_MAX_PATH];
sprintf(pszTemp,"Unknown exception occured in LogRecord::~LogRecord.");
#ifdef _DEBUG
TRACE(pszTemp);
#endif
}
int i;
for (i=0;i<MAX_TYPE_NUM;i++)
{
ModeInterval[i] = 0;
IntervalCount[i] = 0;
FileCount[i] = 0;
ResFileNum[i] = 0;
LengthCount[i] = 0;
FirstRun[i] = TRUE;
switch (i) {
case HOUR:
ModeValue[i].createmode = "hour";
ModeValue[i].modevalue = HOUR;
break;
case DAY:
ModeValue[i].createmode = "day";
ModeValue[i].modevalue = DAY;
break;
case WEEK:
ModeValue[i].createmode = "week";
ModeValue[i].modevalue = WEEK;
break;
case MONTH:
ModeValue[i].createmode = "month";
ModeValue[i].modevalue = MONTH;
break;
case LENGTH:
ModeValue[i].createmode = "length";
ModeValue[i].modevalue = LENGTH;
break;
}
}
//Read config file
ReadConfig(sFile);
//Create log file directory
if (!FileFind.FindFile(strFilePath,0))
CreateDirectory(strFilePath,NULL);
}
void LogRecord::ParseString(CString *pStr,CString str)
{
int position=0,counts=0;
//find the first ',' separator position in str
position = str.Find(',');
while (position > 0)
{
pStr[counts] = str.Mid(0,position);
str.Delete(0,position+1);
str.TrimLeft(' ');
counts++;
position = str.Find(',');
}
pStr[counts] = str;
return;
}
void LogRecord::ParseInt(int *pInt,CString str)
{
int position=0,counts=0;
//find the first ',' separator position in str
position = str.Find(',');
while (position > 0)
{
pInt[counts] = atoi(str.Mid(0,position));
str.Delete(0,position+1);
str.TrimLeft(' ');
counts++;
position = str.Find(',');
}
pInt[counts] = atoi(str);
return;
}
CString LogRecord::GetAppPath()
{
char PathName[255];
GetModuleFileName(NULL, PathName,sizeof( PathName ) );
char *a,*b;
a = b = PathName;
while(b)
{
if(b) a=b;
b=strchr(a+1,'\\');
}
*a='\0';
return PathName;
}
void LogRecord::ReadConfig(CString sFile)
{
// char pszWorkPath[_MAX_PATH];
// char pszFileName[_MAX_PATH];//Full filename string
char FirstName[16];
char SecondName[16];
char FilePath[50];
char FileType[100];
char CreateMode[100];
char IntervalNum[100];
char ResNum[100];
CString strFileType,strCreateMode,strInterval,strResNum;
//Get work path
// _getcwd(pszWorkPath, _MAX_PATH);
//Get full INI file name (with path)
// sprintf(pszFileName, "%s\\%s", pszWorkPath, sFile);
CString pszFileName = GetAppPath();
pszFileName+="\\"+sFile;
//Get all initialization parameters
GetPrivateProfileString("LogConfig", "Computer Name", "", FirstName, 16, pszFileName);
GetPrivateProfileString("LogConfig", "Application Name", "", SecondName, 16, pszFileName);
GetPrivateProfileString("LogConfig", "Log File Path", "", FilePath, 50, pszFileName);
GetPrivateProfileString("LogConfig", "Log File Type ", "", FileType, 100, pszFileName);
GetPrivateProfileString("LogConfig", "File Create Mode ", "", CreateMode, 100, pszFileName);
GetPrivateProfileString("LogConfig", "Interval Number ", "", IntervalNum, 100, pszFileName);
GetPrivateProfileString("LogConfig", "Reserve File Number ", "", ResNum, 100, pszFileName);
strFirstName = (CString)FirstName;
strSecondName = (CString)SecondName;
strFilePath = (CString)FilePath;
//Parse file create mode,interval numver for each file type
ParseString(FileTypes,(CString)FileType);
ParseString(LogCreateMode,(CString)CreateMode);
ParseInt(ModeInterval,(CString)IntervalNum);
ParseInt(ResFileNum,(CString)ResNum);
}
//adjust exist old file,get new file name
int LogRecord::AdjustOldFile(int ResNum,CString PreName,
CString FilePath,CString &NewFile,CString &LastDate)
{
BOOL bFindNext;
int position=0,iFileSeq=0,FileNum=0;
CString sFileSeq,LastFileName,sTemp,NewFileName;
bFindNext = FileFind.FindFile(PreName+"*.log",0);
while (bFindNext)
{
bFindNext=FileFind.FindNextFile();
LastFileName = sTemp = FileFind.GetFileName();
position = sTemp.ReverseFind('_');
if (position != -1)
{
sTemp.Delete(0,position+1);
iFileSeq = atoi((char *)(LPCSTR)sTemp);
if (iFileSeq)
{
FileNum++;
if (FileNum != iFileSeq)
{
_itoa(FileNum,(char *)(LPCSTR)sFileSeq,10);
if (FileNum < 10)
NewFileName = PreName+"00"+(CString)sFileSeq+".log";
else if (FileNum < 100)
NewFileName = PreName+"0"+(CString)sFileSeq+".log";
else
NewFileName = PreName+(CString)sFileSeq+".log";
LastFileName = FilePath + "\\" + LastFileName;
LogFile.Rename(LastFileName,NewFileName);
//HistoryFileSeq = HistoryFileSeq + (CString)sFileSeq + ",";
}
else
NewFileName = FilePath + "\\" + LastFileName;
}
}
if (FileNum == MAX_FILE_NUM)
break;
if ((FileNum == ResNum) && (ResNum > 0))
break;
}
FileFind.Close();
NewFile = NewFileName;
position = LastFileName.ReverseFind('_');
LastDate = LastFileName.Mid(position-8,8);
return FileNum;
}
void LogRecord::PacketSendLog(SENDSTRUCT SendPack, CString &SendLog)
{
try{
// Added by Phips Xue(pxue@gwcom.com) Jun.29 2001
// To create a stance for pXMLDoc
IXMLDOMNodePtr pXMLNode=NULL,pXMLNodeLog=NULL;
CString strTemp;
// <Log>
pXMLNodeLog=pXMLDoc->createNode((long)1,_bstr_t("Log"),_bstr_t(""));
//<Tm>
pXMLNode=pXMLDoc->createNode((long)1,_bstr_t("Tm"),_bstr_t(""));
strTemp=CTime::GetCurrentTime().Format("%H:%M:%S");
pXMLNode->text=_bstr_t(strTemp.AllocSysString(),false);
pXMLNodeLog->appendChild(pXMLNode);
//<Um>
pXMLNode=pXMLDoc->createNode((long)1,_bstr_t("Um"),_bstr_t(""));
strTemp=SendPack.UserMobile;
pXMLNode->text=_bstr_t(strTemp.AllocSysString(),false);
pXMLNodeLog->appendChild(pXMLNode);
//<Up>
pX