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 {
00059
00060 m_pCSection=COSManager::CreateCriticalSection();
00061 }
00062
00063 CFileLog::~CFileLog()
00064 {
00065
00066 fclose(m_pFile);
00067
00068
00069 delete m_pCSection;
00070 }
00071
00072 BOOL CFileLog::Initialize(const std::string& rFileName)
00073 {
00074
00075 try
00076 {
00077
00078 if (m_pFile)
00079
00080 fclose(m_pFile);
00081
00082
00083 BOOL bTruncate;
00084 bTruncate=FALSE;
00085
00086
00087 if (m_dwMaxLogSize)
00088 {
00089
00090 HANDLE hFile;
00091 hFile=CreateFile(rFileName.c_str(),
00092 GENERIC_READ,
00093 FILE_SHARE_READ,
00094 NULL,
00095 OPEN_EXISTING,
00096 FILE_ATTRIBUTE_NORMAL,
00097 NULL);
00098
00099
00100 if (hFile!=INVALID_HANDLE_VALUE)
00101 {
00102
00103 DWORD dwSize;
00104 dwSize=GetFileSize(hFile,
00105 NULL);
00106
00107
00108 CloseHandle(hFile);
00109
00110
00111 if (dwSize>m_dwMaxLogSize)
00112 bTruncate=FALSE;
00113 }
00114 }
00115
00116
00117 if (!bTruncate)
00118 m_pFile=fopen(rFileName.c_str(),"at");
00119 else
00120 m_pFile=fopen(rFileName.c_str(),"wt");
00121
00122
00123 if (!m_pFile)
00124 return FALSE;
00125 else
00126 return TRUE;
00127 }
00128 ERROR_UNKNOWN_RETURN("ReportError",FALSE)
00129 }
00130
00131 void CFileLog::WriteLog(const std::string& rClass,
00132 const std::string& rMethod,
00133 const std::string& rMessage,
00134 CErrorHandler::LogPriority aPriority)
00135 {
00136 try
00137 {
00138 #ifdef _DEBUG
00139 m_aLog.WriteLog(rClass,
00140 rMethod,
00141 rMessage,
00142 aPriority);
00143 #endif
00144
00145 std::string sPrefix;
00146
00147
00148 switch (aPriority)
00149 {
00150 case CErrorHandler::lpMessage:
00151 sPrefix="***MESSAGE*** ";
00152 break;
00153 case CErrorHandler::lpCritical:
00154 sPrefix="***CRITICAL*** ";
00155 break;
00156 case CErrorHandler::lpError:
00157 sPrefix="***ERROR*** ";
00158 break;
00159 }
00160
00161
00162 CCriticalAutoRelease aRelease(m_pCSection);
00163
00164
00165 if (!sPrefix.empty())
00166 fprintf(m_pFile,"%s",sPrefix.c_str());
00167
00168
00169 char tmpbuf[128];
00170
00171
00172 _strdate(tmpbuf);
00173 fprintf(m_pFile,"%s ",tmpbuf);
00174
00175 _strtime(tmpbuf);
00176 fprintf(m_pFile,"%s: ",tmpbuf);
00177
00178
00179 fprintf(m_pFile,"%s, %s, %s\n",rClass.c_str(),
00180 rMethod.c_str(),
00181 rMessage.c_str());
00182
00183
00184 if (aPriority==CErrorHandler::lpError ||
00185 GetAutoFlush())
00186 fflush(m_pFile);
00187 }
00188 ERROR_UNKNOWN("WriteLog")
00189 }
00190
00191 void CFileLog::ReportError(const std::string& rClass,
00192 const std::string& rMethod,
00193 const std::string& rMessage)
00194 {
00195
00196 WriteLog(rClass,
00197 rMethod,
00198 rMessage,
00199 CErrorHandler::lpError);
00200 }
00201
00202 void CFileLog::WriteMessage(const std::string& rClass,
00203 const std::string& rMethod,
00204 const std::string& rMessage,
00205 CErrorHandler::LogPriority aPriority)
00206 {
00207
00208 WriteLog(rClass,
00209 rMethod,
00210 rMessage,
00211 aPriority);
00212 }
00213
00214 void CFileLog::SetMaxLogSize(DWORD dwSize)
00215 {
00216 m_dwMaxLogSize=dwSize;
00217 }
00218
00219 KOMODIA_NAMESPACE_END