event.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #include "nssm.h"
  2. /* Convert error code to error string - must call LocalFree() on return value */
  3. char *error_string(unsigned long error) {
  4. char *message;
  5. if (! FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (char *) &message, 0, 0)) return 0;
  6. return message;
  7. }
  8. /* Log a message to the Event Log */
  9. void eventprintf(unsigned short type, char *format, ...) {
  10. char message[4096];
  11. char *strings[2];
  12. int n, size;
  13. va_list arg;
  14. /* Construct the message */
  15. size = sizeof(message);
  16. va_start(arg, format);
  17. n = _vsnprintf(message, size, format, arg);
  18. va_end(arg);
  19. /* Check success */
  20. if (n < 0 || n >= size) return;
  21. /* Construct strings array */
  22. strings[0] = message;
  23. strings[1] = 0;
  24. /* Open event log */
  25. HANDLE handle = RegisterEventSource(0, TEXT(NSSM));
  26. if (! handle) return;
  27. /* Log it */
  28. if (! ReportEvent(handle, type, 0, 0, 0, 1, 0, (const char **) strings, 0)) {
  29. printf("ReportEvent(): %s\n", error_string(GetLastError()));
  30. }
  31. /* Close event log */
  32. DeregisterEventSource(handle);
  33. }