99#define initgroups (x , y ) (0) /* nothing */
1010#endif
1111
12- static int log_syslog ;
12+ static enum log_destination {
13+ LOG_DESTINATION_UNSET = -1 ,
14+ LOG_DESTINATION_NONE = 0 ,
15+ LOG_DESTINATION_STDERR = 1 ,
16+ LOG_DESTINATION_SYSLOG = 2 ,
17+ } log_destination = LOG_DESTINATION_UNSET ;
1318static int verbose ;
1419static int reuseaddr ;
1520static int informative_errors ;
@@ -25,6 +30,7 @@ static const char daemon_usage[] =
2530" [--access-hook=<path>]\n"
2631" [--inetd | [--listen=<host_or_ipaddr>] [--port=<n>]\n"
2732" [--detach] [--user=<user> [--group=<group>]]\n"
33+ " [--log-destination=(stderr|syslog|none)]\n"
2834" [<directory>...]" ;
2935
3036/* List of acceptable pathname prefixes */
@@ -74,11 +80,14 @@ static const char *get_ip_address(struct hostinfo *hi)
7480
7581static void logreport (int priority , const char * err , va_list params )
7682{
77- if (log_syslog ) {
83+ switch (log_destination ) {
84+ case LOG_DESTINATION_SYSLOG : {
7885 char buf [1024 ];
7986 vsnprintf (buf , sizeof (buf ), err , params );
8087 syslog (priority , "%s" , buf );
81- } else {
88+ break ;
89+ }
90+ case LOG_DESTINATION_STDERR :
8291 /*
8392 * Since stderr is set to buffered mode, the
8493 * logging of different processes will not overlap
@@ -88,6 +97,11 @@ static void logreport(int priority, const char *err, va_list params)
8897 vfprintf (stderr , err , params );
8998 fputc ('\n' , stderr );
9099 fflush (stderr );
100+ break ;
101+ case LOG_DESTINATION_NONE :
102+ break ;
103+ case LOG_DESTINATION_UNSET :
104+ BUG ("log destination not initialized correctly" );
91105 }
92106}
93107
@@ -1286,17 +1300,29 @@ int cmd_main(int argc, const char **argv)
12861300 }
12871301 if (!strcmp (arg , "--inetd" )) {
12881302 inetd_mode = 1 ;
1289- log_syslog = 1 ;
12901303 continue ;
12911304 }
12921305 if (!strcmp (arg , "--verbose" )) {
12931306 verbose = 1 ;
12941307 continue ;
12951308 }
12961309 if (!strcmp (arg , "--syslog" )) {
1297- log_syslog = 1 ;
1310+ log_destination = LOG_DESTINATION_SYSLOG ;
12981311 continue ;
12991312 }
1313+ if (skip_prefix (arg , "--log-destination=" , & v )) {
1314+ if (!strcmp (v , "syslog" )) {
1315+ log_destination = LOG_DESTINATION_SYSLOG ;
1316+ continue ;
1317+ } else if (!strcmp (v , "stderr" )) {
1318+ log_destination = LOG_DESTINATION_STDERR ;
1319+ continue ;
1320+ } else if (!strcmp (v , "none" )) {
1321+ log_destination = LOG_DESTINATION_NONE ;
1322+ continue ;
1323+ } else
1324+ die ("unknown log destination '%s'" , v );
1325+ }
13001326 if (!strcmp (arg , "--export-all" )) {
13011327 export_all_trees = 1 ;
13021328 continue ;
@@ -1353,7 +1379,6 @@ int cmd_main(int argc, const char **argv)
13531379 }
13541380 if (!strcmp (arg , "--detach" )) {
13551381 detach = 1 ;
1356- log_syslog = 1 ;
13571382 continue ;
13581383 }
13591384 if (skip_prefix (arg , "--user=" , & v )) {
@@ -1399,7 +1424,14 @@ int cmd_main(int argc, const char **argv)
13991424 usage (daemon_usage );
14001425 }
14011426
1402- if (log_syslog ) {
1427+ if (log_destination == LOG_DESTINATION_UNSET ) {
1428+ if (inetd_mode || detach )
1429+ log_destination = LOG_DESTINATION_SYSLOG ;
1430+ else
1431+ log_destination = LOG_DESTINATION_STDERR ;
1432+ }
1433+
1434+ if (log_destination == LOG_DESTINATION_SYSLOG ) {
14031435 openlog ("git-daemon" , LOG_PID , LOG_DAEMON );
14041436 set_die_routine (daemon_die );
14051437 } else
0 commit comments