2 * Routines for logging error messages or other informative messages.
4 * Log messages can easily contain the program name, a time stamp, system
5 * error messages, and arbitrary printf-style strings, and can be directed
6 * to stderr or a log file.
8 * Author: Stephen McKay
10 * NOTICE: This is free software. I hope you get some use from this program.
11 * In return you should think about all the nice people who give away software.
12 * Maybe you should write some free software too.
16 static const char rcsid[] =
27 static FILE *error_fp = NULL;
28 static char *prog = NULL;
32 * Log errors to the given file.
35 err_set_log(char *log_file)
39 if ((fp = fopen(log_file, "a")) == NULL)
40 err("cannot log to '%s'", log_file);
47 * Set the error prefix if not logging to a file.
50 err_prog_name(char *name)
52 if ((prog = strrchr(name, '/')) == NULL)
62 * A leading '*' in the message format means we want the system errno
63 * decoded and appended.
66 err(const char *fmt, ...)
75 if ((fp = error_fp) == NULL)
79 fprintf(fp, "%s: ", prog);
85 fprintf(fp, "%04d-%02d-%02d %02d:%02d ", tm->tm_year+1900,
86 tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min);
94 vfprintf(fp, fmt, ap);
98 fprintf(fp, ": %s", strerror(x));