event.cpp 3.7 KB

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