/*********************************************************************
Copyright (C) 1999 Smaller Animals Software, Inc.
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
http://www.smalleranimals.com
[email protected]
**********************************************************************/
#include "stdafx.h"
#include "SAStatusLog.h"
/////////////////////////////////////////////////////////////////////////////
CSAStatusLog::CSAStatusLog()
{
m_bEnable = FALSE;
m_bPrintTime = FALSE;
m_bQuotaSize = FALSE;
m_csFileName = "";
m_csAppName = "";
// we'll make sure only one call uses the critical stuff at a time
InitializeCriticalSection(&m_crit);
}
/////////////////////////////////////////////////////////////////////////////
CSAStatusLog::~CSAStatusLog()
{
DeleteCriticalSection(&m_crit);
}
/////////////////////////////////////////////////////////////////////////////
void CSAStatusLog::Init(LPCTSTR pOutputFilename)
{
m_bEnable = TRUE;
// get application path and name
TCHAR buf[MAX_PATH+1];
DWORD res = GetModuleFileName(AfxGetInstanceHandle(), buf, MAX_PATH);
m_csAppName = GetBaseName(buf);
m_csFileName = pOutputFilename;
}
/////////////////////////////////////////////////////////////////////////////
void CSAStatusLog::Enable(BOOL bEnable)
{
m_bEnable = bEnable;
}
/////////////////////////////////////////////////////////////////////////////
BOOL CSAStatusLog::StatusOut(SAStatusLogType logType, LPCTSTR fmt, ...)
{
#ifndef DEBUG
if(logType == LOG_TYPE_DEBUG)
return TRUE;
#endif
if (m_csFileName.IsEmpty())
return FALSE;
if (!m_bEnable)
return TRUE;
if (!AfxIsValidString(fmt, -1))
return FALSE;
EnterCriticalSection(&m_crit);
// parse that string format
try
{
va_list argptr;
va_start(argptr, fmt);
_vsntprintf_s(m_tBuf, TBUF_SIZE, TBUF_SIZE, fmt, argptr);
va_end(argptr);
}
catch (...)
{
m_tBuf[0] = 0;
}
BOOL bOK = FALSE;
// output
FILE *fp = NULL;
_tfopen_s(&fp, m_csFileName, _T("ab"));
if (fp)
{
fseek(fp,0,SEEK_END);
__int64 nEnd = _ftelli64( fp);
if(sizeof(TCHAR) == 2 && nEnd == 0)
fwrite("\xff\xfe", 1, 2, fp);
CString strOut;
if (m_bPrintAppName)
{
strOut.Format(_T("%s "), m_csAppName);
fwrite((LPCTSTR)strOut, 1, strOut.GetLength()*sizeof(TCHAR), fp);
}
if (m_bPrintTime)
{
CTime ct ;
ct = CTime::GetCurrentTime();
strOut = ct.Format(_T("%m/%d/%Y %H:%M:%S "));
fwrite((LPCTSTR)strOut, 1, strOut.GetLength()*sizeof(TCHAR), fp);
}
if(logType == LOG_TYPE_DEBUG)
strOut = _T("Debug : ");
else if(logType == LOG_TYPE_DEBUG)
strOut = _T("Run : ");
else if(logType == LOG_TYPE_DEBUG)
strOut = _T("Error : ");
else
strOut = _T("Invalid log type : ");
fwrite((LPCTSTR)strOut, 1, strOut.GetLength()*sizeof(TCHAR), fp);
strOut.Format(_T("%s\r\n"), m_tBuf);
fwrite((LPCTSTR)strOut, 1, strOut.GetLength()*sizeof(TCHAR), fp);
bool bRemoveForSize = false;
if(m_bQuotaSize)
{
fseek(fp,0,SEEK_END);
nEnd = _ftelli64( fp);
if(nEnd > m_i64QuotaSize)
bRemoveForSize = true;
}
fclose(fp);
if(bRemoveForSize)
_tremove(m_csFileName);
bOK = TRUE;
}
LeaveCriticalSection(&m_crit);
return bOK;
}
/////////////////////////////////////////////////////////////////////////////
CString CSAStatusLog::GetBaseName(const CString &path)
{
CString out = path;
int iSlashPos = path.ReverseFind('\\');
if (iSlashPos !=-1)
{
out = out.Mid(iSlashPos+1);
}
else
{
iSlashPos = path.ReverseFind('/');
if (iSlashPos !=-1)
{
out = out.Mid(iSlashPos+1);
}
}
int iDotPos = out.ReverseFind('.');
if (iDotPos>0)
{
out = out.Left(iDotPos);
}
return out;
}
/////////////////////////////////////////////////////////////////////////////
CString CSAStatusLog::GetBaseDir(const CString & path)
{
CString out = _T("");
int iSlashPos = path.ReverseFind('\\');
if (iSlashPos !=-1)
{
out = path.Left(iSlashPos);
}
else
{
iSlashPos = path.ReverseFind('/');
if (iSlashPos !=-1)
{
out = path.Left(iSlashPos);
}
}
return out;
}
/////////////////////////////////////////////////////////////////////////////
// Enable Quota limit
void CSAStatusLog::EnableQuotaSize(BOOL bEnable)
{
m_bQuotaSize = bEnable;
}
/////////////////////////////////////////////////////////////////////////////
// Set Quota limit
void CSAStatusLog::SetQuotaSize(__int64 i64QuotaSize)
{
m_i64QuotaSize = i64QuotaSize;
}