Old fashion design for PC-104
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.

241 lines
6.7 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved.
  4. // Version 1.0 01/01/2000
  5. // Last modification: 08/10/2003
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: LgLogger.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of LgLogger.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "..\LgLib\Version.h"
  16. #include "LgLogger.h"
  17. #include "LgDebug.h"
  18. // initialize static variables
  19. BOOL CLgLogger::m_bInitiated = FALSE;
  20. TCHAR CLgLogger::m_pFileName[MAX_PATH];
  21. E_LOG_DEST CLgLogger::m_eFlags = (E_LOG_DEST)(LOG_DEST_FILE | LOG_DEST_DEBUG);
  22. E_LOG_LEVEL CLgLogger::m_eLevel = LOG_LEVEL_LOW;
  23. HANDLE CLgLogger::m_hModule = NULL;
  24. BOOL CLgLogger::SetHmodule( HANDLE hModule )
  25. {
  26. if( m_bInitiated )
  27. {
  28. Log( LOG_LEVEL_LOW, LOG_TYPE_ERROR, NULL, _T("CLgLogger::SetHmodule"), _T("Handle already set") );
  29. return FALSE;
  30. }
  31. m_hModule = hModule;
  32. return TRUE;
  33. }
  34. BOOL CLgLogger::Init( LPCTSTR pIniFileName )
  35. {
  36. if( m_bInitiated )
  37. return TRUE;
  38. BOOL bHandleError = FALSE;
  39. DWORD dwHandleError = 0;
  40. TCHAR pModuleFileName[MAX_PATH];
  41. if( !pIniFileName )
  42. {
  43. if( !::GetModuleFileName( (HINSTANCE)m_hModule, pModuleFileName, MAX_PATH ) )
  44. {
  45. bHandleError = TRUE;
  46. dwHandleError = ::GetLastError();
  47. if( !::GetModuleFileName( NULL, pModuleFileName, MAX_PATH ) )
  48. return FALSE;
  49. }
  50. if( !::PathRenameExtension( pModuleFileName, _T(".ini") ) )
  51. return FALSE;
  52. pIniFileName = pModuleFileName;
  53. }
  54. // load new value
  55. LoadProfile( pIniFileName );
  56. // store values
  57. SaveProfile( pIniFileName );
  58. m_bInitiated = TRUE;
  59. // log handle error
  60. if( bHandleError )
  61. Log( LOG_LEVEL_LOW, LOG_TYPE_ERROR, NULL, _T("CLgLogger::Init"), _T("GetModuleFileName for 0x%08X failed with 0x%08X"), m_hModule, dwHandleError );
  62. // store initiation parameters
  63. return TRUE;
  64. }
  65. BOOL CLgLogger::Log( E_LOG_LEVEL eLevel, E_LOG_TYPE eType, PVOID pSource, LPCTSTR pSourceDescription, LPCTSTR pFormat, ... )
  66. {
  67. // prepare description
  68. va_list pMarker;
  69. va_start( pMarker, pFormat );
  70. // print log
  71. BOOL bResult = Log_( eLevel, eType, pSource, pSourceDescription, pFormat, pMarker );
  72. va_end( pMarker );
  73. return bResult;
  74. }
  75. BOOL CLgLogger::Log_( E_LOG_LEVEL eLevel, E_LOG_TYPE eType, PVOID pSource, LPCTSTR pSourceDescription, LPCTSTR pFormat, va_list pMarker )
  76. {
  77. // initialize (if nessessary)
  78. if( !Init() )
  79. return FALSE;
  80. // check log level
  81. if( m_eLevel < eLevel )
  82. return TRUE;
  83. // check
  84. if( m_eFlags == LOG_DEST_NONE )
  85. return TRUE;
  86. // prepare description
  87. TCHAR pBuffer[CLgDebug::MAX_PRINT_LENGTH];
  88. INT nBytesPrinted = _vsnprintf( pBuffer, CLgDebug::MAX_PRINT_LENGTH, pFormat, pMarker );
  89. if( nBytesPrinted < 0 )
  90. {
  91. nBytesPrinted = CLgDebug::MAX_PRINT_LENGTH;
  92. pBuffer[ CLgDebug::MAX_PRINT_LENGTH - 1 ] = 0;
  93. }
  94. // prepare data&time
  95. SYSTEMTIME st;
  96. GetLocalTime( &st );
  97. // prepare type string
  98. LPCTSTR ppType[] = { _T("INFO"), _T("WARNING"), _T("ERROR"), _T("UNKNOWN") };
  99. LPCTSTR pType = "";
  100. switch( eType )
  101. {
  102. case LOG_TYPE_INFO: pType = ppType[0]; break;
  103. case LOG_TYPE_WARNING: pType = ppType[1]; break;
  104. case LOG_TYPE_ERROR: pType = ppType[2]; break;
  105. default: pType = ppType[3]; break;
  106. }
  107. // prepare full string
  108. TCHAR pFullBuffer[CLgDebug::MAX_PRINT_LENGTH];
  109. INT nFullBufferPrinted = _sntprintf( pFullBuffer, CLgDebug::MAX_PRINT_LENGTH,
  110. _T("%08X [%02d/%02d/%02d %02d:%02d:%02d:%03d] %s %p %s: %s\r\n"),
  111. ::GetCurrentThreadId(),
  112. st.wDay, st.wMonth, st.wYear,
  113. st.wHour, st.wMinute, st.wSecond, st.wMilliseconds,
  114. pType, pSource, pSourceDescription,
  115. pBuffer );
  116. if( nFullBufferPrinted < 0 )
  117. {
  118. nFullBufferPrinted = CLgDebug::MAX_PRINT_LENGTH;
  119. pFullBuffer[ CLgDebug::MAX_PRINT_LENGTH - 1 ] = 0;
  120. }
  121. // if nessessary to write to file
  122. if( m_eFlags & LOG_DEST_FILE )
  123. {
  124. // open file
  125. FILE *pFile = _tfopen( m_pFileName, "a+" );
  126. if( pFile )
  127. {
  128. // write to file
  129. fwrite( pFullBuffer, nFullBufferPrinted, 1, pFile );
  130. // close file
  131. fclose( pFile );
  132. }
  133. }
  134. // if nessessary to print
  135. if( m_eFlags & LOG_DEST_ERROR )
  136. {
  137. DWORD dwWritten;
  138. HANDLE hOutput = ::GetStdHandle( STD_OUTPUT_HANDLE );
  139. if( hOutput != INVALID_HANDLE_VALUE )
  140. ::WriteFile( hOutput, pFullBuffer, nFullBufferPrinted, &dwWritten, NULL );
  141. }
  142. // if nsesseary to write into debugger
  143. if( m_eFlags & LOG_DEST_DEBUG )
  144. ::OutputDebugString( pFullBuffer );
  145. return TRUE;
  146. }
  147. INT CLgLogger::GetPrivateProfileInt( LPCTSTR pSectionName, LPCTSTR pValueName, INT nDefault, LPCTSTR pFileName )
  148. {
  149. TCHAR pBuffer[MAX_PATH];
  150. TCHAR pDefault[MAX_PATH];
  151. // init default
  152. INT nCount = _snprintf( pDefault, MAX_PATH, _T("%d"), nDefault );
  153. if( nCount < 0 )
  154. *pDefault = '0';
  155. // read value from file
  156. DWORD dwSize = ::GetPrivateProfileString( pSectionName, pValueName, pDefault, pBuffer, MAX_PATH, pFileName );
  157. // paranoia: set terminating zero
  158. pBuffer[dwSize] = 0;
  159. // convert to integer
  160. return atoi( pBuffer );
  161. }
  162. INT CLgLogger::WritePrivateProfileInt( LPCTSTR pSectionName, LPCTSTR pValueName, INT nValue, LPCTSTR pFileName )
  163. {
  164. TCHAR pValue[MAX_PATH];
  165. // init default
  166. INT nCount = _snprintf( pValue, MAX_PATH, _T("%d"), nValue );
  167. if( nCount < 0 )
  168. nCount = MAX_PATH - 1;
  169. pValue[nCount] = 0;
  170. // write value from file
  171. return ::WritePrivateProfileString( pSectionName, pValueName, pValue, pFileName );
  172. }
  173. BOOL CLgLogger::LoadProfile( LPCTSTR pIniFileName )
  174. {
  175. const TCHAR pSectionName[] = _T("LOGGER");
  176. // prepare default output file name
  177. TCHAR pDefaultFileName[MAX_PATH];
  178. _tcscpy( pDefaultFileName, pIniFileName );
  179. ::PathRenameExtension( pDefaultFileName, ".log" );
  180. // load destination parameters
  181. m_eFlags = (E_LOG_DEST)GetPrivateProfileInt( pSectionName, "FLAGS", m_eFlags, pIniFileName );
  182. // load level parameters
  183. m_eLevel = (E_LOG_LEVEL)GetPrivateProfileInt( pSectionName, "LEVEL", m_eLevel, pIniFileName );
  184. // load destination file parameters
  185. GetPrivateProfileString( pSectionName, "FILE", pDefaultFileName, m_pFileName, MAX_PATH, pIniFileName );
  186. return TRUE;
  187. }
  188. BOOL CLgLogger::SaveProfile( LPCTSTR pIniFileName )
  189. {
  190. const TCHAR pSectionName[] = _T("LOGGER");
  191. // load destination parameters
  192. WritePrivateProfileInt( pSectionName, "FLAGS", m_eFlags, pIniFileName );
  193. // load level parameters
  194. WritePrivateProfileInt( pSectionName, "LEVEL", m_eLevel, pIniFileName );
  195. // load destination file parameters
  196. WritePrivateProfileString( pSectionName, "FILE", m_pFileName, pIniFileName );
  197. return TRUE;
  198. }