event.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #include "nssm.h"
  2. #define NSSM_ERROR_BUFSIZE 65535
  3. unsigned long tls_index;
  4. /* Convert error code to error string - must call LocalFree() on return value */
  5. char *error_string(unsigned long error) {
  6. /* Thread-safe buffer */
  7. char *error_message = (char *) TlsGetValue(tls_index);
  8. if (! error_message) {
  9. error_message = (char *) LocalAlloc(LPTR, NSSM_ERROR_BUFSIZE);
  10. if (! error_message) return "<out of memory for error message>";
  11. TlsSetValue(tls_index, (void *) error_message);
  12. }
  13. if (! FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (char *) error_message, NSSM_ERROR_BUFSIZE, 0)) {
  14. if (_snprintf(error_message, NSSM_ERROR_BUFSIZE, "system error %lu", error) < 0) return 0;
  15. }
  16. return error_message;
  17. }
  18. /* Log a message to the Event Log */
  19. void log_event(unsigned short type, unsigned long id, ...) {
  20. va_list arg;
  21. int count;
  22. char *s;
  23. char *strings[6];
  24. /* Open event log */
  25. HANDLE handle = RegisterEventSource(0, TEXT(NSSM));
  26. if (! handle) return;
  27. /* Log it */
  28. count = 0;
  29. va_start(arg, id);
  30. while ((s = va_arg(arg, char *))) strings[count++] = s;
  31. va_end(arg);
  32. ReportEvent(handle, type, 0, id, 0, count, 0, (const char **) strings, 0);
  33. /* Close event log */
  34. DeregisterEventSource(handle);
  35. }