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.

213 lines
7.0 KiB

  1. /*
  2. * An event log wrapper to simplify event logging for DLLs and to add
  3. * a few extra features.
  4. */
  5. #ifndef __EVENTWRAP_H__
  6. #define __EVENTWRAP_H__
  7. #include <dbgtrace.h>
  8. #include <fhashex.h>
  9. #define LOGEVENT_DEBUGLEVEL_HIGH 1
  10. #define LOGEVENT_DEBUGLEVEL_MEDIUM 2^15
  11. #define LOGEVENT_DEBUGLEVEL_LOW 2^16
  12. #define LOGEVENT_FLAG_ALWAYS 0x00000001
  13. #define LOGEVENT_FLAG_ONETIME 0x00000002
  14. #define LOGEVENT_FLAG_PERIODIC 0x00000003
  15. // we use the lower 8 bits for various logging modes, and reserve the
  16. // other 24 for flags
  17. #define LOGEVENT_FLAG_MODEMASK 0x000000ff
  18. //
  19. // This object is the key for the eventlog hash table
  20. //
  21. class CEventLogHashKey {
  22. public:
  23. char *m_szKey;
  24. DWORD m_idMessage;
  25. BOOL m_fAllocKey;
  26. CEventLogHashKey() {
  27. m_fAllocKey = FALSE;
  28. m_szKey = NULL;
  29. m_idMessage = 0;
  30. }
  31. HRESULT Init(const char *szKey, DWORD idMessage) {
  32. m_idMessage = idMessage;
  33. m_szKey = new char[strlen(szKey) + 1];
  34. if (m_szKey) {
  35. m_fAllocKey = TRUE;
  36. strcpy(m_szKey, szKey);
  37. } else {
  38. return E_OUTOFMEMORY;
  39. }
  40. return S_OK;
  41. }
  42. ~CEventLogHashKey() {
  43. if (m_fAllocKey && m_szKey) {
  44. delete[] m_szKey;
  45. m_szKey = NULL;
  46. m_fAllocKey = FALSE;
  47. }
  48. m_idMessage = 0;
  49. }
  50. };
  51. // Time to wait between periodic event logs in 100ns units. Note that this
  52. // is larger than 32 bits (it is 8'61c46800).
  53. const __int64 LOGEVENT_PERIOD = 36000000000; // 60 minutes
  54. //
  55. // For each unique idMessage/szKey event that is logged we insert one
  56. // of these objects into a hash table. This allows us to support the
  57. // LOGEVENT_FLAG_ONETIME and LOGEVENT_FLAG_PERIODIC flags
  58. //
  59. class CEventLogHashItem {
  60. public:
  61. CEventLogHashItem *m_pNext;
  62. CEventLogHashItem() {
  63. m_pNext = NULL;
  64. ZeroMemory(&m_timeLastLog, sizeof(FILETIME));
  65. UpdateLogTime();
  66. }
  67. CEventLogHashKey *GetKey() {
  68. return &(m_key);
  69. }
  70. int MatchKey(CEventLogHashKey *pOtherKey) {
  71. return (m_key.m_idMessage == pOtherKey->m_idMessage &&
  72. strcmp(m_key.m_szKey, pOtherKey->m_szKey) == 0);
  73. }
  74. HRESULT InitializeKey(const char *szKey, DWORD idMessage) {
  75. return m_key.Init(szKey, idMessage);
  76. }
  77. BOOL PeriodicLogOkay() {
  78. FILETIME timeCurrent;
  79. GetSystemTimeAsFileTime(&timeCurrent);
  80. LARGE_INTEGER liCurrent =
  81. { timeCurrent.dwLowDateTime, timeCurrent.dwHighDateTime };
  82. LARGE_INTEGER liLastLog =
  83. { m_timeLastLog.dwLowDateTime, m_timeLastLog.dwHighDateTime };
  84. LARGE_INTEGER liDifference;
  85. liDifference.QuadPart = liCurrent.QuadPart - liLastLog.QuadPart;
  86. return (liDifference.QuadPart > LOGEVENT_PERIOD);
  87. }
  88. void UpdateLogTime() {
  89. GetSystemTimeAsFileTime(&m_timeLastLog);
  90. }
  91. private:
  92. CEventLogHashKey m_key;
  93. FILETIME m_timeLastLog;
  94. };
  95. class CEventLogWrapper {
  96. public:
  97. CEventLogWrapper() {
  98. m_hEventLog = NULL;
  99. }
  100. //
  101. // Register your event source in the registry.
  102. //
  103. // Parameters:
  104. // szEventSource - the name of the eventsource
  105. // szMessageFile - the full path to the DLL which contains the
  106. // eventlog strings
  107. // fApplication - The eventsource is an application, not a system
  108. // component
  109. //
  110. static
  111. HRESULT AddEventSourceToRegistry(char *szEventSource,
  112. char *szMessageFile,
  113. BOOL fApplication = FALSE);
  114. //
  115. // Unregister your event source in the registry.
  116. //
  117. // Parameters:
  118. // szEventSource - the name of the eventsource
  119. // fApplication - The eventsource is an application, not a system
  120. // component
  121. //
  122. static
  123. HRESULT RemoveEventSourceFromRegistry(char *szEventSource,
  124. BOOL fApplication = FALSE);
  125. //
  126. // Initialize the event logging library.
  127. //
  128. // Parameters:
  129. // szEventSource - the name of the eventsource
  130. //
  131. HRESULT Initialize(char *szEventSource);
  132. //
  133. // Write an event to the event log
  134. //
  135. // Parameters:
  136. // idMessage - the eventlog ID
  137. // cSubstrings - count of strings in rgszSubstrings
  138. // rgszSubstrings - substrings for the eventlog text
  139. // wType - eventlog error type. Should be EVENTLOG_WARNING_TYPE,
  140. // EVENTLOG_INFORMATION_TYPE or EVENTLOG_ERROR_TYPE.
  141. // errCode - Win32 error code to log (or 0)
  142. // iDebugLevel - debug level of the event. 1 = highest priority,
  143. // 2^16 = lowest priority. normally anything above 2^15 isn't
  144. // logged.
  145. // szKey - a key which is used along with idMessage to uniquely
  146. // identify this eventlog. It is used to control the options.
  147. // dwOptions - options for logging this event.
  148. // Optional Parameters:
  149. // iMessageString - call FormatMessage on errCode and save
  150. // the string into rgszSubstrings[iMessageString].
  151. // HMODULE hModule - module with extra error codes for
  152. // FormatMessage.
  153. //
  154. HRESULT LogEvent(DWORD idMessage,
  155. WORD cSubstrings,
  156. LPCSTR *rgszSubstrings,
  157. WORD wType,
  158. DWORD errCode,
  159. WORD iDebugLevel,
  160. LPCSTR szKey,
  161. DWORD dwOptions,
  162. DWORD iMessageString = 0xffffffff,
  163. HMODULE hModule = NULL);
  164. //
  165. // Reset any history about events using this message and key,
  166. // so that the next LogEvent with one-time or periodic logging
  167. // will cause the event to be logged.
  168. //
  169. // Parameters:
  170. // idMessage - the eventlog ID
  171. // szKey - a key which is used along with idMessage to uniquely
  172. // identify this eventlog.
  173. //
  174. HRESULT ResetEvent(DWORD idMessage,
  175. LPCSTR szKey);
  176. ~CEventLogWrapper();
  177. private:
  178. // the handle returned from RegisterEventSource
  179. HANDLE m_hEventLog;
  180. // this hash table is used to remember which keys we have
  181. // used to support PERIODIC and ONETIME options
  182. TFHashEx<CEventLogHashItem, CEventLogHashKey *, CEventLogHashKey *> m_hash;
  183. };
  184. #endif