Deleted Added
full compact
error.c (6081) error.c (6695)
1/*
2 * Routines for logging error messages or other informative messages.
3 *
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.
7 *
8 * Author: Stephen McKay
9 *
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.
13 */
14
1#include <stdio.h>
2#include <string.h>
3#include <stdarg.h>
4#include <time.h>
15#include <stdio.h>
16#include <string.h>
17#include <stdarg.h>
18#include <time.h>
19#include <errno.h>
5#include "error.h"
6
7static FILE *error_fp = NULL;
8static char *prog = NULL;
9
10
11/*
12 * Log errors to the given file.

--- 20 unchanged lines hidden (view full) ---

33 prog = name;
34 else
35 prog++;
36 }
37
38
39/*
40 * Log an error.
20#include "error.h"
21
22static FILE *error_fp = NULL;
23static char *prog = NULL;
24
25
26/*
27 * Log errors to the given file.

--- 20 unchanged lines hidden (view full) ---

48 prog = name;
49 else
50 prog++;
51 }
52
53
54/*
55 * Log an error.
56 *
57 * A leading '*' in the message format means we want the system errno
58 * decoded and appended.
41 */
42void
43err(char *fmt, ...)
44 {
45 va_list ap;
46 time_t now;
47 struct tm *tm;
48 FILE *fp;
59 */
60void
61err(char *fmt, ...)
62 {
63 va_list ap;
64 time_t now;
65 struct tm *tm;
66 FILE *fp;
67 int x = errno;
68 int want_errno;
49
50 if ((fp = error_fp) == NULL)
51 {
52 fp = stderr;
53 if (prog != NULL)
54 fprintf(fp, "%s: ", prog);
55 }
56 else
57 {
58 time(&now);
59 tm = localtime(&now);
60 fprintf(fp, "%04d-%02d-%02d %02d:%02d ", tm->tm_year+1900,
61 tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min);
62 }
63
69
70 if ((fp = error_fp) == NULL)
71 {
72 fp = stderr;
73 if (prog != NULL)
74 fprintf(fp, "%s: ", prog);
75 }
76 else
77 {
78 time(&now);
79 tm = localtime(&now);
80 fprintf(fp, "%04d-%02d-%02d %02d:%02d ", tm->tm_year+1900,
81 tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min);
82 }
83
84 want_errno = 0;
85 if (*fmt == '*')
86 want_errno++, fmt++;
87
64 va_start(ap, fmt);
65 vfprintf(fp, fmt, ap);
66 va_end(ap);
67
88 va_start(ap, fmt);
89 vfprintf(fp, fmt, ap);
90 va_end(ap);
91
92 if (want_errno)
93 fprintf(fp, ": %s", strerror(x));
94
68 fprintf(fp, "\n");
69 fflush(fp);
70 }
95 fprintf(fp, "\n");
96 fflush(fp);
97 }