2021-04-14 05:34:15 +00:00
|
|
|
#include "Log.h"
|
2021-04-12 09:41:28 +00:00
|
|
|
#include "path.h"
|
2016-01-12 18:51:53 +00:00
|
|
|
#include "Platform.h"
|
2008-09-18 03:15:49 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
CLog::CLog (void ) :
|
|
|
|
m_FlushOnWrite(false),
|
|
|
|
m_TruncateFileLog(true),
|
|
|
|
m_MaxFileSize(MAX_FILE_SIZE),
|
2017-01-03 05:21:35 +00:00
|
|
|
m_FileChangeSize(0),
|
|
|
|
m_FileSize(0)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-03-17 21:19:42 +00:00
|
|
|
CLog::~CLog (void)
|
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2015-11-06 11:37:21 +00:00
|
|
|
bool CLog::Open( const char * FileName, LOG_OPEN_MODE mode /* = Log_New */)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2021-04-12 11:35:39 +00:00
|
|
|
if (FileName == nullptr)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CPath File(FileName);
|
|
|
|
if (m_hLogFile.IsOpen())
|
|
|
|
{
|
|
|
|
m_hLogFile.Close();
|
|
|
|
}
|
|
|
|
|
2015-11-06 11:37:21 +00:00
|
|
|
uint32_t nOpenFlags = CFile::modeReadWrite | CFile::modeCreate;
|
2008-09-18 03:15:49 +00:00
|
|
|
if (mode == Log_Append) { nOpenFlags |= CFile::modeNoTruncate; }
|
|
|
|
|
|
|
|
if (!m_hLogFile.Open(File, nOpenFlags))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2015-11-06 11:37:21 +00:00
|
|
|
m_FileName = (const char *)File;
|
2008-09-18 03:15:49 +00:00
|
|
|
m_hLogFile.Seek(0,mode == Log_Append ? CFile::end : CFile::begin);
|
2022-07-11 10:00:25 +00:00
|
|
|
m_FileSize = mode == Log_Append ? (uint32_t)m_hLogFile.GetLength() : 0;
|
2008-09-18 03:15:49 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CLog::Close ( void )
|
|
|
|
{
|
|
|
|
if (m_hLogFile.IsOpen())
|
|
|
|
{
|
|
|
|
m_hLogFile.Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-06 11:37:21 +00:00
|
|
|
void CLog::LogF(const char * Message, ...)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start( ap, Message );
|
|
|
|
LogArgs(Message,ap);
|
|
|
|
va_end( ap );
|
|
|
|
}
|
|
|
|
|
2015-11-06 11:37:21 +00:00
|
|
|
void CLog::LogArgs(const char * Message, va_list & args )
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
|
|
|
if (!m_hLogFile.IsOpen()) { return; }
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2016-01-12 18:51:53 +00:00
|
|
|
size_t nlen = _vscprintf(Message, args) + 1;
|
|
|
|
char * Msg = (char *)alloca(nlen * sizeof(char));
|
|
|
|
Msg[nlen - 1] = 0;
|
2021-04-12 11:35:39 +00:00
|
|
|
if (Msg != nullptr)
|
2016-01-12 18:51:53 +00:00
|
|
|
{
|
|
|
|
vsprintf(Msg, Message, args);
|
|
|
|
Log(Msg);
|
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
Log("Invalid message format");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-06 11:37:21 +00:00
|
|
|
void CLog::Log( const char * Message )
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
|
|
|
if (!m_hLogFile.IsOpen()) { return; }
|
2018-11-18 01:06:02 +00:00
|
|
|
uint32_t message_len = (uint32_t)strlen(Message);
|
2017-01-03 05:21:35 +00:00
|
|
|
m_hLogFile.Write(Message, message_len);
|
2008-09-18 03:15:49 +00:00
|
|
|
if (m_FlushOnWrite)
|
|
|
|
{
|
|
|
|
m_hLogFile.Flush();
|
|
|
|
}
|
|
|
|
|
2017-01-03 05:21:35 +00:00
|
|
|
m_FileSize += message_len;
|
|
|
|
if (m_TruncateFileLog && m_FileSize > m_MaxFileSize)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2021-03-16 22:51:11 +00:00
|
|
|
// Check file size
|
2022-07-11 10:00:25 +00:00
|
|
|
m_FileSize = (uint32_t)m_hLogFile.GetLength();
|
2021-03-16 22:51:11 +00:00
|
|
|
// If larger then maximum size then
|
2017-01-03 05:21:35 +00:00
|
|
|
if (m_FileSize > m_MaxFileSize)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
|
|
|
if (!m_FlushOnWrite)
|
|
|
|
{
|
|
|
|
m_hLogFile.Flush();
|
2022-07-11 10:00:25 +00:00
|
|
|
m_FileSize = (uint32_t)m_hLogFile.GetLength();
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 10:00:25 +00:00
|
|
|
uint32_t end = (uint32_t)m_hLogFile.SeekToEnd();
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2021-03-16 22:51:11 +00:00
|
|
|
// Move to reduce size
|
2008-09-18 03:15:49 +00:00
|
|
|
m_hLogFile.Seek((end - m_MaxFileSize) + m_FileChangeSize,CFile::begin);
|
|
|
|
|
|
|
|
// Find next end of line
|
2015-11-11 06:51:19 +00:00
|
|
|
uint32_t NextEnter = 0, dwRead = 0;
|
2015-11-06 11:37:21 +00:00
|
|
|
do
|
|
|
|
{
|
2016-01-12 18:51:53 +00:00
|
|
|
uint8_t Data[300];
|
2015-11-11 06:51:19 +00:00
|
|
|
dwRead = m_hLogFile.Read(Data,sizeof(Data));
|
2008-09-18 03:15:49 +00:00
|
|
|
if (dwRead == 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < sizeof(Data); i++)
|
|
|
|
{
|
|
|
|
if (Data[i] == '\n')
|
|
|
|
{
|
|
|
|
NextEnter += (i + 1);
|
|
|
|
dwRead = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NextEnter += dwRead;
|
|
|
|
} while(dwRead != 0);
|
|
|
|
|
2021-03-16 22:51:11 +00:00
|
|
|
// Copy content of log to the new file
|
2015-11-11 06:51:19 +00:00
|
|
|
uint32_t ReadPos = (end - m_MaxFileSize) + m_FileChangeSize + NextEnter;
|
|
|
|
uint32_t SizeToRead, WritePos = 0;
|
2015-11-06 11:37:21 +00:00
|
|
|
do
|
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
enum { fIS_MvSize = 0x5000 };
|
|
|
|
unsigned char Data[fIS_MvSize + 1];
|
|
|
|
|
|
|
|
SizeToRead = end - ReadPos;
|
|
|
|
if (SizeToRead > fIS_MvSize) { SizeToRead = fIS_MvSize; }
|
|
|
|
|
|
|
|
m_hLogFile.Seek(ReadPos,CFile::begin);
|
|
|
|
|
2015-11-11 06:51:19 +00:00
|
|
|
dwRead = m_hLogFile.Read(Data,SizeToRead);
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
m_hLogFile.Seek(WritePos,CFile::begin);
|
|
|
|
|
|
|
|
if (!m_hLogFile.Write(Data,dwRead))
|
|
|
|
{
|
2016-01-12 18:51:53 +00:00
|
|
|
//BreakPoint(__FILE__,__LINE__);
|
2008-09-18 03:15:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReadPos += dwRead;
|
|
|
|
WritePos += dwRead;
|
|
|
|
} while (SizeToRead > 0);
|
|
|
|
|
2021-03-16 22:51:11 +00:00
|
|
|
// Clean up
|
2010-05-14 23:28:15 +00:00
|
|
|
m_hLogFile.SetEndOfFile();
|
|
|
|
m_hLogFile.Flush();
|
2022-07-11 10:00:25 +00:00
|
|
|
m_FileSize = (uint32_t)m_hLogFile.GetLength();
|
2008-09-18 03:15:49 +00:00
|
|
|
} // end if
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CLog::Empty(void)
|
|
|
|
{
|
|
|
|
if (!m_hLogFile.IsOpen()) { return true; }
|
|
|
|
if (m_hLogFile.GetLength() == 0)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2015-01-31 19:27:27 +00:00
|
|
|
}
|