From 088d880247dc7bb1a27f07e320bab7f9b63e06bc Mon Sep 17 00:00:00 2001 From: Mike Pape Date: Thu, 4 Nov 2010 02:35:10 +0100 Subject: mingw: implement syslog Syslog does not usually exist on Windows, so implement our own using Window's ReportEvent mechanism. Strings containing "%1" gets expanded into them selves by ReportEvent, resulting in an unreadable string. "%2" and above is not a problem. Unfortunately, on Windows an IPv6 address can contain "%1", so expand "%1" to "% 1" before reporting. "%%1" is also a problem for ReportEvent, but that string cannot occur in an IPv6 address. Signed-off-by: Mike Pape Signed-off-by: Erik Faye-Lund Signed-off-by: Junio C Hamano --- compat/win32/syslog.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++ compat/win32/syslog.h | 20 ++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 compat/win32/syslog.c create mode 100644 compat/win32/syslog.h (limited to 'compat/win32') diff --git a/compat/win32/syslog.c b/compat/win32/syslog.c new file mode 100644 index 0000000000..42b95a9b51 --- /dev/null +++ b/compat/win32/syslog.c @@ -0,0 +1,72 @@ +#include "../../git-compat-util.h" +#include "../../strbuf.h" + +static HANDLE ms_eventlog; + +void openlog(const char *ident, int logopt, int facility) +{ + if (ms_eventlog) + return; + + ms_eventlog = RegisterEventSourceA(NULL, ident); + + if (!ms_eventlog) + warning("RegisterEventSource() failed: %lu", GetLastError()); +} + +void syslog(int priority, const char *fmt, ...) +{ + struct strbuf sb = STRBUF_INIT; + struct strbuf_expand_dict_entry dict[] = { + {"1", "% 1"}, + {NULL, NULL} + }; + WORD logtype; + char *str; + int str_len; + va_list ap; + + if (!ms_eventlog) + return; + + va_start(ap, fmt); + str_len = vsnprintf(NULL, 0, fmt, ap); + va_end(ap); + + if (str_len < 0) { + warning("vsnprintf failed: '%s'", strerror(errno)); + return; + } + + str = malloc(str_len + 1); + va_start(ap, fmt); + vsnprintf(str, str_len + 1, fmt, ap); + va_end(ap); + strbuf_expand(&sb, str, strbuf_expand_dict_cb, &dict); + free(str); + + switch (priority) { + case LOG_EMERG: + case LOG_ALERT: + case LOG_CRIT: + case LOG_ERR: + logtype = EVENTLOG_ERROR_TYPE; + break; + + case LOG_WARNING: + logtype = EVENTLOG_WARNING_TYPE; + break; + + case LOG_NOTICE: + case LOG_INFO: + case LOG_DEBUG: + default: + logtype = EVENTLOG_INFORMATION_TYPE; + break; + } + + ReportEventA(ms_eventlog, logtype, 0, 0, NULL, 1, 0, + (const char **)&sb.buf, NULL); + + strbuf_release(&sb); +} diff --git a/compat/win32/syslog.h b/compat/win32/syslog.h new file mode 100644 index 0000000000..70daa7c08b --- /dev/null +++ b/compat/win32/syslog.h @@ -0,0 +1,20 @@ +#ifndef SYSLOG_H +#define SYSLOG_H + +#define LOG_PID 0x01 + +#define LOG_EMERG 0 +#define LOG_ALERT 1 +#define LOG_CRIT 2 +#define LOG_ERR 3 +#define LOG_WARNING 4 +#define LOG_NOTICE 5 +#define LOG_INFO 6 +#define LOG_DEBUG 7 + +#define LOG_DAEMON (3<<3) + +void openlog(const char *ident, int logopt, int facility); +void syslog(int priority, const char *fmt, ...); + +#endif /* SYSLOG_H */ -- cgit v1.2.3