00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "stdafx.h"
00037 #include "FileLog.h"
00038
00039 #include "ErrorHandlerMacros.h"
00040 #include "OSManager.h"
00041 #include "GenericCriticalSection.h"
00042
00043 #include <time.h>
00044 #include <assert.h>
00045
00046 #ifdef _MEMORY_DEBUG
00047 #define new DEBUG_NEW
00048 #define malloc DEBUG_MALLOC
00049 static char THIS_FILE[] = __FILE__;
00050 #endif
00051
00052 KOMODIA_NAMESPACE_START
00053
00054 CFileLog::CFileLog() : CErrorHandler::CErrorLog(),
00055 m_pFile(NULL),
00056 m_pCSection(NULL),
00057 m_dwMaxLogSize(5000000),
00058 m_bTruncate(FALSE)
00059 {
00060
00061 m_pCSection=COSManager::CreateCriticalSection();
00062 }
00063
00064 CFileLog::~CFileLog()
00065 {
00066 try
00067 {
00068
00069 fclose(m_pFile);
00070
00071
00072 delete m_pCSection;
00073 }
00074 ERROR_UNKNOWN("~CFileLog")
00075 }
00076
00077 BOOL CFileLog::Initialize(const std::string& rFileName)
00078 {
00079
00080 try
00081 {
00082
00083 if (m_pFile)
00084
00085 fclose(m_pFile);
00086
00087
00088 BOOL bTruncate;
00089 bTruncate=FALSE;
00090
00091
00092 if (m_dwMaxLogSize)
00093 {
00094
00095 HANDLE hFile;
00096 hFile=CreateFile(rFileName.c_str(),
00097 GENERIC_READ,
00098 FILE_SHARE_READ,
00099 NULL,
00100 OPEN_EXISTING,
00101 FILE_ATTRIBUTE_NORMAL,
00102 NULL);
00103
00104
00105 if (hFile!=INVALID_HANDLE_VALUE)
00106 {
00107
00108 DWORD dwSize;
00109 dwSize=GetFileSize(hFile,
00110 NULL);
00111
00112
00113 CloseHandle(hFile);
00114
00115
00116 if (dwSize>m_dwMaxLogSize)
00117 bTruncate=TRUE;
00118 }
00119 }
00120
00121
00122 if (!bTruncate)
00123 m_pFile=fopen(rFileName.c_str(),"at");
00124 else
00125 m_pFile=fopen(rFileName.c_str(),"wt");
00126
00127
00128 m_sFileName=rFileName;
00129
00130
00131 if (!m_pFile)
00132 return FALSE;
00133 else
00134 return TRUE;
00135 }
00136 ERROR_UNKNOWN_RETURN("ReportError",FALSE)
00137 }
00138
00139 bool CFileLog::WriteLog(const std::string& rData)
00140 {
00141 try
00142 {
00143 #ifdef _DEBUG
00144 m_aLog.WriteLog(rData);
00145 #endif
00146
00147 CCriticalAutoRelease aRelease(m_pCSection);
00148
00149
00150 fprintf(m_pFile,"%s\n",rData.c_str());
00151
00152
00153 if (GetAutoFlush())
00154 fflush(m_pFile);
00155
00156
00157 if (m_bTruncate &&
00158 m_dwMaxLogSize &&
00159 ftell(m_pFile)>m_dwMaxLogSize)
00160 {
00161
00162
00163 Initialize(m_sFileName);
00164
00165
00166 WriteLog(CErrorHandler::FormatData("CFileLog",
00167 "WriteLog",
00168 "Log truncated!"));
00169 }
00170
00171
00172 return true;
00173 }
00174 ERROR_UNKNOWN_RETURN("WriteLog",false)
00175 }
00176
00177 void CFileLog::WriteError(const std::string& rError)
00178 {
00179 WriteLog(rError);
00180 }
00181
00182 void CFileLog::WriteMessage(const std::string& rMessage)
00183 {
00184 WriteLog(rMessage);
00185 }
00186
00187 void CFileLog::SetMaxLogSize(DWORD dwSize)
00188 {
00189 m_dwMaxLogSize=dwSize;
00190 }
00191
00192 void CFileLog::SetLogTruncate(BOOL bTruncate)
00193 {
00194 m_bTruncate=bTruncate;
00195 }
00196
00197 KOMODIA_NAMESPACE_END