9 #ifdef WIN32 // only available on Win32 17 NTEventLogAppender::NTEventLogAppender(
const std::string& name,
const std::string& sourceName) :
18 AppenderSkeleton(name),
19 _strSourceName(sourceName),
25 NTEventLogAppender::~NTEventLogAppender()
30 void NTEventLogAppender::open()
32 addRegistryInfo(_strSourceName.c_str());
33 _hEventSource = ::RegisterEventSource(NULL, _strSourceName.c_str());
36 void NTEventLogAppender::close()
39 ::DeregisterEventSource(_hEventSource);
43 bool NTEventLogAppender::reopen() {
49 bool NTEventLogAppender::requiresLayout()
const {
53 void NTEventLogAppender::setLayout(Layout* layout) {
57 void NTEventLogAppender::_append(
const LoggingEvent& event) {
59 ps[0] =
event.message.c_str();
61 const DWORD messageID = 0x1000;
62 ::ReportEvent(_hEventSource, getType(event.priority),
63 getCategory(event.priority),
64 messageID, NULL, 1, 0, ps, NULL);
72 WORD NTEventLogAppender::getCategory(Priority::Value priority) {
74 return (WORD)((priority / 100) + 1);
82 WORD NTEventLogAppender::getType(Priority::Value priority) {
93 ret_val = EVENTLOG_ERROR_TYPE;
96 ret_val = EVENTLOG_WARNING_TYPE;
98 case Priority::NOTICE:
100 case Priority::DEBUG:
102 ret_val = EVENTLOG_INFORMATION_TYPE;
108 HKEY NTEventLogAppender::regGetKey(TCHAR *subkey, DWORD *disposition) {
110 RegCreateKeyEx(HKEY_LOCAL_MACHINE, subkey, 0, NULL,
111 REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, NULL,
116 void NTEventLogAppender::regSetString(HKEY hkey, TCHAR *name, TCHAR *value) {
117 RegSetValueEx(hkey, name, 0, REG_SZ, (LPBYTE)value, lstrlen(value));
120 void NTEventLogAppender::regSetDword(HKEY hkey, TCHAR *name, DWORD value) {
121 RegSetValueEx(hkey, name, 0, REG_DWORD, (LPBYTE)&value,
sizeof(DWORD));
127 void NTEventLogAppender::addRegistryInfo(
const char *source) {
128 const TCHAR *prefix =
"SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\";
133 lstrcpy(subkey, prefix);
134 lstrcat(subkey, source);
135 hkey = regGetKey(subkey, &disposition);
136 if (disposition == REG_CREATED_NEW_KEY) {
137 regSetString(hkey,
"EventMessageFile",
"NTEventLogAppender.dll");
138 regSetString(hkey,
"CategoryMessageFile",
"NTEventLogAppender.dll");
139 regSetDword(hkey,
"TypesSupported", (DWORD)7);
140 regSetDword(hkey,
"CategoryCount", (DWORD)8);
148 std::string name, source_name;
149 params.get_for(
"nt event log appender").required(
"name", name)(
"source_name", source_name);
151 return std::auto_ptr<Appender>(
new NTEventLogAppender(name, source_name));
std::auto_ptr< Appender > create_nt_event_log_appender(const FactoryParams &)