This commit is contained in:
zilmar 2015-11-07 12:48:24 +11:00
commit 45a8598290
6 changed files with 39 additions and 33 deletions

View File

@ -140,15 +140,19 @@ bool CFile::Flush()
return ::FlushFileBuffers(m_hFile) != 0; return ::FlushFileBuffers(m_hFile) != 0;
} }
bool CFile::Write(const void* lpBuf, uint32_t nCount) bool CFile::Write(const void* lpBuf, size_t nCount)
{ {
if (nCount == 0) if (nCount == 0)
{ {
return true; // avoid Win32 "null-write" option return true; // avoid Win32 "null-write" option
} }
if (nCount > ULONG_MAX)
{
nCount = ULONG_MAX; /* Or should we loop WriteFile() every 2 GB? */
}
ULONG nWritten = 0; DWORD nWritten = 0;
if (!::WriteFile(m_hFile, lpBuf, nCount, &nWritten, NULL)) if (!::WriteFile(m_hFile, lpBuf, (DWORD)nCount, &nWritten, NULL))
{ {
return false; return false;
} }
@ -161,19 +165,23 @@ bool CFile::Write(const void* lpBuf, uint32_t nCount)
return true; return true;
} }
uint32_t CFile::Read(void* lpBuf, uint32_t nCount) size_t CFile::Read(void* lpBuf, size_t nCount)
{ {
if (nCount == 0) if (nCount == 0)
{ {
return 0; // avoid Win32 "null-read" return 0; // avoid Win32 "null-read"
} }
if (nCount > ULONG_MAX)
{
nCount = ULONG_MAX; /* Or should we loop ReadFile() every 2 GB? */
}
DWORD dwRead = 0; DWORD dwRead = 0;
if (!::ReadFile(m_hFile, lpBuf, nCount, &dwRead, NULL)) if (!::ReadFile(m_hFile, lpBuf, (DWORD)nCount, &dwRead, NULL))
{ {
return 0; return 0;
} }
return (uint32_t)dwRead; return (dwRead);
} }
long CFile::Seek(long lOff, SeekPosition nFrom) long CFile::Seek(long lOff, SeekPosition nFrom)

View File

@ -40,8 +40,8 @@ public:
virtual bool SetLength(uint32_t dwNewLen) = 0; virtual bool SetLength(uint32_t dwNewLen) = 0;
virtual uint32_t GetLength() const = 0; virtual uint32_t GetLength() const = 0;
virtual uint32_t Read(void* lpBuf, uint32_t nCount) = 0; virtual size_t Read(void* lpBuf, size_t nCount) = 0;
virtual bool Write(const void* lpBuf, uint32_t nCount) = 0; virtual bool Write(const void* lpBuf, size_t nCount) = 0;
virtual bool Flush() = 0; virtual bool Flush() = 0;
virtual bool Close() = 0; virtual bool Close() = 0;
@ -78,8 +78,8 @@ public:
virtual bool SetLength(uint32_t dwNewLen); virtual bool SetLength(uint32_t dwNewLen);
virtual uint32_t GetLength() const; virtual uint32_t GetLength() const;
virtual uint32_t Read(void* lpBuf, uint32_t nCount); virtual size_t Read(void* lpBuf, size_t nCount);
virtual bool Write(const void* lpBuf, uint32_t nCount); virtual bool Write(const void* lpBuf, size_t nCount);
virtual bool Flush(); virtual bool Flush();
virtual bool Close(); virtual bool Close();

View File

@ -120,7 +120,7 @@ void CLog::LogArgs(const char * Message, va_list & args )
void CLog::Log( const char * Message ) void CLog::Log( const char * Message )
{ {
if (!m_hLogFile.IsOpen()) { return; } if (!m_hLogFile.IsOpen()) { return; }
m_hLogFile.Write(Message,(uint32_t)strlen(Message)*sizeof(TCHAR)); m_hLogFile.Write(Message, strlen(Message)*sizeof(TCHAR));
if (m_FlushOnWrite) if (m_FlushOnWrite)
{ {
m_hLogFile.Flush(); m_hLogFile.Flush();
@ -145,13 +145,13 @@ void CLog::Log( const char * Message )
m_hLogFile.Seek((end - m_MaxFileSize) + m_FileChangeSize,CFile::begin); m_hLogFile.Seek((end - m_MaxFileSize) + m_FileChangeSize,CFile::begin);
// Find next end of line // Find next end of line
uint32_t NextEnter = 0, dwRead = 0; size_t NextEnter = 0, dwRead = 0;
do do
{ {
BYTE Data[300]; BYTE Data[300];
uint32_t dwRead; size_t dwRead;
dwRead = m_hLogFile.Read(Data,sizeof(Data)); dwRead = m_hLogFile.Read(Data, sizeof(Data));
if (dwRead == 0) if (dwRead == 0)
{ {
break; break;
@ -170,8 +170,9 @@ void CLog::Log( const char * Message )
} while(dwRead != 0); } while(dwRead != 0);
// copy content of log to the new file // copy content of log to the new file
uint32_t ReadPos = (end - m_MaxFileSize) + m_FileChangeSize + NextEnter; size_t ReadPos = (end - m_MaxFileSize) + m_FileChangeSize + NextEnter;
uint32_t SizeToRead, WritePos = 0; uint32_t WritePos = 0;
size_t SizeToRead;
do do
{ {
enum { fIS_MvSize = 0x5000 }; enum { fIS_MvSize = 0x5000 };
@ -182,8 +183,8 @@ void CLog::Log( const char * Message )
m_hLogFile.Seek(ReadPos,CFile::begin); m_hLogFile.Seek(ReadPos,CFile::begin);
uint32_t dwRead; size_t dwRead;
dwRead = m_hLogFile.Read(Data,SizeToRead); dwRead = m_hLogFile.Read(Data, SizeToRead);
m_hLogFile.Seek(WritePos,CFile::begin); m_hLogFile.Seek(WritePos,CFile::begin);

View File

@ -15,8 +15,8 @@ class CLog
bool m_FlushOnWrite; bool m_FlushOnWrite;
stdstr m_FileName; stdstr m_FileName;
bool m_TruncateFileLog; bool m_TruncateFileLog;
uint32_t m_MaxFileSize; size_t m_MaxFileSize;
uint32_t m_FileChangeSize; size_t m_FileChangeSize;
public: public:
CLog ( void ); CLog ( void );
@ -29,10 +29,10 @@ public:
bool Empty ( void ); bool Empty ( void );
void Close ( void ); void Close ( void );
inline void SetMaxFileSize ( uint32_t Size ) inline void SetMaxFileSize(size_t Size)
{ {
m_MaxFileSize = Size; m_MaxFileSize = Size;
m_FileChangeSize = (uint32_t)(Size * 0.1); m_FileChangeSize = (size_t)(Size * 0.1);
} }
inline void SetTruncateFile( bool Truncate ) { m_TruncateFileLog = Truncate; } inline void SetTruncateFile( bool Truncate ) { m_TruncateFileLog = Truncate; }
inline void SetFlush ( bool Always ) { m_FlushOnWrite = Always; } inline void SetFlush ( bool Always ) { m_FlushOnWrite = Always; }

View File

@ -191,7 +191,7 @@ m_FlushFile(FlushFile)
m_hLogFile.Open(FileName, Log_Append); m_hLogFile.Open(FileName, Log_Append);
} }
CTraceFileLog::CTraceFileLog(LPCTSTR FileName, bool FlushFile, LOG_OPEN_MODE eMode, uint32_t dwMaxFileSize) : CTraceFileLog::CTraceFileLog(LPCTSTR FileName, bool FlushFile, LOG_OPEN_MODE eMode, size_t dwMaxFileSize) :
m_FlushFile(FlushFile) m_FlushFile(FlushFile)
{ {
enum { MB = 1024 * 1024 }; enum { MB = 1024 * 1024 };
@ -199,14 +199,11 @@ m_FlushFile(FlushFile)
m_hLogFile.SetFlush(false); m_hLogFile.SetFlush(false);
m_hLogFile.SetTruncateFile(true); m_hLogFile.SetTruncateFile(true);
if (dwMaxFileSize < 2048 && dwMaxFileSize > 2) if (dwMaxFileSize < 3 || dwMaxFileSize > 2047)
{ { /* Clamp file size to 5 MB if it exceeds 2047 or falls short of 3. */
m_hLogFile.SetMaxFileSize(dwMaxFileSize * MB); dwMaxFileSize = 5;
}
else
{
m_hLogFile.SetMaxFileSize(5 * MB);
} }
m_hLogFile.SetMaxFileSize(dwMaxFileSize * MB);
m_hLogFile.Open(FileName, eMode); m_hLogFile.Open(FileName, eMode);
} }

View File

@ -26,7 +26,7 @@ class CTraceFileLog : public CTraceModule
public: public:
CTraceFileLog (const char * FileName, bool FlushFile = true); CTraceFileLog (const char * FileName, bool FlushFile = true);
CTraceFileLog (const char * FileName, bool FlushFile, LOG_OPEN_MODE eMode, uint32_t dwMaxFileSize = 5); CTraceFileLog(const char * FileName, bool FlushFile, LOG_OPEN_MODE eMode, size_t dwMaxFileSize = 5);
virtual ~CTraceFileLog (); virtual ~CTraceFileLog ();
void Write ( const char * Message, bool EndOfLine ); void Write ( const char * Message, bool EndOfLine );