eventlog.cpp
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 5k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. //+-------------------------------------------------------------------------
  2. //
  3. //  Microsoft Windows
  4. //
  5. //  Copyright (C) Microsoft Corporation, 1997 - 1999
  6. //
  7. //  File:       eventlog.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. #include "pch.h"
  11. #pragma hdrstop
  12. #include "eventlog.h"
  13. CEventLog::~CEventLog(
  14.     void
  15.     )
  16. {
  17.     Close();
  18. }
  19. //
  20. // Register the specified event source.
  21. // Note that the registry entries must already exist.
  22. // HKLMSystemCurrentControlSetServicesEventLogApplication<pszEventSource>
  23. //     Requires values "EventMessageFile" and "TypesSupported".
  24. //        
  25. HRESULT
  26. CEventLog::Initialize(
  27.     LPCTSTR pszEventSource
  28.     )
  29. {
  30.     if (NULL != m_hLog)
  31.     {
  32.         return S_FALSE;
  33.     }
  34.     HRESULT hr = NOERROR;
  35.     m_hLog = RegisterEventSource(NULL, pszEventSource);
  36.     if (NULL == m_hLog)
  37.     {
  38.         hr = HRESULT_FROM_WIN32(GetLastError());
  39.     }
  40.     return hr;
  41. }
  42. //
  43. // Deregister the event source.
  44. //
  45. void
  46. CEventLog::Close(
  47.     void
  48.     )
  49. {
  50.     if (NULL != m_hLog)
  51.     {
  52.         DeregisterEventSource(m_hLog);
  53.         m_hLog = NULL;
  54.     }
  55. }
  56. //
  57. // Report an event.  No replaceable parameters explicitly specified.
  58. // If msg string contains replaceable parameters, use Push() to 
  59. // build list of replacement strings.
  60. //
  61. HRESULT
  62. CEventLog::ReportEvent(
  63.     WORD wType,
  64.     WORD wCategory,
  65.     DWORD dwEventID,
  66.     PSID lpUserSid,    // [optional]
  67.     LPVOID pvRawData,  // [optional]
  68.     DWORD cbRawData    // [optional]
  69.     )
  70. {
  71.     if (NULL == m_hLog)
  72.         return E_FAIL;
  73.     BOOL bResult = FALSE;
  74.     HRESULT hr = NOERROR;
  75.     if (!::ReportEvent(m_hLog,
  76.                        wType,
  77.                        wCategory,
  78.                        dwEventID,
  79.                        lpUserSid,
  80.                        (WORD)m_rgstrText.Count(),
  81.                        cbRawData,
  82.                        m_rgstrText,
  83.                        pvRawData))
  84.     {
  85.         //
  86.         // Special-case ERROR_IO_PENDING.  ::ReportEvent will fail with
  87.         // this error code even when it succeeds.  Don't know exactly why
  88.         // but it does.  Treat this as success so we don't get unnecessary
  89.         // debugger output.
  90.         //
  91.         DWORD dwError = GetLastError();
  92.         if (ERROR_IO_PENDING != dwError)
  93.         {
  94.             hr = HRESULT_FROM_WIN32(dwError);
  95.         }
  96.     }
  97.     m_rgstrText.Clear();
  98.     return hr;
  99. }
  100.             
  101. //
  102. // Push an HRESULT value onto the stack of replacment strings.
  103. //
  104. void 
  105. CEventLog::Push(
  106.     HRESULT hr, 
  107.     eFmt fmt
  108.     )
  109. {
  110.     if (eFmtSysErr == fmt)
  111.     {
  112.         LPTSTR pszBuffer = NULL;
  113.         int cchLoaded = ::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
  114.                                         FORMAT_MESSAGE_ALLOCATE_BUFFER,
  115.                                         NULL,
  116.                                         HRESULT_CODE(hr),
  117.                                         0,
  118.                                         (LPTSTR)&pszBuffer,
  119.                                         1,
  120.                                         NULL);
  121.         if (NULL != pszBuffer)
  122.         {
  123.             if (0 != cchLoaded)
  124.             {
  125.                 m_rgstrText.Append(pszBuffer);
  126.             }
  127.             LocalFree(pszBuffer);
  128.         }
  129.     }
  130.     else
  131.     {
  132.         TCHAR szNumber[40];
  133.         wsprintf(szNumber, eFmtDec == fmt ? TEXT("%d") : TEXT("0x%08X"), hr);
  134.         m_rgstrText.Append(szNumber);
  135.     }     
  136. }
  137. //
  138. // Push a string onto the stack of replacement strings.
  139. //
  140. void 
  141. CEventLog::Push(
  142.     LPCTSTR psz
  143.     )
  144. {
  145.     m_rgstrText.Append(psz);
  146. }
  147. CEventLog::CStrArray::CStrArray(
  148.     void
  149.     ) : m_cEntries(0)
  150. {
  151.     ZeroMemory(m_rgpsz, sizeof(m_rgpsz));
  152. }
  153. LPCTSTR
  154. CEventLog::CStrArray::Get(
  155.     int iEntry
  156.     ) const
  157. {
  158.     TraceAssert(iEntry < m_cEntries);
  159.     if (iEntry < m_cEntries)
  160.         return m_rgpsz[iEntry];
  161.     return NULL;
  162. }
  163. bool
  164. CEventLog::CStrArray::Append(
  165.     LPCTSTR psz
  166.     )
  167. {
  168.     TraceAssert(m_cEntries < (ARRAYSIZE(m_rgpsz) - 1));
  169.     if (m_cEntries < (ARRAYSIZE(m_rgpsz) - 1))
  170.     {
  171.         LPTSTR pszNew = new TCHAR[lstrlen(psz) + 1];
  172.         if (NULL != pszNew)
  173.         {
  174.             lstrcpy(pszNew, psz);
  175.             m_rgpsz[m_cEntries++] = pszNew;
  176.             return true;
  177.         }
  178.     }
  179.     return false;
  180. }
  181. void
  182. CEventLog::CStrArray::Destroy(
  183.     void
  184.     )
  185. {
  186.     for (int i = 0; i < ARRAYSIZE(m_rgpsz); i++)
  187.     {
  188.         delete[] m_rgpsz[i];
  189.         m_rgpsz[i] = NULL;
  190.     }
  191.     m_cEntries = 0;
  192. }