From: Joel Rosdahl Date: Mon, 10 May 2010 16:30:17 +0000 (+0200) Subject: Improve log initialization X-Git-Tag: v3.0pre1~5 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=4795f6e3defd1f15a9f2f661f80215a12102f2aa;p=thirdparty%2Fccache.git Improve log initialization --- diff --git a/ccache.h b/ccache.h index be9ec4147..7d50b2dd2 100644 --- a/ccache.h +++ b/ccache.h @@ -65,9 +65,9 @@ char *hash_result(struct mdfour *md); void hash_result_as_bytes(struct mdfour *md, unsigned char *out); void hash_buffer(struct mdfour *md, const void *s, size_t len); -int cc_log_no_newline(const char *format, ...) ATTR_FORMAT(printf, 1, 2); -int cc_log(const char *format, ...) ATTR_FORMAT(printf, 1, 2); -int cc_log_executed_command(char **argv); +void cc_log_no_newline(const char *format, ...) ATTR_FORMAT(printf, 1, 2); +void cc_log(const char *format, ...) ATTR_FORMAT(printf, 1, 2); +void cc_log_executed_command(char **argv); void fatal(const char *format, ...) ATTR_FORMAT(printf, 1, 2); void copy_fd(int fd_in, int fd_out); diff --git a/util.c b/util.c index 4a6195d79..0aef7f972 100644 --- a/util.c +++ b/util.c @@ -42,67 +42,76 @@ static FILE *logfile; -static int cc_log_va_list(const char *format, va_list ap) +static int init_log() { extern char *cache_logfile; + if (logfile) { + return 1; + } if (!cache_logfile) { return 0; } + logfile = fopen(cache_logfile, "a"); + if (logfile) { + return 1; + } else { + return 0; + } +} - if (!logfile) { - logfile = fopen(cache_logfile, "a"); - if (!logfile) { - return 0; - } +static void log_va_list(const char *format, va_list ap) +{ + if (!init_log()) { + return; } fprintf(logfile, "[%-5d] ", getpid()); vfprintf(logfile, format, ap); - - return 1; } /* * Log a message to the CCACHE_LOGFILE location without newline and without * flushing. */ -int cc_log_no_newline(const char *format, ...) +void cc_log_no_newline(const char *format, ...) { - int logged; + if (!init_log()) { + return; + } + va_list ap; va_start(ap, format); - logged = cc_log_va_list(format, ap); + log_va_list(format, ap); va_end(ap); - return logged; } /* * Log a message to the CCACHE_LOGFILE location adding a newline and flushing. */ -int cc_log(const char *format, ...) +void cc_log(const char *format, ...) { - int logged; + if (!init_log()) { + return; + } + va_list ap; va_start(ap, format); - logged = cc_log_va_list(format, ap); - if (logged) { - fprintf(logfile, "\n"); - fflush(logfile); - } + log_va_list(format, ap); va_end(ap); - return logged; + fprintf(logfile, "\n"); + fflush(logfile); } -int cc_log_executed_command(char **argv) +void cc_log_executed_command(char **argv) { - if (cc_log_no_newline("Executing ")) { - print_command(logfile, argv); - fflush(logfile); - return 1; - } else { - return 0; + if (!init_log()) { + return; } + + cc_log_no_newline("Executing "); + print_command(logfile, argv); + fflush(logfile); } /* something went badly wrong! */