git-daemon: use LOG_PID, simplify logging code
Make git-daemon use LOG_PID like most daemons, instead of prepending the pid to the message ourselves, when using syslog(3). Simplify the logging code by setting stderr to line buffered, instead of building a single string and writing it out with a single write(2). Give an extra log message at the daemon start-up. Signed-off-by: Stephen R. van den Berg <srb@cuci.nl> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Stephen R. van den Berg committed
Aug 14, 2008 at 20:02 UTC
6a992e9e1da08af3e36ab90d5f5b7ee8077630ce
1 file changed
+16
-31
daemon.c
+16
-31
@@ -78,38 +78,19 @@ static struct interp interp_table[] = {
78
79
static void logreport(int priority, const char *err, va_list params)
80
{
81
- /* We should do a single write so that it is atomic and output
82
- * of several processes do not get intermingled. */
83
- char buf[1024];
84
- int buflen;
85
- int maxlen, msglen;
86
-
87
- /* sizeof(buf) should be big enough for "[pid] \n" */
88
- buflen = snprintf(buf, sizeof(buf), "[%ld] ", (long) getpid());
89
-
90
- maxlen = sizeof(buf) - buflen - 1; /* -1 for our own LF */
91
- msglen = vsnprintf(buf + buflen, maxlen, err, params);
92
-
81
if (log_syslog) {
82
+ char buf[1024];
83
+ vsnprintf(buf, sizeof(buf), err, params);
84
syslog(priority, "%s", buf);
95
- return;
85
}
97
-
98
- /* maxlen counted our own LF but also counts space given to
99
- * vsnprintf for the terminating NUL. We want to make sure that
100
- * we have space for our own LF and NUL after the "meat" of the
101
- * message, so truncate it at maxlen - 1.
102
- */
103
- if (msglen > maxlen - 1)
104
- msglen = maxlen - 1;
105
- else if (msglen < 0)
106
- msglen = 0; /* Protect against weird return values. */
107
- buflen += msglen;
108
-
109
- buf[buflen++] = '\n';
110
- buf[buflen] = '\0';
111
-
112
- write_in_full(2, buf, buflen);
86
+ else {
87
+ /* Since stderr is set to linebuffered mode, the
88
+ * logging of different processes will not overlap
89
+ */
90
+ fprintf(stderr, "[%d] ", (int)getpid());
91
+ vfprintf(stderr, err, params);
92
+ fputc('\n', stderr);
93
+ }
94
}
95
96
static void logerror(const char *err, ...)
@@ -1178,9 +1159,11 @@ int main(int argc, char **argv)
1159
}
1160
1161
if (log_syslog) {
1181
- openlog("git-daemon", 0, LOG_DAEMON);
1162
+ openlog("git-daemon", LOG_PID, LOG_DAEMON);
1163
set_die_routine(daemon_die);
1164
}
1165
+ else
1166
+ setlinebuf(stderr); /* avoid splitting a message in the middle */
1167
1168
if (inetd_mode && (group_name || user_name))
1169
die("--user and --group are incompatible with --inetd");
@@ -1233,8 +1216,10 @@ int main(int argc, char **argv)
1216
return execute(peer);
1217
}
1218
1236
- if (detach)
1219
+ if (detach) {
1220
daemonize();
1221
+ loginfo("Ready to rumble");
1222
+ }
1223
else
1224
sanitize_stdfds();
1225