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.
14 * $FreeBSD: src/usr.sbin/ctm/ctm_rmail/error.c,v 1.2.12.1 2001/07/05 07:47:00 kris Exp $
15 * $DragonFly: src/usr.sbin/ctm/ctm_rmail/Attic/error.c,v 1.2 2003/06/17 04:29:53 dillon Exp $
25 static FILE *error_fp = NULL;
26 static char *prog = NULL;
30 * Log errors to the given file.
33 err_set_log(char *log_file)
37 if ((fp = fopen(log_file, "a")) == NULL)
38 err("cannot log to '%s'", log_file);
45 * Set the error prefix if not logging to a file.
48 err_prog_name(char *name)
50 if ((prog = strrchr(name, '/')) == NULL)
60 * A leading '*' in the message format means we want the system errno
61 * decoded and appended.
64 err(const char *fmt, ...)
73 if ((fp = error_fp) == NULL)
77 fprintf(fp, "%s: ", prog);
83 fprintf(fp, "%04d-%02d-%02d %02d:%02d ", tm->tm_year+1900,
84 tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min);
92 vfprintf(fp, fmt, ap);
96 fprintf(fp, ": %s", strerror(x));