]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Log fatal messages both to stderr and log file
authorJoel Rosdahl <joel@rosdahl.net>
Thu, 12 Nov 2009 18:58:09 +0000 (19:58 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 5 Jan 2010 17:53:01 +0000 (18:53 +0100)
ccache.c
ccache.h
execute.c
hash.c
stats.c
util.c

index ef29edaef7e6e1312ac8571bccc49b1a7e3a9b8a..f644b750c6311ec372c6f3090249be2cc44bd31d 100644 (file)
--- 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");
        }
 }
 
index f81ff5734dabd5b9ae561f84e5a5a4bb11be3fda..9b5f86c49f4c6efb0268ee302948c8163941a21a 100644 (file)
--- 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);
index 37252d623befce0435164dbf5d80a4ce42f54f16..3f936ea33b0c31871dae6cdf40600a098fc7f3f5 100644 (file)
--- 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 eddee854caa8e84277a747f4bd70c025d64000ea..4e6552780ef7ad1f16fb85c6991134f4617ba904 100644 (file)
--- 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 d2ed9cc2eee4086c5d5bd7ba56c1dc20f9d329e5..d8317960dd647f9200c2a6e97f10ce384461242d 100644 (file)
--- a/stats.c
+++ b/stats.c
@@ -85,13 +85,13 @@ static void write_stats(int fd, unsigned counters[STATS_END])
 
        for (i=0;i<STATS_END;i++) {
                len += snprintf(buf+len, sizeof(buf)-(len+1), "%u ", counters[i]);
-               if (len >= (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 f8d619a241d79683ffe9b94a7fe54989f199f7b8..a15492bcb6114681d0f055f819ddd1f514b8e50b 100644 (file)
--- 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;
 }