Skip to content

Commit 088d880

Browse files
dotzengitster
authored andcommitted
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 <[email protected]> Signed-off-by: Erik Faye-Lund <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 772991a commit 088d880

File tree

5 files changed

+96
-4
lines changed

5 files changed

+96
-4
lines changed

Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@ LIB_H += compat/bswap.h
496496
LIB_H += compat/cygwin.h
497497
LIB_H += compat/mingw.h
498498
LIB_H += compat/win32/pthread.h
499+
LIB_H += compat/win32/syslog.h
499500
LIB_H += csum-file.h
500501
LIB_H += decorate.h
501502
LIB_H += delta.h
@@ -1081,7 +1082,7 @@ ifeq ($(uname_S),Windows)
10811082
AR = compat/vcbuild/scripts/lib.pl
10821083
CFLAGS =
10831084
BASIC_CFLAGS = -nologo -I. -I../zlib -Icompat/vcbuild -Icompat/vcbuild/include -DWIN32 -D_CONSOLE -DHAVE_STRING_H -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE
1084-
COMPAT_OBJS = compat/msvc.o compat/fnmatch/fnmatch.o compat/winansi.o compat/win32/pthread.o
1085+
COMPAT_OBJS = compat/msvc.o compat/fnmatch/fnmatch.o compat/winansi.o compat/win32/pthread.o compat/win32/syslog.o
10851086
COMPAT_CFLAGS = -D__USE_MINGW_ACCESS -DNOGDI -DHAVE_STRING_H -DHAVE_ALLOCA_H -Icompat -Icompat/fnmatch -Icompat/regex -Icompat/fnmatch -Icompat/win32 -DSTRIP_EXTENSION=\".exe\"
10861087
BASIC_LDFLAGS = -IGNORE:4217 -IGNORE:4049 -NOLOGO -SUBSYSTEM:CONSOLE -NODEFAULTLIB:MSVCRT.lib
10871088
EXTLIBS = advapi32.lib shell32.lib wininet.lib ws2_32.lib
@@ -1131,7 +1132,7 @@ ifneq (,$(findstring MINGW,$(uname_S)))
11311132
COMPAT_CFLAGS += -D__USE_MINGW_ACCESS -DNOGDI -Icompat -Icompat/fnmatch -Icompat/win32
11321133
COMPAT_CFLAGS += -DSTRIP_EXTENSION=\".exe\"
11331134
COMPAT_OBJS += compat/mingw.o compat/fnmatch/fnmatch.o compat/winansi.o \
1134-
compat/win32/pthread.o
1135+
compat/win32/pthread.o compat/win32/syslog.o
11351136
EXTLIBS += -lws2_32
11361137
PTHREAD_LIBS =
11371138
X = .exe

compat/win32/syslog.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include "../../git-compat-util.h"
2+
#include "../../strbuf.h"
3+
4+
static HANDLE ms_eventlog;
5+
6+
void 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+
17+
void 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+
}

compat/win32/syslog.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef SYSLOG_H
2+
#define SYSLOG_H
3+
4+
#define LOG_PID 0x01
5+
6+
#define LOG_EMERG 0
7+
#define LOG_ALERT 1
8+
#define LOG_CRIT 2
9+
#define LOG_ERR 3
10+
#define LOG_WARNING 4
11+
#define LOG_NOTICE 5
12+
#define LOG_INFO 6
13+
#define LOG_DEBUG 7
14+
15+
#define LOG_DAEMON (3<<3)
16+
17+
void openlog(const char *ident, int logopt, int facility);
18+
void syslog(int priority, const char *fmt, ...);
19+
20+
#endif /* SYSLOG_H */

daemon.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
#include "strbuf.h"
66
#include "string-list.h"
77

8-
#include <syslog.h>
9-
108
#ifndef HOST_NAME_MAX
119
#define HOST_NAME_MAX 256
1210
#endif

git-compat-util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
#include <assert.h>
105105
#include <regex.h>
106106
#include <utime.h>
107+
#include <syslog.h>
107108
#ifndef __MINGW32__
108109
#include <sys/wait.h>
109110
#include <sys/poll.h>

0 commit comments

Comments
 (0)