You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
242 lines
6.5 KiB
242 lines
6.5 KiB
//----------------------------------------------------------------------------
|
|
// Laser Guard Ltd.
|
|
// Copyright © 2000-2002. All Rights Reserved.
|
|
// Version 1.0 01/01/2000
|
|
// Last modification: 08/10/2003
|
|
// PROJECT:
|
|
// SUBSYSTEM:
|
|
// FILE: LgLogger.cpp
|
|
// AUTHOR: Sergey Chizhevskiy
|
|
// OVERVIEW
|
|
// ~~~~~~~~
|
|
// Source file of LgLogger.cpp
|
|
//----------------------------------------------------------------------------
|
|
#include "stdafx.h"
|
|
#include "..\LgLib\Version.h"
|
|
#include "LgLogger.h"
|
|
#include "LgDebug.h"
|
|
|
|
// initialize static variables
|
|
BOOL CLgLogger::m_bInitiated = FALSE;
|
|
TCHAR CLgLogger::m_pFileName[MAX_PATH];
|
|
E_LOG_DEST CLgLogger::m_eFlags = (E_LOG_DEST)(LOG_DEST_FILE | LOG_DEST_DEBUG);
|
|
E_LOG_LEVEL CLgLogger::m_eLevel = LOG_LEVEL_LOW;
|
|
HANDLE CLgLogger::m_hModule = NULL;
|
|
|
|
BOOL CLgLogger::SetHmodule( HANDLE hModule )
|
|
{
|
|
if( m_bInitiated )
|
|
{
|
|
Log( LOG_LEVEL_LOW, LOG_TYPE_ERROR, NULL, _T("CLgLogger::SetHmodule"), _T("Handle already set") );
|
|
return FALSE;
|
|
}
|
|
|
|
m_hModule = hModule;
|
|
return TRUE;
|
|
}
|
|
|
|
BOOL CLgLogger::Init( LPCTSTR pIniFileName )
|
|
{
|
|
if( m_bInitiated )
|
|
return TRUE;
|
|
|
|
BOOL bHandleError = FALSE;
|
|
DWORD dwHandleError = 0;
|
|
|
|
TCHAR pModuleFileName[MAX_PATH];
|
|
|
|
if( !pIniFileName )
|
|
{
|
|
if( !::GetModuleFileName( (HINSTANCE)m_hModule, pModuleFileName, MAX_PATH ) )
|
|
{
|
|
bHandleError = TRUE;
|
|
dwHandleError = ::GetLastError();
|
|
|
|
if( !::GetModuleFileName( NULL, pModuleFileName, MAX_PATH ) )
|
|
return FALSE;
|
|
}
|
|
|
|
if( !::PathRenameExtension( pModuleFileName, _T(".ini") ) )
|
|
return FALSE;
|
|
|
|
pIniFileName = pModuleFileName;
|
|
}
|
|
|
|
// load new value
|
|
LoadProfile( pIniFileName );
|
|
|
|
// store values
|
|
SaveProfile( pIniFileName );
|
|
|
|
m_bInitiated = TRUE;
|
|
|
|
// log handle error
|
|
if( bHandleError )
|
|
Log( LOG_LEVEL_LOW, LOG_TYPE_ERROR, NULL, _T("CLgLogger::Init"), _T("GetModuleFileName for 0x%08X failed with 0x%08X"), m_hModule, dwHandleError );
|
|
|
|
// store initiation parameters
|
|
return TRUE;
|
|
}
|
|
|
|
BOOL CLgLogger::Log( E_LOG_LEVEL eLevel, E_LOG_TYPE eType, PVOID pSource, LPCTSTR pSourceDescription, LPCTSTR pFormat, ... )
|
|
{
|
|
// prepare description
|
|
va_list pMarker;
|
|
va_start( pMarker, pFormat );
|
|
// print log
|
|
BOOL bResult = Log_( eLevel, eType, pSource, pSourceDescription, pFormat, pMarker );
|
|
va_end( pMarker );
|
|
|
|
return bResult;
|
|
}
|
|
|
|
BOOL CLgLogger::Log_( E_LOG_LEVEL eLevel, E_LOG_TYPE eType, PVOID pSource, LPCTSTR pSourceDescription, LPCTSTR pFormat, va_list pMarker )
|
|
{
|
|
// initialize (if nessessary)
|
|
if( !Init() )
|
|
return FALSE;
|
|
|
|
// check log level
|
|
if( m_eLevel < eLevel )
|
|
return TRUE;
|
|
|
|
// check
|
|
if( m_eFlags == LOG_DEST_NONE )
|
|
return TRUE;
|
|
|
|
// prepare description
|
|
TCHAR pBuffer[CLgDebug::MAX_PRINT_LENGTH];
|
|
INT nBytesPrinted = _vsnprintf( pBuffer, CLgDebug::MAX_PRINT_LENGTH, pFormat, pMarker );
|
|
if( nBytesPrinted < 0 )
|
|
{
|
|
nBytesPrinted = CLgDebug::MAX_PRINT_LENGTH;
|
|
pBuffer[ CLgDebug::MAX_PRINT_LENGTH - 1 ] = 0;
|
|
}
|
|
|
|
// prepare data&time
|
|
SYSTEMTIME st;
|
|
GetLocalTime( &st );
|
|
|
|
// prepare type string
|
|
LPCTSTR ppType[] = { _T("INFO"), _T("WARNING"), _T("ERROR"), _T("UNKNOWN") };
|
|
LPCTSTR pType = "";
|
|
switch( eType )
|
|
{
|
|
case LOG_TYPE_INFO: pType = ppType[0]; break;
|
|
case LOG_TYPE_WARNING: pType = ppType[1]; break;
|
|
case LOG_TYPE_ERROR: pType = ppType[2]; break;
|
|
default: pType = ppType[3]; break;
|
|
}
|
|
|
|
// prepare full string
|
|
TCHAR pFullBuffer[CLgDebug::MAX_PRINT_LENGTH];
|
|
INT nFullBufferPrinted = _sntprintf( pFullBuffer, CLgDebug::MAX_PRINT_LENGTH,
|
|
_T("%08X [%02d/%02d/%02d %02d:%02d:%02d:%03d] %s %p %s: %s\r\n"),
|
|
::GetCurrentThreadId(),
|
|
st.wDay, st.wMonth, st.wYear,
|
|
st.wHour, st.wMinute, st.wSecond, st.wMilliseconds,
|
|
pType, pSource, pSourceDescription,
|
|
pBuffer );
|
|
if( nFullBufferPrinted < 0 )
|
|
{
|
|
nFullBufferPrinted = CLgDebug::MAX_PRINT_LENGTH;
|
|
pFullBuffer[ CLgDebug::MAX_PRINT_LENGTH - 1 ] = 0;
|
|
}
|
|
|
|
// if nessessary to write to file
|
|
if( m_eFlags & LOG_DEST_FILE )
|
|
{
|
|
// open file
|
|
FILE *pFile = _tfopen( m_pFileName, _T("a+") );
|
|
if( pFile )
|
|
{
|
|
// write to file
|
|
fwrite( pFullBuffer, nFullBufferPrinted, 1, pFile );
|
|
|
|
// close file
|
|
fclose( pFile );
|
|
}
|
|
}
|
|
|
|
// if nessessary to print
|
|
if( m_eFlags & LOG_DEST_ERROR )
|
|
{
|
|
DWORD dwWritten;
|
|
HANDLE hOutput = ::GetStdHandle( STD_OUTPUT_HANDLE );
|
|
if( hOutput != INVALID_HANDLE_VALUE )
|
|
::WriteFile( hOutput, pFullBuffer, nFullBufferPrinted, &dwWritten, NULL );
|
|
}
|
|
|
|
// if nsesseary to write into debugger
|
|
if( m_eFlags & LOG_DEST_DEBUG )
|
|
::OutputDebugString( pFullBuffer );
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
INT CLgLogger::GetPrivateProfileInt( LPCTSTR pSectionName, LPCTSTR pValueName, INT nDefault, LPCTSTR pFileName )
|
|
{
|
|
TCHAR pBuffer[MAX_PATH];
|
|
TCHAR pDefault[MAX_PATH];
|
|
|
|
// init default
|
|
INT nCount = _snprintf( pDefault, MAX_PATH, _T("%d"), nDefault );
|
|
if( nCount < 0 )
|
|
*pDefault = '0';
|
|
|
|
// read value from file
|
|
DWORD dwSize = ::GetPrivateProfileString( pSectionName, pValueName, pDefault, pBuffer, MAX_PATH, pFileName );
|
|
|
|
// paranoia: set terminating zero
|
|
pBuffer[dwSize] = 0;
|
|
|
|
// convert to integer
|
|
return atoi( pBuffer );
|
|
}
|
|
|
|
INT CLgLogger::WritePrivateProfileInt( LPCTSTR pSectionName, LPCTSTR pValueName, INT nValue, LPCTSTR pFileName )
|
|
{
|
|
TCHAR pValue[MAX_PATH];
|
|
|
|
// init default
|
|
INT nCount = _snprintf( pValue, MAX_PATH, _T("%d"), nValue );
|
|
if( nCount < 0 )
|
|
nCount = MAX_PATH - 1;
|
|
pValue[nCount] = 0;
|
|
|
|
// write value from file
|
|
return ::WritePrivateProfileString( pSectionName, pValueName, pValue, pFileName );
|
|
}
|
|
|
|
BOOL CLgLogger::LoadProfile( LPCTSTR pIniFileName )
|
|
{
|
|
const TCHAR pSectionName[] = _T("LOGGER");
|
|
|
|
// prepare default output file name
|
|
TCHAR pDefaultFileName[MAX_PATH];
|
|
_tcscpy( pDefaultFileName, pIniFileName );
|
|
::PathRenameExtension( pDefaultFileName, _T(".log") );
|
|
|
|
// load destination parameters
|
|
m_eFlags = (E_LOG_DEST)GetPrivateProfileInt( pSectionName, _T("FLAGS"), m_eFlags, pIniFileName );
|
|
// load level parameters
|
|
m_eLevel = (E_LOG_LEVEL)GetPrivateProfileInt( pSectionName, _T("LEVEL"), m_eLevel, pIniFileName );
|
|
// load destination file parameters
|
|
GetPrivateProfileString( pSectionName, _T("FILE"), pDefaultFileName, m_pFileName, MAX_PATH, pIniFileName );
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
BOOL CLgLogger::SaveProfile( LPCTSTR pIniFileName )
|
|
{
|
|
const TCHAR pSectionName[] = _T("LOGGER");
|
|
|
|
// load destination parameters
|
|
WritePrivateProfileInt( pSectionName, _T("FLAGS"), m_eFlags, pIniFileName );
|
|
// load level parameters
|
|
WritePrivateProfileInt( pSectionName, _T("LEVEL"), m_eLevel, pIniFileName );
|
|
// load destination file parameters
|
|
WritePrivateProfileString( pSectionName, _T("FILE"), m_pFileName, pIniFileName );
|
|
|
|
return TRUE;
|
|
}
|