X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=event.cpp;h=726c9f104140de3bfa76498911d48d0bc58a4107;hb=fb6318bf814fd7f3445de8c83b7620e1b6b3c197;hp=661761dac8d3ef74aed441a0e313d06118bc6660;hpb=4440946eccf1cb14e16f8d29c54fb4acecbd44ff;p=nssm.git diff --git a/event.cpp b/event.cpp index 661761d..726c9f1 100644 --- a/event.cpp +++ b/event.cpp @@ -1,5 +1,24 @@ #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) { + /* 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 log_event(unsigned short type, unsigned long id, ...) { va_list arg;