EventLog.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #ifndef _TWINKLE_EVENT_LOG_HELPER_H_
  2. #define _TWINKLE_EVENT_LOG_HELPER_H_
  3. #pragma once
  4. #include <windows.h>
  5. #include <stdio.h>
  6. #include <strsafe.h>
  7. #include <fstream>
  8. #include <sstream>
  9. #include <string>
  10. #include <iomanip>
  11. #include "SimpleString.h"
  12. #define EXT_EVTLOG_NAME ".evtlog"
  13. #define _U 0x01 /* upper */
  14. #define _L 0x02 /* lower */
  15. #define _D 0x04 /* digit */
  16. #define _C 0x08 /* cntrl */
  17. #define _P 0x10 /* punct */
  18. #define _S 0x20 /* white space (space/lf/tab) */
  19. #define _X 0x40 /* hex digit */
  20. //#define _SP 0x80 /* hard space (0x20) */
  21. extern unsigned char _ctype[];
  22. #define isdigit(c) ((_ctype+1)[c]&(_D))
  23. #define MAX_TIMESTAMP_LEN 23 + 1 // yyyy/mm/dd hh:mm:ss.mmm
  24. #define MAX_RECORD_BUFFER_SIZE 0x10000 // 64K
  25. #define APPLICATION_ERROR "Application Error"
  26. #define DURATION_NONE 0x0000
  27. #define DURATION_HOUR_ONE 0x0001
  28. #define DURATION_HOUR_TWELVE 0x0002
  29. #define DURATION_DAY_ONE 0x0003
  30. #define DURATION_DAY_SEVENT 0x0004
  31. #define DURATION_MONTH_ONE 0x0005
  32. #define DURAITON_CUSTOM 0x000F
  33. //------------------
  34. // DEFINES
  35. //------------------
  36. #define APPLICATION_LOG "Application"
  37. #define SYSTEM_LOG "System"
  38. #define SECURITY_LOG "Security"
  39. #define REG_APPLICATION_KEY "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\"
  40. #define REG_SYSTEM_KEY "SYSTEM\\CurrentControlSet\\Services\\EventLog\\System\\"
  41. #define REG_SECURITY_KEY "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Security\\"
  42. #define REG_FULLFILL_KEY "SYSTEM\\CurrentControlSet\\Services\\EventLog\\%s\\%s"
  43. #define EVENT_MESSAGE_FILE "EventMessageFile"
  44. static CONST LPCTSTR pEventTypeNames[] = {
  45. "错误", "警告", "信息", "审计成功", "审计失败"
  46. };
  47. static DWORD GetEventTypeName(DWORD EventType)
  48. {
  49. DWORD index = 0;
  50. switch (EventType)
  51. {
  52. case EVENTLOG_ERROR_TYPE:
  53. index = 0;
  54. break;
  55. case EVENTLOG_WARNING_TYPE:
  56. index = 1;
  57. break;
  58. case EVENTLOG_INFORMATION_TYPE:
  59. index = 2;
  60. break;
  61. case EVENTLOG_AUDIT_SUCCESS:
  62. index = 3;
  63. break;
  64. case EVENTLOG_AUDIT_FAILURE:
  65. index = 4;
  66. break;
  67. }
  68. return index;
  69. }
  70. typedef struct _tagEVENTLOGFILTERPARAM {
  71. BOOL fApplication;
  72. BOOL fSecurity;
  73. BOOL fSystem;
  74. BOOL fCustom;
  75. CHAR lpszCustomEventName[MAX_PATH + 1]; //Valid when fCustom is true.
  76. CHAR lpszSourceEventName[MAX_PATH + 1];
  77. DWORD dwEventId;
  78. WORD wEventType;
  79. // This time is measured in the number of seconds elapsed since
  80. // 00:00:00 January 1,1970, Universal Coordinated Time.
  81. DWORD dwTimeBegin; //The begin time at which the record should be retrived.
  82. DWORD dwTimeEnd; //The endline time at which the record should be retrived.
  83. }EVENTLOGPARAM, *LPEVENTLOGPARAM;
  84. class EvtLogFile {
  85. public:
  86. EvtLogFile(const std::string& strFileName, BOOL bCreateIfNoExist) {
  87. _filestream.open(strFileName, std::fstream::in | std::fstream::out | std::fstream::app);
  88. }
  89. ~EvtLogFile() {
  90. if(IsValid())
  91. _filestream.close();
  92. }
  93. BOOL IsValid() const {
  94. return (_filestream.is_open() && !_filestream.fail());
  95. }
  96. void Close() {
  97. if(IsValid())
  98. _filestream.close();
  99. }
  100. std::size_t WriteEventLogEntry(const std::string& strEntry) {
  101. if(_filestream) {
  102. _filestream << strEntry << std::endl;
  103. }
  104. return 0;
  105. }
  106. private:
  107. std::fstream _filestream;
  108. };
  109. class CEventLog
  110. {
  111. public:
  112. CEventLog(void);
  113. CEventLog(LPCTSTR lpSrcName);
  114. ~CEventLog(void);
  115. bool IsInitialized(void) { return NULL != m_hEventLog; }
  116. DWORD FilterEventLog(LPCTSTR lpszSourceName, WORD wEventType,
  117. DWORD dwEventID, DWORD dwStartTime, DWORD dwEndTime);
  118. BOOL InitializeLogFile(const std::string& strFileName) {
  119. if(!pOutFile) {
  120. pOutFile = new EvtLogFile(strFileName, TRUE);
  121. std::string strTitle;
  122. strTitle.append("============================================================\r\n");
  123. strTitle.append(m_szSourceName);
  124. strTitle.append("\r\n============================================================\r\n");
  125. pOutFile->WriteEventLogEntry(strTitle);
  126. }
  127. return (pOutFile != NULL && pOutFile->IsValid());
  128. }
  129. BOOL ClearLogFile() {
  130. //if(pOutFile && pOutFile->IsValid()) {
  131. // Close();
  132. // return !(pOutFile->IsValid());
  133. //}
  134. if(pOutFile) {
  135. delete pOutFile;
  136. pOutFile = NULL;
  137. }
  138. return (pOutFile == NULL);
  139. }
  140. protected:
  141. HRESULT Initialize(LPCTSTR lpSrcName);
  142. void GetTimestamp(const DWORD Time, PSYSTEMTIME stTime, CHAR DisplayString[]);
  143. HMODULE GetMessageResources(LPCTSTR lpszdllPath)
  144. {
  145. HMODULE hResources = NULL;
  146. hResources = LoadLibraryEx(lpszdllPath, NULL,
  147. LOAD_LIBRARY_AS_IMAGE_RESOURCE | LOAD_LIBRARY_AS_DATAFILE);
  148. if (NULL == hResources)
  149. {
  150. printf("LoadLibrary(%s) failed with %lu.\n", lpszdllPath, GetLastError());
  151. }
  152. return hResources;
  153. }
  154. LPTSTR GetMessageString(HMODULE hModule, DWORD MessageId, DWORD argc, LPTSTR argv);
  155. DWORD ApplyParameterStringsToMessage(HMODULE hModule, CONST LPCTSTR pMessage, LPTSTR& pFinalMessage);
  156. private:
  157. DWORD SeekToLastRecord();
  158. DWORD GetLastRecordNumber(DWORD* pdwRecordNumber);
  159. DWORD ReadSingleRecord(PBYTE & pBuffer, DWORD dwRecordNumber, DWORD dwReadFlags);
  160. private:
  161. HANDLE m_hEventLog;
  162. CHAR m_szSourceName[MAX_PATH];
  163. EvtLogFile* pOutFile;
  164. };
  165. #endif //_TWINKLE_EVENT_LOG_HELPER_H_