From 26d779b300afb80974d4a6a825630920778bcc08 Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Thu, 12 Nov 2009 19:58:09 +0100 Subject: [PATCH] Log fatal messages both to stderr and log file --- ccache.c | 4 ++-- ccache.h | 2 +- execute.c | 4 ++-- hash.c | 3 +-- stats.c | 6 +++--- util.c | 37 +++++++++++++++++++++++++++---------- 6 files changed, 36 insertions(+), 20 deletions(-) diff --git a/ccache.c b/ccache.c index ef29eda..f644b75 100644 --- a/ccache.c +++ b/ccache.c @@ -145,7 +145,7 @@ static const char *tmp_string(void) #endif hostname[sizeof(hostname)-1] = 0; if (asprintf(&ret, "%s.%u", hostname, (unsigned)getpid()) == -1) { - fatal("could not allocate tmp_string"); + fatal("Could not allocate tmp_string\n"); } } @@ -996,7 +996,7 @@ static void usage(void) static void check_cache_dir(void) { if (!cache_dir) { - fatal("Unable to determine home directory"); + fatal("Unable to determine home directory\n"); } } diff --git a/ccache.h b/ccache.h index f81ff57..9b5f86c 100644 --- a/ccache.h +++ b/ccache.h @@ -95,7 +95,7 @@ char *hash_result(struct mdfour *md); void hash_buffer(struct mdfour *md, const char *s, int len); void cc_log(const char *format, ...) ATTR_FORMAT(printf, 1, 2); -void fatal(const char *msg); +void fatal(const char *format, ...) ATTR_FORMAT(printf, 1, 2); void copy_fd(int fd_in, int fd_out); int copy_file(const char *src, const char *dest); diff --git a/execute.c b/execute.c index 37252d6..3f936ea 100644 --- a/execute.c +++ b/execute.c @@ -31,7 +31,7 @@ int execute(char **argv, int status; pid = fork(); - if (pid == -1) fatal("Failed to fork"); + if (pid == -1) fatal("Failed to fork\n"); if (pid == 0) { int fd; @@ -56,7 +56,7 @@ int execute(char **argv, } if (waitpid(pid, &status, 0) != pid) { - fatal("waitpid failed"); + fatal("waitpid failed\n"); } if (WEXITSTATUS(status) == 0 && WIFSIGNALED(status)) { diff --git a/hash.c b/hash.c index eddee85..4e65527 100644 --- a/hash.c +++ b/hash.c @@ -49,8 +49,7 @@ void hash_file(struct mdfour *md, const char *fname) fd = open(fname, O_RDONLY|O_BINARY); if (fd == -1) { - cc_log("Failed to open %s\n", fname); - fatal("hash_file"); + fatal("Failed to open %s\n", fname); } while ((n = read(fd, buf, sizeof(buf))) > 0) { diff --git a/stats.c b/stats.c index d2ed9cc..d831796 100644 --- a/stats.c +++ b/stats.c @@ -85,13 +85,13 @@ static void write_stats(int fd, unsigned counters[STATS_END]) for (i=0;i= (int)sizeof(buf)-1) fatal("stats too long?!"); + if (len >= (int)sizeof(buf)-1) fatal("stats too long?!\n"); } len += snprintf(buf+len, sizeof(buf)-(len+1), "\n"); - if (len >= (int)sizeof(buf)-1) fatal("stats too long?!"); + if (len >= (int)sizeof(buf)-1) fatal("stats too long?!\n"); lseek(fd, 0, SEEK_SET); - if (write(fd, buf, len) == -1) fatal("could not write stats"); + if (write(fd, buf, len) == -1) fatal("Could not write stats\n"); } diff --git a/util.c b/util.c index f8d619a..a15492b 100644 --- a/util.c +++ b/util.c @@ -38,9 +38,26 @@ void cc_log(const char *format, ...) } /* something went badly wrong! */ -void fatal(const char *msg) +void fatal(const char *format, ...) { - cc_log("FATAL: %s\n", msg); + va_list ap; + extern char *cache_logfile; + + if (!cache_logfile) return; + + if (!logfile) logfile = fopen(cache_logfile, "a"); + if (!logfile) return; + + va_start(ap, format); + + fprintf(logfile, "FATAL: "); + vfprintf(logfile, format, ap); + fflush(logfile); + + fprintf(stderr, "ccache: FATAL: "); + vfprintf(stderr, format, ap); + + va_end(ap); exit(1); } @@ -53,7 +70,7 @@ void copy_fd(int fd_in, int fd_out) while ((n = read(fd_in, buf, sizeof(buf))) > 0) { if (write(fd_out, buf, n) != n) { - fatal("Failed to copy fd"); + fatal("Failed to copy fd\n"); } } } @@ -154,12 +171,12 @@ void copy_fd(int fd_in, int fd_out) { gz_in = gzdopen(dup(fd_in), "rb"); if (!gz_in) { - fatal("Failed to copy fd"); + fatal("Failed to copy fd\n"); } while ((n = gzread(gz_in, buf, sizeof(buf))) > 0) { if (write(fd_out, buf, n) != n) { - fatal("Failed to copy fd"); + fatal("Failed to copy fd\n"); } } } @@ -371,11 +388,11 @@ void x_asprintf(char **ptr, const char *format, ...) *ptr = NULL; va_start(ap, format); if (vasprintf(ptr, format, ap) == -1) { - fatal("out of memory in x_asprintf"); + fatal("Out of memory in x_asprintf\n"); } va_end(ap); - if (!*ptr) fatal("out of memory in x_asprintf"); + if (!*ptr) fatal("Out of memory in x_asprintf\n"); } /* @@ -386,7 +403,7 @@ char *x_strdup(const char *s) char *ret; ret = strdup(s); if (!ret) { - fatal("out of memory in strdup\n"); + fatal("Out of memory in strdup\n"); } return ret; } @@ -399,7 +416,7 @@ void *x_malloc(size_t size) void *ret; ret = malloc(size); if (!ret) { - fatal("out of memory in malloc\n"); + fatal("Out of memory in malloc\n"); } return ret; } @@ -413,7 +430,7 @@ void *x_realloc(void *ptr, size_t size) if (!ptr) return x_malloc(size); p2 = realloc(ptr, size); if (!p2) { - fatal("out of memory in x_realloc"); + fatal("Out of memory in x_realloc\n"); } return p2; }