X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=event.cpp;h=726c9f104140de3bfa76498911d48d0bc58a4107;hb=5078d2360f13ed4e122d21202f06d0a97b75b458;hp=7f2b9f4f3363f2ae1feab6c3ee3b96099dcd94d6;hpb=d007ff22cecf2ce85cc68a1de81a010d4a65badc;p=nssm.git diff --git a/event.cpp b/event.cpp index 7f2b9f4..726c9f1 100644 --- a/event.cpp +++ b/event.cpp @@ -1,40 +1,41 @@ #include "nssm.h" +#define NSSM_ERROR_BUFSIZE 65535 +unsigned long tls_index; + /* Convert error code to error string - must call LocalFree() on return value */ char *error_string(unsigned long error) { - char *message; - if (! FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (char *) &message, 0, 0)) return 0; - return message; + /* Thread-safe buffer */ + char *error_message = (char *) TlsGetValue(tls_index); + if (! error_message) { + error_message = (char *) LocalAlloc(LPTR, NSSM_ERROR_BUFSIZE); + if (! error_message) return ""; + TlsSetValue(tls_index, (void *) error_message); + } + + if (! FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (char *) error_message, NSSM_ERROR_BUFSIZE, 0)) { + if (_snprintf(error_message, NSSM_ERROR_BUFSIZE, "system error %lu", error) < 0) return 0; + } + return error_message; } /* Log a message to the Event Log */ -void eventprintf(unsigned short type, char *format, ...) { - char message[4096]; - char *strings[2]; - int n, size; +void log_event(unsigned short type, unsigned long id, ...) { va_list arg; + int count; + char *s; + char *strings[6]; - /* Construct the message */ - size = sizeof(message); - va_start(arg, format); - n = _vsnprintf(message, size, format, arg); - va_end(arg); - - /* Check success */ - if (n < 0 || n >= size) return; - - /* Construct strings array */ - strings[0] = message; - strings[1] = 0; - /* Open event log */ HANDLE handle = RegisterEventSource(0, TEXT(NSSM)); if (! handle) return; /* Log it */ - if (! ReportEvent(handle, type, 0, 0, 0, 1, 0, (const char **) strings, 0)) { - printf("ReportEvent(): %s\n", error_string(GetLastError())); - } + count = 0; + va_start(arg, id); + while ((s = va_arg(arg, char *))) strings[count++] = s; + va_end(arg); + ReportEvent(handle, type, 0, id, 0, count, 0, (const char **) strings, 0); /* Close event log */ DeregisterEventSource(handle);