event.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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, GetUserDefaultLangID(), (TCHAR *) error_message, NSSM_ERROR_BUFSIZE, 0)) {
  15. if (! FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, 0, (TCHAR *) error_message, NSSM_ERROR_BUFSIZE, 0)) {
  16. if (_sntprintf_s(error_message, NSSM_ERROR_BUFSIZE, _TRUNCATE, _T("system error %lu"), error) < 0) return 0;
  17. }
  18. }
  19. return error_message;
  20. }
  21. /* Convert message code to format string */
  22. TCHAR *message_string(unsigned long error) {
  23. TCHAR *ret;
  24. if (! FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, GetUserDefaultLangID(), (LPTSTR) &ret, NSSM_ERROR_BUFSIZE, 0)) {
  25. if (! FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, 0, (LPTSTR) &ret, NSSM_ERROR_BUFSIZE, 0)) {
  26. ret = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, 32 * sizeof(TCHAR));
  27. if (_sntprintf_s(ret, NSSM_ERROR_BUFSIZE, _TRUNCATE, _T("system error %lu"), error) < 0) return 0;
  28. }
  29. }
  30. return ret;
  31. }
  32. /* Log a message to the Event Log */
  33. void log_event(unsigned short type, unsigned long id, ...) {
  34. va_list arg;
  35. int count;
  36. TCHAR *s;
  37. TCHAR *strings[NSSM_NUM_EVENT_STRINGS];
  38. /* Open event log */
  39. HANDLE handle = RegisterEventSource(0, NSSM);
  40. if (! handle) return;
  41. /* Log it */
  42. count = 0;
  43. va_start(arg, id);
  44. while ((s = va_arg(arg, TCHAR *)) && count < NSSM_NUM_EVENT_STRINGS - 1) strings[count++] = s;
  45. strings[count] = 0;
  46. va_end(arg);
  47. ReportEvent(handle, type, 0, id, 0, count, 0, (const TCHAR **) strings, 0);
  48. /* Close event log */
  49. DeregisterEventSource(handle);
  50. }
  51. /* Log a message to the console */
  52. void print_message(FILE *file, unsigned long id, ...) {
  53. va_list arg;
  54. TCHAR *format = message_string(id);
  55. if (! format) return;
  56. va_start(arg, id);
  57. _vftprintf(file, format, arg);
  58. va_end(arg);
  59. LocalFree(format);
  60. }
  61. /* Show a GUI dialogue */
  62. int popup_message(HWND owner, unsigned int type, unsigned long id, ...) {
  63. va_list arg;
  64. TCHAR *format = message_string(id);
  65. if (! format) {
  66. return MessageBox(0, _T("The message which was supposed to go here is missing!"), NSSM, MB_OK | MB_ICONEXCLAMATION);
  67. }
  68. TCHAR blurb[1024];
  69. va_start(arg, id);
  70. if (_vsntprintf_s(blurb, _countof(blurb), _TRUNCATE, format, arg) < 0) {
  71. va_end(arg);
  72. LocalFree(format);
  73. return MessageBox(0, _T("The message which was supposed to go here is too big!"), NSSM, MB_OK | MB_ICONEXCLAMATION);
  74. }
  75. va_end(arg);
  76. MSGBOXPARAMS params;
  77. ZeroMemory(&params, sizeof(params));
  78. params.cbSize = sizeof(params);
  79. params.hInstance = GetModuleHandle(0);
  80. params.hwndOwner = owner;
  81. params.lpszText = blurb;
  82. params.lpszCaption = NSSM;
  83. params.dwStyle = type;
  84. if (type == MB_OK) {
  85. params.dwStyle |= MB_USERICON;
  86. params.lpszIcon = MAKEINTRESOURCE(IDI_NSSM);
  87. }
  88. int ret = MessageBoxIndirect(&params);
  89. LocalFree(format);
  90. return ret;
  91. }