Source code of Windows XP (NT5)
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.

214 lines
6.8 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. // 100ns units between periodic event logs. this can't be larger then
  52. // 0xffffffff
  53. #define LOGEVENT_PERIOD (DWORD) (3600000000) // 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.HighPart ||
  87. liDifference.LowPart > LOGEVENT_PERIOD);
  88. }
  89. void UpdateLogTime() {
  90. GetSystemTimeAsFileTime(&m_timeLastLog);
  91. }
  92. private:
  93. CEventLogHashKey m_key;
  94. FILETIME m_timeLastLog;
  95. };
  96. class CEventLogWrapper {
  97. public:
  98. CEventLogWrapper() {
  99. m_hEventLog = NULL;
  100. }
  101. //
  102. // Register your event source in the registry.
  103. //
  104. // Parameters:
  105. // szEventSource - the name of the eventsource
  106. // szMessageFile - the full path to the DLL which contains the
  107. // eventlog strings
  108. // fApplication - The eventsource is an application, not a system
  109. // component
  110. //
  111. static
  112. HRESULT AddEventSourceToRegistry(char *szEventSource,
  113. char *szMessageFile,
  114. BOOL fApplication = FALSE);
  115. //
  116. // Unregister your event source in the registry.
  117. //
  118. // Parameters:
  119. // szEventSource - the name of the eventsource
  120. // fApplication - The eventsource is an application, not a system
  121. // component
  122. //
  123. static
  124. HRESULT RemoveEventSourceFromRegistry(char *szEventSource,
  125. BOOL fApplication = FALSE);
  126. //
  127. // Initialize the event logging library.
  128. //
  129. // Parameters:
  130. // szEventSource - the name of the eventsource
  131. //
  132. HRESULT Initialize(char *szEventSource);
  133. //
  134. // Write an event to the event log
  135. //
  136. // Parameters:
  137. // idMessage - the eventlog ID
  138. // cSubstrings - count of strings in rgszSubstrings
  139. // rgszSubstrings - substrings for the eventlog text
  140. // wType - eventlog error type. Should be EVENTLOG_WARNING_TYPE,
  141. // EVENTLOG_INFORMATION_TYPE or EVENTLOG_ERROR_TYPE.
  142. // errCode - Win32 error code to log (or 0)
  143. // iDebugLevel - debug level of the event. 1 = highest priority,
  144. // 2^16 = lowest priority. normally anything above 2^15 isn't
  145. // logged.
  146. // szKey - a key which is used along with idMessage to uniquely
  147. // identify this eventlog. It is used to control the options.
  148. // dwOptions - options for logging this event.
  149. // Optional Parameters:
  150. // iMessageString - call FormatMessage on errCode and save
  151. // the string into rgszSubstrings[iMessageString].
  152. // HMODULE hModule - module with extra error codes for
  153. // FormatMessage.
  154. //
  155. HRESULT LogEvent(DWORD idMessage,
  156. WORD cSubstrings,
  157. LPCSTR *rgszSubstrings,
  158. WORD wType,
  159. DWORD errCode,
  160. WORD iDebugLevel,
  161. LPCSTR szKey,
  162. DWORD dwOptions,
  163. DWORD iMessageString = 0xffffffff,
  164. HMODULE hModule = NULL);
  165. //
  166. // Reset any history about events using this message and key,
  167. // so that the next LogEvent with one-time or periodic logging
  168. // will cause the event to be logged.
  169. //
  170. // Parameters:
  171. // idMessage - the eventlog ID
  172. // szKey - a key which is used along with idMessage to uniquely
  173. // identify this eventlog.
  174. //
  175. HRESULT ResetEvent(DWORD idMessage,
  176. LPCSTR szKey);
  177. ~CEventLogWrapper();
  178. private:
  179. // the handle returned from RegisterEventSource
  180. HANDLE m_hEventLog;
  181. // this hash table is used to remember which keys we have
  182. // used to support PERIODIC and ONETIME options
  183. TFHashEx<CEventLogHashItem, CEventLogHashKey *, CEventLogHashKey *> m_hash;
  184. };
  185. #endif