1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-11-16 06:03:44 +01:00
git/compat/win32/syslog.c
Erik Faye-Lund 2a6b149c64 mingw: avoid using strbuf in syslog
strbuf can call die, which again can call syslog from git-daemon.

Endless recursion is no fun; fix it by hand-rolling the logic. As
a side-effect malloc/realloc errors are changed into non-fatal
warnings; this is probably an improvement anyway.

Signed-off-by: Erik Faye-Lund <kusmabite@gmail.com>
Noticed-by: Johannes Sixt <j.sixt@viscovery.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2011-10-07 16:12:25 -07:00

78 lines
1.4 KiB
C

#include "../../git-compat-util.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, ...)
{
WORD logtype;
char *str, *pos;
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);
if (!str) {
warning("malloc failed: '%s'", strerror(errno));
return;
}
va_start(ap, fmt);
vsnprintf(str, str_len + 1, fmt, ap);
va_end(ap);
while ((pos = strstr(str, "%1")) != NULL) {
str = realloc(str, ++str_len + 1);
if (!str) {
warning("realloc failed: '%s'", strerror(errno));
return;
}
memmove(pos + 2, pos + 1, strlen(pos));
pos[1] = ' ';
}
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 **)&str, NULL);
free(str);
}