xdiff/xprepare: improve O(n*m) performance in xdl_cleanup_records()
[git/raj.git] / compat / win32 / syslog.c
blob42b95a9b5169125fac7bde3caabb15f4cb6a351d
1 #include "../../git-compat-util.h"
2 #include "../../strbuf.h"
4 static HANDLE ms_eventlog;
6 void openlog(const char *ident, int logopt, int facility)
8 if (ms_eventlog)
9 return;
11 ms_eventlog = RegisterEventSourceA(NULL, ident);
13 if (!ms_eventlog)
14 warning("RegisterEventSource() failed: %lu", GetLastError());
17 void syslog(int priority, const char *fmt, ...)
19 struct strbuf sb = STRBUF_INIT;
20 struct strbuf_expand_dict_entry dict[] = {
21 {"1", "% 1"},
22 {NULL, NULL}
24 WORD logtype;
25 char *str;
26 int str_len;
27 va_list ap;
29 if (!ms_eventlog)
30 return;
32 va_start(ap, fmt);
33 str_len = vsnprintf(NULL, 0, fmt, ap);
34 va_end(ap);
36 if (str_len < 0) {
37 warning("vsnprintf failed: '%s'", strerror(errno));
38 return;
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);
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;
56 case LOG_WARNING:
57 logtype = EVENTLOG_WARNING_TYPE;
58 break;
60 case LOG_NOTICE:
61 case LOG_INFO:
62 case LOG_DEBUG:
63 default:
64 logtype = EVENTLOG_INFORMATION_TYPE;
65 break;
68 ReportEventA(ms_eventlog, logtype, 0, 0, NULL, 1, 0,
69 (const char **)&sb.buf, NULL);
71 strbuf_release(&sb);