Leaked source code of windows server 2003
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.

157 lines
6.3 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // File Name
  4. // EventLogger.h
  5. //
  6. //
  7. // Portions Copyright (c) 2002 Microsoft Corporation. All rights reserved.
  8. //
  9. #ifndef _CEVENTLOGGER_H
  10. #define _CEVENTLOGGER_H
  11. #pragma warning (disable : 4100)
  12. #pragma warning (disable : 4127)
  13. #pragma warning (disable : 4201)
  14. #pragma warning (disable : 4237)
  15. #pragma warning (disable : 4245)
  16. #pragma warning (disable : 4514)
  17. #ifndef STRICT
  18. #define STRICT
  19. #endif //!STRICT
  20. #include <WINDOWS.H>
  21. #include <TCHAR.H>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #define REGVAL_LOGGING_LEVEL _T("Logging")
  25. #define COUNTOF(a) (sizeof(a)/sizeof(a[0]))
  26. #define STR_BYTE_SIZE(a) ((_tcslen (a) + 1)*sizeof(TCHAR))
  27. // Level of logging in the event registry
  28. typedef enum LOGLEVEL
  29. {
  30. LOGGING_LEVEL_0 = 0, // None: No event log traces at all.
  31. LOGGING_LEVEL_1, // Minimal: No audit or information traces. Only critical and warning errors
  32. LOGGING_LEVEL_2, // Normal: Security audit traces plus previous level
  33. LOGGING_LEVEL_3 // Verbose: All transactions are traced out plus previous level
  34. } LOGLEVEL;
  35. typedef enum LOGTYPE
  36. {
  37. LOGTYPE_FORCE_ERROR, // Logged in any Level regardless.
  38. LOGTYPE_FORCE_WARNING, // Logged in any Level regardless.
  39. LOGTYPE_FORCE_INFORMATION, // Logged in any Level regardless.
  40. LOGTYPE_ERR_CRITICAL, // Logged in level 1 or above
  41. LOGTYPE_ERR_WARNING, // Logged in level 1 or above
  42. LOGTYPE_AUDIT_SECURITY_ACCESS, // Logged in level 2 or above
  43. LOGTYPE_AUDIT_SECURITY_DENIED, // Logged in level 2 or above
  44. LOGTYPE_INFORMATION, // Logged in level 3 or above
  45. LOGTYPE_VERBOSE, // Logged in level 4
  46. LOGTYPE_VERBOSE_ERROR, // Logged in level 4
  47. LOGTYPE_VERBOSE_WARNING // Logged in level 4
  48. } LOGTYPE;
  49. class CEventLogger
  50. {
  51. public :
  52. DWORD WINAPI InitEventLog
  53. (LPTSTR szEventSource,
  54. WORD wEventsCategory,
  55. LPTSTR szRegKey = NULL);
  56. DWORD WINAPI InitEventLog
  57. (LPTSTR szEventSource,
  58. WORD wEventsCategory,
  59. LOGLEVEL NewLoggingLevel)
  60. {
  61. LPTSTR szKey = NULL;
  62. DWORD dwError = InitEventLog (szEventSource, wEventsCategory, szKey);
  63. if (!dwError)
  64. {
  65. SetLoggingLevel (NewLoggingLevel);
  66. }
  67. return dwError;
  68. }
  69. void WINAPI SetLoggingLevel
  70. (DWORD dwNewLoggingLevel)
  71. {
  72. if (dwNewLoggingLevel > (DWORD)LOGGING_LEVEL_3)
  73. {
  74. dwNewLoggingLevel = (DWORD)LOGGING_LEVEL_3;
  75. }
  76. SetLoggingLevel ((LOGLEVEL)dwNewLoggingLevel);
  77. }
  78. void WINAPI SetLoggingLevel
  79. (LOGLEVEL NewLoggingLevel);
  80. DWORD WINAPI GetLoggingLevel
  81. ()
  82. {
  83. EnterCriticalSection (&m_csObj);
  84. DWORD dwLevel = (DWORD)m_LoggingLevel;
  85. LeaveCriticalSection (&m_csObj);
  86. return dwLevel;
  87. }
  88. void WINAPI LogEvent
  89. (LOGTYPE Type,
  90. DWORD dwEventID,
  91. DWORD dwData,
  92. LPCTSTR rgszMsgs[],
  93. WORD wCount,
  94. DWORD cbData = 0,
  95. LPVOID pvData = NULL);
  96. void WINAPI LogEvent
  97. (LOGTYPE Type,
  98. DWORD dwEventID,
  99. DWORD dwError = 0)
  100. { LogEvent (Type, dwEventID, dwError, NULL, 0); }
  101. void WINAPI LogEvent
  102. (LOGTYPE Type,
  103. DWORD dwEventID,
  104. int iError )
  105. { LogEvent (Type, dwEventID, iError, NULL, 0); }
  106. void WINAPI LogEvent
  107. (LOGTYPE Type,
  108. DWORD dwEventID,
  109. LPCTSTR rgszMsgs[],
  110. WORD wCount)
  111. { LogEvent (Type, dwEventID, 0, rgszMsgs, wCount); };
  112. void WINAPI LogEvent
  113. (LOGTYPE Type,
  114. DWORD dwEventID,
  115. HRESULT hResult)
  116. { TCHAR szBuffer[32];
  117. LPCTSTR rgText[] = { szBuffer };
  118. _stprintf (szBuffer, _T("%#08x"), hResult);
  119. LogEvent (Type, dwEventID, 0, rgText, 1); };
  120. void WINAPI LogEvent
  121. (LOGTYPE Type,
  122. DWORD dwEventID,
  123. LPCTSTR szString,
  124. HRESULT hResult)
  125. { TCHAR szBuffer[32];
  126. LPCTSTR rgText[] = { szString, szBuffer };
  127. _stprintf (szBuffer, _T("%#08x"), hResult);
  128. LogEvent (Type, dwEventID, 0, rgText, 2); };
  129. public :
  130. CEventLogger();
  131. CEventLogger(LPTSTR szEventSource,
  132. WORD wEventsCategory);
  133. ~CEventLogger();
  134. private :
  135. CRITICAL_SECTION m_csObj;
  136. WORD m_wServiceCategory;
  137. LOGLEVEL m_LoggingLevel;
  138. HANDLE m_hEventLog;
  139. HANDLE m_hLogMutex;
  140. HMODULE m_hMsgSource;
  141. TCHAR m_szCat[32];
  142. BOOL m_fLogEvent;
  143. };
  144. #endif // _CEVENTLOGGER_H