Fix event logging.
[nssm.git] / event.cpp
1 #include "nssm.h"\r
2 \r
3 static char error_message[65535];\r
4 \r
5 /* Convert error code to error string - must call LocalFree() on return value */\r
6 char *error_string(unsigned long error) {\r
7   if (! FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (char *) &error_message, sizeof(error_message), 0)) {\r
8     if (_snprintf(error_message, sizeof(error_message), "system error %lu", error) < 0) return 0;\r
9   }\r
10   return error_message;\r
11 }\r
12 \r
13 /* Log a message to the Event Log */\r
14 void log_event(unsigned short type, unsigned long id, ...) {\r
15   va_list arg;\r
16   int count;\r
17   char *s;\r
18   char *strings[6];\r
19 \r
20   /* Open event log */\r
21   HANDLE handle = RegisterEventSource(0, TEXT(NSSM));\r
22   if (! handle) return;\r
23 \r
24   /* Log it */\r
25   count = 0;\r
26   va_start(arg, id);\r
27   while ((s = va_arg(arg, char *))) strings[count++] = s;\r
28   va_end(arg);\r
29   ReportEvent(handle, type, 0, id, 0, count, 0, (const char **) strings, 0);\r
30 \r
31   /* Close event log */\r
32   DeregisterEventSource(handle);\r
33 }\r