src/os/win32/ngx_event_log.c - nginx source code

Functions defined

Macros defined

Source code

  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */


  5. #include <ngx_config.h>
  6. #include <ngx_core.h>


  7. #define NGX_MAX_ERROR_STR   2048


  8. void ngx_cdecl
  9. ngx_event_log(ngx_err_t err, const char *fmt, ...)
  10. {
  11.     u_char         *p, *last;
  12.     long            types;
  13.     HKEY            key;
  14.     HANDLE          ev;
  15.     va_list         args;
  16.     u_char          text[NGX_MAX_ERROR_STR];
  17.     const char     *msgarg[9];
  18.     static u_char   netmsg[] = "%SystemRoot%\\System32\\netmsg.dll";

  19.     last = text + NGX_MAX_ERROR_STR;
  20.     p = text + GetModuleFileName(NULL, (char *) text, NGX_MAX_ERROR_STR - 50);

  21.     *p++ = ':';
  22.     ngx_linefeed(p);

  23.     va_start(args, fmt);
  24.     p = ngx_vslprintf(p, last, fmt, args);
  25.     va_end(args);

  26.     if (err) {
  27.         p = ngx_log_errno(p, last, err);
  28.     }

  29.     if (p > last - NGX_LINEFEED_SIZE - 1) {
  30.         p = last - NGX_LINEFEED_SIZE - 1;
  31.     }

  32.     ngx_linefeed(p);

  33.     *p = '\0';

  34.     /*
  35.      * we do not log errors here since we use
  36.      * Event Log only to log our own logs open errors
  37.      */

  38.     if (RegCreateKeyEx(HKEY_LOCAL_MACHINE,
  39.            "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\nginx",
  40.            0, NULL, REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, NULL, &key, NULL)
  41.         != 0)
  42.     {
  43.         return;
  44.     }

  45.     if (RegSetValueEx(key, "EventMessageFile", 0, REG_EXPAND_SZ,
  46.                       netmsg, sizeof(netmsg) - 1)
  47.         != 0)
  48.     {
  49.         return;
  50.     }

  51.     types = EVENTLOG_ERROR_TYPE;

  52.     if (RegSetValueEx(key, "TypesSupported", 0, REG_DWORD,
  53.                       (u_char *) &types, sizeof(long))
  54.         != 0)
  55.     {
  56.         return;
  57.     }

  58.     RegCloseKey(key);

  59.     ev = RegisterEventSource(NULL, "nginx");

  60.     msgarg[0] = (char *) text;
  61.     msgarg[1] = NULL;
  62.     msgarg[2] = NULL;
  63.     msgarg[3] = NULL;
  64.     msgarg[4] = NULL;
  65.     msgarg[5] = NULL;
  66.     msgarg[6] = NULL;
  67.     msgarg[7] = NULL;
  68.     msgarg[8] = NULL;

  69.     /*
  70.      * the 3299 event id in netmsg.dll has the generic message format:
  71.      *     "%1 %2 %3 %4 %5 %6 %7 %8 %9"
  72.      */

  73.     ReportEvent(ev, EVENTLOG_ERROR_TYPE, 0, 3299, NULL, 9, 0, msgarg, NULL);

  74.     DeregisterEventSource(ev);
  75. }