compat / win32 / syslog.con commit Merge branch 'jk/maint-1.6.2-upload-archive' into jk/maint-upload-archive (7b51c33)
   1#include "../../git-compat-util.h"
   2#include "../../strbuf.h"
   3
   4static HANDLE ms_eventlog;
   5
   6void openlog(const char *ident, int logopt, int facility)
   7{
   8        if (ms_eventlog)
   9                return;
  10
  11        ms_eventlog = RegisterEventSourceA(NULL, ident);
  12
  13        if (!ms_eventlog)
  14                warning("RegisterEventSource() failed: %lu", GetLastError());
  15}
  16
  17void syslog(int priority, const char *fmt, ...)
  18{
  19        struct strbuf sb = STRBUF_INIT;
  20        struct strbuf_expand_dict_entry dict[] = {
  21                {"1", "% 1"},
  22                {NULL, NULL}
  23        };
  24        WORD logtype;
  25        char *str;
  26        int str_len;
  27        va_list ap;
  28
  29        if (!ms_eventlog)
  30                return;
  31
  32        va_start(ap, fmt);
  33        str_len = vsnprintf(NULL, 0, fmt, ap);
  34        va_end(ap);
  35
  36        if (str_len < 0) {
  37                warning("vsnprintf failed: '%s'", strerror(errno));
  38                return;
  39        }
  40
  41        str = malloc(str_len + 1);
  42        va_start(ap, fmt);
  43        vsnprintf(str, str_len + 1, fmt, ap);
  44        va_end(ap);
  45        strbuf_expand(&sb, str, strbuf_expand_dict_cb, &dict);
  46        free(str);
  47
  48        switch (priority) {
  49        case LOG_EMERG:
  50        case LOG_ALERT:
  51        case LOG_CRIT:
  52        case LOG_ERR:
  53                logtype = EVENTLOG_ERROR_TYPE;
  54                break;
  55
  56        case LOG_WARNING:
  57                logtype = EVENTLOG_WARNING_TYPE;
  58                break;
  59
  60        case LOG_NOTICE:
  61        case LOG_INFO:
  62        case LOG_DEBUG:
  63        default:
  64                logtype = EVENTLOG_INFORMATION_TYPE;
  65                break;
  66        }
  67
  68        ReportEventA(ms_eventlog, logtype, 0, 0, NULL, 1, 0,
  69            (const char **)&sb.buf, NULL);
  70
  71        strbuf_release(&sb);
  72}