EventLogW.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. extern unsigned char _ctype[];
  13. #define isdigit(c) ((_ctype+1)[c]&(_D))
  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 L"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 L"Application"
  28. #define SYSTEM_LOG L"System"
  29. #define SECURITY_LOG L"Security"
  30. #define REG_APPLICATION_KEY L"SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\"
  31. #define REG_SYSTEM_KEY L"SYSTEM\\CurrentControlSet\\Services\\EventLog\\System\\"
  32. #define REG_SECURITY_KEY L"SYSTEM\\CurrentControlSet\\Services\\EventLog\\Security\\"
  33. #define REG_FULLFILL_KEY L"SYSTEM\\CurrentControlSet\\Services\\EventLog\\%s\\%s"
  34. #define EVENT_MESSAGE_FILE L"EventMessageFile"
  35. static LPCWSTR pEventTypeNames[] = {
  36. L"错误", L"警告", L"信息", L"审计成功", L"审计失败"
  37. };
  38. static DWORD GetEventTypeNameW(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 _tagEVENTLOGFILTERPARAMW {
  62. BOOL fApplication;
  63. BOOL fSecurity;
  64. BOOL fSystem;
  65. BOOL fCustom;
  66. WCHAR lpszCustomEventName[MAX_PATH + 1]; //Valid when fCustom is true.
  67. WCHAR 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_W, *LPEVENTLOGPARAM_W;
  75. class EvtLogFileW {
  76. public:
  77. EvtLogFileW(const std::wstring& strFileName, BOOL bCreateIfNoExist) {
  78. _filestream.open(strFileName, std::fstream::binary | std::fstream::out | std::fstream::app);
  79. _filestream.imbue(std::locale("chs"));
  80. }
  81. ~EvtLogFileW() {
  82. if(IsValid())
  83. _filestream.close();
  84. }
  85. static std::string to_utf8(const wchar_t* buffer, int len)
  86. {
  87. int nChars = ::WideCharToMultiByte(
  88. CP_UTF8,
  89. 0,
  90. buffer,
  91. len,
  92. NULL,
  93. 0,
  94. NULL,
  95. NULL);
  96. if (nChars == 0) return "";
  97. std::string newbuffer;
  98. newbuffer.resize(nChars);
  99. ::WideCharToMultiByte(
  100. CP_UTF8,
  101. 0,
  102. buffer,
  103. len,
  104. const_cast< char* >(newbuffer.c_str()),
  105. nChars,
  106. NULL,
  107. NULL);
  108. return newbuffer;
  109. }
  110. static std::string to_utf8(const std::wstring& str)
  111. {
  112. return to_utf8(str.c_str(), (int)str.size());
  113. }
  114. BOOL IsValid() const {
  115. return (_filestream.is_open() && !_filestream.fail());
  116. }
  117. void Close() {
  118. if(IsValid())
  119. _filestream.close();
  120. }
  121. std::size_t WriteEventLogEntry(const std::wstring& strEntry) {
  122. if(_filestream) {
  123. std::string outtext = to_utf8(strEntry);
  124. _filestream << outtext << std::endl;
  125. //_filestream << strEntry << std::endl;
  126. }
  127. return 0;
  128. }
  129. private:
  130. std::fstream _filestream;
  131. };
  132. class CEventLogW
  133. {
  134. public:
  135. CEventLogW(void);
  136. CEventLogW(LPCWSTR lpSrcName, BOOL bCustom = FALSE);
  137. ~CEventLogW(void);
  138. bool IsInitialized(void) const { return NULL != m_hEventLog; }
  139. DWORD FilterEventLog(LPCWSTR lpszSourceName, WORD wEventType,
  140. DWORD dwEventID, DWORD dwStartTime, DWORD dwEndTime);
  141. BOOL InitializeLogFile(const std::wstring& strFileName) {
  142. if(!pOutFile) {
  143. pOutFile = new EvtLogFileW(strFileName, TRUE);
  144. std::wstring strTitle;
  145. strTitle.append(L"============================================================\r\n");
  146. strTitle.append(m_szSourceName);
  147. strTitle.append(L"\r\n============================================================\r\n");
  148. pOutFile->WriteEventLogEntry(strTitle);
  149. }
  150. return (pOutFile != NULL && pOutFile->IsValid());
  151. }
  152. BOOL ClearLogFile() {
  153. if(pOutFile) {
  154. delete pOutFile;
  155. pOutFile = NULL;
  156. }
  157. return (pOutFile == NULL);
  158. }
  159. protected:
  160. HRESULT Initialize(LPCWSTR lpSrcName, BOOL bCustom);
  161. void GetTimestamp(const DWORD Time, PSYSTEMTIME stTime, WCHAR DisplayString[]);
  162. static HMODULE GetMessageResources(LPCWSTR lpszdllPath)
  163. {
  164. HMODULE hResources;
  165. hResources = LoadLibraryExW(lpszdllPath, NULL,
  166. LOAD_LIBRARY_AS_IMAGE_RESOURCE | LOAD_LIBRARY_AS_DATAFILE);
  167. if (NULL == hResources)
  168. {
  169. //!!wprintf(L"LoadLibrary(%s) failed with %lu.\n", lpszdllPath, GetLastError());
  170. }
  171. return hResources;
  172. }
  173. LPWSTR GetMessageString(HMODULE hModule, DWORD MessageId, DWORD argc, LPWSTR argv);
  174. DWORD ApplyParameterStringsToMessage(HMODULE hModule, CONST LPCWSTR pMessage, LPWSTR& pFinalMessage);
  175. private:
  176. DWORD SeekToLastRecord();
  177. DWORD GetLastRecordNumber(DWORD* pdwRecordNumber);
  178. DWORD ReadSingleRecord(PBYTE & pBuffer, DWORD dwRecordNumber, DWORD dwReadFlags);
  179. private:
  180. HANDLE m_hEventLog;
  181. WCHAR m_szSourceName[MAX_PATH];
  182. EvtLogFileW* pOutFile;
  183. };
  184. #endif //_TWINKLE_EVENT_LOG_W_HELPER_H_