event.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "nssm.h"
  2. #define NSSM_ERROR_BUFSIZE 65535
  3. #define NSSM_NUM_EVENT_STRINGS 16
  4. unsigned long tls_index;
  5. /* Convert error code to error string - must call LocalFree() on return value */
  6. TCHAR *error_string(unsigned long error) {
  7. /* Thread-safe buffer */
  8. TCHAR *error_message = (TCHAR *) TlsGetValue(tls_index);
  9. if (! error_message) {
  10. error_message = (TCHAR *) LocalAlloc(LPTR, NSSM_ERROR_BUFSIZE);
  11. if (! error_message) return _T("<out of memory for error message>");
  12. TlsSetValue(tls_index, (void *) error_message);
  13. }
  14. if (! FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (TCHAR *) error_message, NSSM_ERROR_BUFSIZE, 0)) {
  15. if (_sntprintf_s(error_message, NSSM_ERROR_BUFSIZE, _TRUNCATE, _T("system error %lu"), error) < 0) return 0;
  16. }
  17. return error_message;
  18. }
  19. /* Convert message code to format string */
  20. TCHAR *message_string(unsigned long error) {
  21. TCHAR *ret;
  22. if (! FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &ret, NSSM_ERROR_BUFSIZE, 0)) {
  23. ret = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, 32 * sizeof(TCHAR));
  24. if (_sntprintf_s(ret, NSSM_ERROR_BUFSIZE, _TRUNCATE, _T("system error %lu"), error) < 0) return 0;
  25. }
  26. return ret;
  27. }
  28. /* Log a message to the Event Log */
  29. void log_event(unsigned short type, unsigned long id, ...) {
  30. va_list arg;
  31. int count;
  32. TCHAR *s;
  33. TCHAR *strings[NSSM_NUM_EVENT_STRINGS];
  34. /* Open event log */
  35. HANDLE handle = RegisterEventSource(0, NSSM);
  36. if (! handle) return;
  37. /* Log it */
  38. count = 0;
  39. va_start(arg, id);
  40. while ((s = va_arg(arg, TCHAR *)) && count < NSSM_NUM_EVENT_STRINGS - 1) strings[count++] = s;
  41. strings[count] = 0;
  42. va_end(arg);
  43. ReportEvent(handle, type, 0, id, 0, count, 0, (const TCHAR **) strings, 0);
  44. /* Close event log */
  45. DeregisterEventSource(handle);
  46. }
  47. /* Log a message to the console */
  48. void print_message(FILE *file, unsigned long id, ...) {
  49. va_list arg;
  50. TCHAR *format = message_string(id);
  51. if (! format) return;
  52. va_start(arg, id);
  53. _vftprintf(file, format, arg);
  54. va_end(arg);
  55. LocalFree(format);
  56. }
  57. /* Show a GUI dialogue */
  58. int popup_message(unsigned int type, unsigned long id, ...) {
  59. va_list arg;
  60. TCHAR *format = message_string(id);
  61. if (! format) {
  62. return MessageBox(0, _T("Message %lu was supposed to go here!"), NSSM, MB_OK | MB_ICONEXCLAMATION);
  63. }
  64. TCHAR blurb[256];
  65. va_start(arg, id);
  66. if (_vsntprintf_s(blurb, sizeof(blurb), _TRUNCATE, format, arg) < 0) {
  67. va_end(arg);
  68. LocalFree(format);
  69. return MessageBox(0, _T("Message %lu was supposed to go here!"), NSSM, MB_OK | MB_ICONEXCLAMATION);
  70. }
  71. va_end(arg);
  72. int ret = MessageBox(0, blurb, NSSM, type);
  73. LocalFree(format);
  74. return ret;
  75. }