[Common] Get logs to track file size

This commit is contained in:
zilmar 2017-01-03 16:21:35 +11:00
parent bfb64dc458
commit 45c6d0d157
3 changed files with 15 additions and 7 deletions

View File

@ -222,7 +222,9 @@ bool CFile::Flush()
return ::FlushFileBuffers(m_hFile) != 0; return ::FlushFileBuffers(m_hFile) != 0;
#else #else
return fflush((FILE *)m_hFile) == 0; fflush((FILE *)m_hFile);
fsync(fileno((FILE *)m_hFile));
return true;
#endif #endif
} }

View File

@ -7,7 +7,8 @@ CLog::CLog (void ) :
m_FlushOnWrite(false), m_FlushOnWrite(false),
m_TruncateFileLog(true), m_TruncateFileLog(true),
m_MaxFileSize(MAX_FILE_SIZE), m_MaxFileSize(MAX_FILE_SIZE),
m_FileChangeSize(0) m_FileChangeSize(0),
m_FileSize(0)
{ {
} }
@ -37,6 +38,7 @@ bool CLog::Open( const char * FileName, LOG_OPEN_MODE mode /* = Log_New */)
} }
m_FileName = (const char *)File; m_FileName = (const char *)File;
m_hLogFile.Seek(0,mode == Log_Append ? CFile::end : CFile::begin); m_hLogFile.Seek(0,mode == Log_Append ? CFile::end : CFile::begin);
m_FileSize = mode == Log_Append ? m_hLogFile.GetLength() : 0;
return true; return true;
} }
@ -80,23 +82,25 @@ 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(char)); uint32_t message_len = strlen(Message);
m_hLogFile.Write(Message, message_len);
if (m_FlushOnWrite) if (m_FlushOnWrite)
{ {
m_hLogFile.Flush(); m_hLogFile.Flush();
} }
if (m_TruncateFileLog) m_FileSize += message_len;
if (m_TruncateFileLog && m_FileSize > m_MaxFileSize)
{ {
// check file size // check file size
uint32_t FileSize = m_hLogFile.GetLength(); m_FileSize = m_hLogFile.GetLength();
// if larger then max size then // if larger then max size then
if (FileSize > m_MaxFileSize) if (m_FileSize > m_MaxFileSize)
{ {
if (!m_FlushOnWrite) if (!m_FlushOnWrite)
{ {
m_hLogFile.Flush(); m_hLogFile.Flush();
FileSize = m_hLogFile.GetLength(); m_FileSize = m_hLogFile.GetLength();
} }
uint32_t end = m_hLogFile.SeekToEnd(); uint32_t end = m_hLogFile.SeekToEnd();
@ -160,6 +164,7 @@ void CLog::Log( const char * Message )
//clean up //clean up
m_hLogFile.SetEndOfFile(); m_hLogFile.SetEndOfFile();
m_hLogFile.Flush(); m_hLogFile.Flush();
m_FileSize = m_hLogFile.GetLength();
} // end if } // end if
} }
} }

View File

@ -43,6 +43,7 @@ private:
bool m_FlushOnWrite; bool m_FlushOnWrite;
std::string m_FileName; std::string m_FileName;
bool m_TruncateFileLog; bool m_TruncateFileLog;
uint32_t m_FileSize;
uint32_t m_MaxFileSize; uint32_t m_MaxFileSize;
uint32_t m_FileChangeSize; uint32_t m_FileChangeSize;
}; };