EventLogW.h 5.3 KB

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