20 #include "EventLogHandler.hpp"
23 EventLogHandler::EventLogHandler(
const char* source_name)
25 m_source_name(source_name),
32 EventLogHandler::~EventLogHandler()
39 check_message_resource(
void)
42 static bool check_message_resource_done =
false;
43 if (check_message_resource_done)
45 check_message_resource_done =
true;
51 if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
52 FORMAT_MESSAGE_FROM_HMODULE |
53 FORMAT_MESSAGE_IGNORE_INSERTS,
54 NULL, MSG_EVENTLOG, NULL,
55 (LPTSTR)&message_text, 0, NULL) != 0)
57 LocalFree(message_text);
63 DWORD last_err = GetLastError();
65 "This program does not seem to have the message resource "
66 "required for logging to Windows event log, error: %u ", last_err);
67 if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
68 FORMAT_MESSAGE_ALLOCATE_BUFFER |
69 FORMAT_MESSAGE_IGNORE_INSERTS,
71 (LPSTR)&message_text, 0, NULL))
73 fprintf(stderr,
"message: '%s'\n", message_text);
74 LocalFree(message_text);
78 fprintf(stderr,
"message: <unknown>\n");
89 setup_eventlogging(
const char* source_name)
92 if (!check_message_resource())
95 char sub_key[MAX_PATH];
97 "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s",
102 LONG error = RegCreateKey(HKEY_LOCAL_MACHINE, sub_key, &key_handle);
103 if (error != ERROR_SUCCESS)
106 fprintf(stderr,
"Could neither create or open key '%s', error: %u\n",
112 char module_path[MAX_PATH];
113 DWORD len = GetModuleFileName(NULL, module_path,
sizeof(module_path));
114 if (len == 0 || len ==
sizeof(module_path))
117 "Could not extract path of module, module_len: %u, error: %u\n",
118 len, GetLastError());
119 RegCloseKey(key_handle);
123 (void)RegSetValueEx(key_handle,
"EventMessageFile", 0, REG_EXPAND_SZ,
124 (PBYTE)module_path, len + 1 );
127 const DWORD event_types= (EVENTLOG_ERROR_TYPE |
128 EVENTLOG_WARNING_TYPE |
129 EVENTLOG_INFORMATION_TYPE);
130 (void)RegSetValueEx(key_handle,
"TypesSupported", 0, REG_DWORD,
131 (PBYTE)&event_types,
sizeof(event_types));
133 RegCloseKey(key_handle);
141 if (!setup_eventlogging(m_source_name))
143 fprintf(stderr,
"Failed to setup event logging\n");
147 m_event_source = RegisterEventSource(NULL, m_source_name);
150 fprintf(stderr,
"Failed to register event source, error: %u\n",
164 (void)DeregisterEventSource(m_event_source);
173 return (m_event_source != NULL);
190 case Logger::LL_DEBUG:
191 case Logger::LL_INFO:
192 type = EVENTLOG_INFORMATION_TYPE;
195 case Logger::LL_WARNING:
196 type = EVENTLOG_WARNING_TYPE;
199 case Logger::LL_ERROR:
200 case Logger::LL_ALERT:
201 case Logger::LL_CRITICAL:
202 type = EVENTLOG_ERROR_TYPE;
206 if (!ReportEvent(eventlog_handle, type, 0, MSG_EVENTLOG,
207 NULL, 1, 0, &msg, NULL))
217 EventLogHandler::writeMessage(
const char* msg)
222 if (!write_event_log(m_event_source, m_level, msg))
224 fprintf(stderr,
"Failed to report event to event log, error: %u\n",
231 EventLogHandler::writeFooter()
244 const char* msg, ...)
246 if (setup_eventlogging(source_name))
252 char buf[MAX_LOG_MESSAGE_SIZE];
255 int ret = vsnprintf_s(buf,
sizeof(buf), _TRUNCATE, msg, ap);
258 HANDLE eventlog_handle = RegisterEventSource(NULL, source_name);
259 if (!eventlog_handle)
265 if (!write_event_log(eventlog_handle, level, buf))
268 (void)DeregisterEventSource(eventlog_handle);
272 (void)DeregisterEventSource(eventlog_handle);