]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Implement optional logging to syslog (#409)
authorAnders Björklund <anders.f.bjorklund@gmail.com>
Fri, 10 May 2019 19:46:58 +0000 (21:46 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Fri, 10 May 2019 19:46:58 +0000 (21:46 +0200)
* Add syslog implementation, for the log_file

The file logger has some issues with large log lines and many jobs,
so provide the "syslog" alternative when doing the ccache logging.

* Add docs on how to filter with rsyslogd(8)

configure.ac
doc/MANUAL.adoc
misc/rsyslog.d/00-ccache.conf [new file with mode: 0644]
src/util.c

index 903091aefa4bf11bb7fe179147073df4a61e7314..b7f3f1fd2ee8f3adff384eedac9d0800cf9f2aea 100644 (file)
@@ -81,6 +81,7 @@ AC_HEADER_SYS_WAIT
 AC_CHECK_TYPES(long long)
 
 AC_CHECK_HEADERS(ctype.h pwd.h stdlib.h string.h strings.h sys/time.h sys/mman.h)
+AC_CHECK_HEADERS(syslog.h)
 AC_CHECK_HEADERS(termios.h)
 
 AC_CHECK_FUNCS(gethostname)
@@ -93,6 +94,7 @@ AC_CHECK_FUNCS(realpath)
 AC_CHECK_FUNCS(setenv)
 AC_CHECK_FUNCS(strndup)
 AC_CHECK_FUNCS(strtok_r)
+AC_CHECK_FUNCS(syslog)
 AC_CHECK_FUNCS(unsetenv)
 AC_CHECK_FUNCS(utimes)
 
index bfafd3025ff3b14b41549274fb4dc4f0ad7457f3..8e710d0498c387dcf90223b8eaef5a1050fcc8b4 100644 (file)
@@ -463,6 +463,16 @@ might be incorrect.
 
     If set to a file path, ccache will write information on what it is doing to
     the specified file. This is useful for tracking down problems.
++
+If set to "syslog", then ccache will log using syslog() instead of a file.
+Then you can use syslogd configuration to filter into a file, example:
++
+-------------------------------------------------------------------------------
+# log ccache to file
+:programname, isequal, "ccache"         /var/log/ccache
+# remove from syslog
+& ~
+-------------------------------------------------------------------------------
 
 *max_files* (*CCACHE_MAXFILES*)::
 
diff --git a/misc/rsyslog.d/00-ccache.conf b/misc/rsyslog.d/00-ccache.conf
new file mode 100644 (file)
index 0000000..cb68a12
--- /dev/null
@@ -0,0 +1,2 @@
+:programname, isequal, "ccache"         /var/log/ccache
+& ~
index 233c932f4a3ddae81067eeefaf09c77158bf7a20..625a75f2f43f211586b6a583803604f4f39910fb 100644 (file)
@@ -22,6 +22,9 @@
 #ifdef HAVE_PWD_H
 #include <pwd.h>
 #endif
+#ifdef HAVE_SYSLOG_H
+#include <syslog.h>
+#endif
 #ifdef HAVE_SYS_TIME_H
 #include <sys/time.h>
 #endif
@@ -36,6 +39,9 @@
 // Destination for conf->log_file.
 static FILE *logfile;
 
+// Whether to use syslog() instead.
+static bool use_syslog;
+
 // Buffer used for logs in conf->debug mode.
 static char *debug_log_buffer;
 
@@ -52,7 +58,7 @@ init_log(void)
 {
        extern struct conf *conf;
 
-       if (debug_log_buffer || logfile) {
+       if (debug_log_buffer || logfile || use_syslog) {
                return true;
        }
        assert(conf);
@@ -64,6 +70,13 @@ init_log(void)
        if (str_eq(conf->log_file, "")) {
                return conf->debug;
        }
+#ifdef HAVE_SYSLOG
+       if (str_eq(conf->log_file, "syslog")) {
+               use_syslog = true;
+               openlog("ccache", LOG_PID, LOG_USER);
+               return true;
+       }
+#endif
        logfile = fopen(conf->log_file, "a");
        if (logfile) {
 #ifndef _WIN32
@@ -112,6 +125,11 @@ log_prefix(bool log_updated_time)
        if (logfile) {
                fputs(prefix, logfile);
        }
+#ifdef HAVE_SYSLOG
+       if (use_syslog) {
+               // prefix information will be added by syslog
+       }
+#endif
        if (debug_log_buffer) {
                append_to_debug_log(prefix, strlen(prefix));
        }
@@ -163,6 +181,11 @@ vlog(const char *format, va_list ap, bool log_updated_time)
                        warn_log_fail();
                }
        }
+#ifdef HAVE_SYSLOG
+       if (use_syslog) {
+               vsyslog(LOG_DEBUG, format, ap);
+       }
+#endif
        if (debug_log_buffer) {
                char buf[8192];
                int len = vsnprintf(buf, sizeof(buf), format, aq);
@@ -215,6 +238,13 @@ cc_log_argv(const char *prefix, char **argv)
                        warn_log_fail();
                }
        }
+#ifdef HAVE_SYSLOG
+       if (use_syslog) {
+               char *s = format_command(argv);
+               syslog(LOG_DEBUG, "%s", s);
+               free(s);
+       }
+#endif
        if (debug_log_buffer) {
                append_to_debug_log(prefix, strlen(prefix));
                char *s = format_command(argv);