EventLog.h 4.7 KB

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