This source file includes following definitions.
- Log
- Log
- Delete
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 #pragma once
22
23 #include "RegistryKey.hpp"
24 #include "dll/EventLogMessages.h"
25
26 #include "../../../core/non_copyable.hpp"
27
28 namespace jmmcg { namespace NTUtils {
29
30 class AFX_EXT_CLASS EventLog : protected non_copyable {
31 public:
32 typedef win_exception exception_type;
33
34
35
36 enum error_types {
37 err_error=EVENTLOG_ERROR_TYPE,
38 err_warning=EVENTLOG_WARNING_TYPE,
39 err_information=EVENTLOG_INFORMATION_TYPE,
40 err_audit_success=EVENTLOG_AUDIT_SUCCESS,
41 err_audit_fail=EVENTLOG_AUDIT_FAILURE
42 };
43 enum categories {
44 cat_none=CAT_NONE,
45 cat_success=CAT_SUCCESS,
46 cat_warning=CAT_WARNING,
47 cat_error=CAT_ERROR,
48 cat_critical=CAT_CRITICAL,
49 cat_information=CAT_INFO,
50 cat_custom1=CAT_CUSTOM1,
51 cat_custom2=CAT_CUSTOM2,
52 cat_custom3=CAT_CUSTOM3,
53 cat_custom4=CAT_CUSTOM4,
54 cat_custom5=CAT_CUSTOM5
55 };
56
57 __stdcall EventLog();
58
59
60
61 __stdcall EventLog(const tstring &event_src_name, const unsigned int types = err_error|err_warning|err_information, const tstring &server = _T(""), const tstring &appln_log = _T("Application"));
62 __stdcall ~EventLog();
63
64 void __fastcall Create(const tstring &event_src_name, const unsigned int types = err_error|err_warning|err_information, const tstring &server = _T(""), const tstring &appln_log = _T("Application"));
65 void __fastcall Delete();
66
67
68
69 void __fastcall Log(const error_types wType, const categories Category, const tstring &str, const unsigned long dwDataSize = 0, void * const lpRawData = NULL, SID * const lpUserSid = NULL);
70 void __fastcall Log(const error_types wType, const categories Category, const tstringstream &strm, const unsigned long dwDataSize = 0, void * const lpRawData = NULL, SID * const lpUserSid = NULL) {
71 Log(wType, Category, strm.str(), dwDataSize, lpRawData, lpUserSid);
72 }
73 template<typename T> void __fastcall
74 Log(const error_types wType, const categories wCategory, const typename T::const_iterator &begin, const typename T::const_iterator &end, const unsigned long dwDataSize, void * const lpRawData, SID * const lpUserSid) {
75 assert(event_src);
76 std::auto_ptr<TCHAR *> strs;
77 if (begin!=end) {
78 strs=std::auto_ptr<TCHAR *>(new TCHAR *[end-begin]);
79 typename T::const_iterator i(begin);
80 do {
81 strs[i-begin]=new TCHAR[i->size()];
82 _tcscpy(strs[i-begin], i->c_str());
83 } while (++i!=end);
84 }
85 const bool ret=static_cast<bool>(::ReportEvent(event_src, (WORD)wType, (WORD)wCategory, DEFAULT_MSG, lpUserSid, end-begin, dwDataSize, const_cast<const TCHAR **>(strs.get()), lpRawData));
86 if (strs.get()) {
87 typename T::const_iterator i(begin);
88 do {
89 delete[] strs[i-begin];
90 } while (++i!=end);
91 }
92 if (!ret) {
93 jmmcg::info::function fn(__LINE__,__PRETTY_FUNCTION__,typeid(EventLog::Log),jmmcg::info::function::argument(_T("const error_types wType"),jmmcg::tostring(wType)));
94 fn.add_arg(_T("const categories wCategory"),jmmcg::tostring(wCategory));
95 tstringstream ss;
96 typename T::const_iterator i(begin);
97 do {
98 ss<<_T("'")<<*i<<_T("'")<<std::endl;
99 } while (++i!=end);
100 fn.add_arg(_T("String data:"),ss.str());
101 fn.add_arg(_T("const unsigned long dwDataSize"),jmmcg::tostring(dwDataSize));
102 fn.add_arg(_T("void * const lpRawData"),jmmcg::tostring(lpRawData));
103 fn.add_arg(_T("SID * const lpUserSid"),jmmcg::tostring(lpUserSid));
104 throw exception(error_msgs[3],fn,JMMCG_REVISION(_JMMCG_REVISION_HDR));
105 }
106 }
107
108 private:
109 RegistryKey key;
110 HANDLE event_src;
111
112 void operator=(const EventLog &)=delete;
113 };
114
115 inline __stdcall
116 EventLog::EventLog()
117 : event_src(NULL) {
118 }
119
120 inline __stdcall
121 EventLog::~EventLog() {
122 Delete();
123 }
124
125 inline void __fastcall
126 EventLog::Delete() {
127 ::DeregisterEventSource(event_src);
128 }
129
130 } }