Log fatal messages both to stderr and log file

This commit is contained in:
Joel Rosdahl 2009-11-12 19:58:09 +01:00
parent 90835e650f
commit 26d779b300
6 changed files with 36 additions and 20 deletions

View File

@ -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");
}
}

View File

@ -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);

View File

@ -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)) {

3
hash.c
View File

@ -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) {

View File

@ -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");
}

37
util.c
View File

@ -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;
}