X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=event.cpp;fp=event.cpp;h=baec6be25ec715199c0c120fc1bc0af8b3a74b96;hb=f2ec1c0c55a6b3e8ca02b3d66b78c87fe0ac1f47;hp=726c9f104140de3bfa76498911d48d0bc58a4107;hpb=933eb36fac6b7f5fe8fe7ff62839ce5c92fff7f4;p=nssm.git diff --git a/event.cpp b/event.cpp index 726c9f1..baec6be 100644 --- a/event.cpp +++ b/event.cpp @@ -19,6 +19,16 @@ char *error_string(unsigned long error) { return error_message; } +/* Convert message code to format string */ +char *message_string(unsigned long error) { + char *ret; + if (! FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR) &ret, NSSM_ERROR_BUFSIZE, 0)) { + ret = (char *) HeapAlloc(GetProcessHeap(), 0, 32); + if (_snprintf(ret, NSSM_ERROR_BUFSIZE, "system error %lu", error) < 0) return 0; + } + return ret; +} + /* Log a message to the Event Log */ void log_event(unsigned short type, unsigned long id, ...) { va_list arg; @@ -40,3 +50,42 @@ void log_event(unsigned short type, unsigned long id, ...) { /* Close event log */ DeregisterEventSource(handle); } + +/* Log a message to the console */ +void print_message(FILE *file, unsigned long id, ...) { + va_list arg; + + char *format = message_string(id); + if (! format) return; + + va_start(arg, id); + vfprintf(file, format, arg); + va_end(arg); + + LocalFree(format); +} + +/* Show a GUI dialogue */ +int popup_message(unsigned int type, unsigned long id, ...) { + va_list arg; + + char *format = message_string(id); + if (! format) { + return MessageBox(0, "Message %lu was supposed to go here!", NSSM, MB_OK | MB_ICONEXCLAMATION); + } + + char blurb[256]; + va_start(arg, id); + if (vsnprintf(blurb, sizeof(blurb), format, arg) < 0) { + va_end(arg); + LocalFree(format); + return MessageBox(0, "Message %lu was supposed to go here!", NSSM, MB_OK | MB_ICONEXCLAMATION); + } + va_end(arg); + + int ret = MessageBox(0, blurb, NSSM, type); + + LocalFree(format); + + return ret; +}