-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.c
134 lines (102 loc) · 2.93 KB
/
logger.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* Routines for logging and error messages */
#include <stdio.h>
#include <err.h>
#include <sysexits.h>
#include <errno.h>
#include <time.h>
#include <stdarg.h>
#include <string.h>
#include "export.h"
EXPORT FILE *logfile = NULL;
/* vlogger -- write a log message to the log file, if any */
EXPORT void vlogger(const char *message, va_list ap)
{
char nowstring[26]; /* YYYY-MM-DD HH:MM:SS±zzzz (RFC 3339) */
time_t now;
if (logfile) {
now = time(NULL);
strftime(nowstring, sizeof(nowstring) - 1, "%F %T%z", localtime(&now));
fprintf(logfile, "%s ", nowstring);
vfprintf(logfile, message, ap);
fprintf(logfile, "\n");
}
}
/* logger -- write a log message to the log file, if any */
EXPORT void logger(const char *message,...)
{
va_list ap;
va_start(ap, message);
vlogger(message, ap);
va_end(ap);
}
/* vlog_warnx -- write an error message to the log */
EXPORT void vlog_warnx(const char *fmt, va_list ap)
{
char message[2048];
int n = 0;
if (logfile != stdout) {
n += snprintf(message + n, sizeof(message) - n, "Error");
if (fmt) {
n += snprintf(message + n, sizeof(message) - n, ": ");
n += vsnprintf(message + n, sizeof(message) - n, fmt, ap);
}
logger("%s", message);
}
}
/* vlog_warn -- write an error message and strerror(errno) to the log */
EXPORT void vlog_warn(const char *fmt, va_list ap)
{
char message[2048];
int n = 0;
if (logfile != stdout) {
n += snprintf(message + n, sizeof(message) - n, "Error");
if (fmt) {
n += snprintf(message + n, sizeof(message) - n, ": ");
n += vsnprintf(message + n, sizeof(message) - n, fmt, ap);
}
n += snprintf(message + n, sizeof(message) - n, ": %s", strerror(errno));
logger("%s", message);
}
}
/* log_warnx -- write an error message to the log */
EXPORT void log_warnx(const char *fmt,...)
{
va_list ap;
va_start(ap, fmt);
vlog_warnx(fmt, ap);
va_end(ap);
}
/* log_warn -- write an error message and strerror(errno) to the log */
EXPORT void log_warn(const char *fmt,...)
{
va_list ap;
va_start(ap, fmt);
vlog_warn(fmt, ap);
va_end(ap);
}
/* vlog_errx -- write an error message to the log and to stderr, then exit */
EXPORT void vlog_errx(int eval, const char *fmt, va_list ap)
{
vlog_warnx(fmt, ap);
verr(eval, fmt, ap); /* Does not return */
}
/* vlog_err -- write an error message to the log and to stderr, then exit */
EXPORT void vlog_err(int eval, const char *fmt, va_list ap)
{
vlog_warn(fmt, ap);
verr(eval, fmt, ap); /* Does not return */
}
/* log_errx -- write an error message to the log and to stderr, then exit */
EXPORT void log_errx(int eval, const char *fmt,...)
{
va_list ap;
va_start(ap, fmt);
vlog_errx(eval, fmt, ap); /* Does not return */
}
/* log_err -- write an error message to the log and to stderr, then exit */
EXPORT void log_err(int eval, const char *fmt,...)
{
va_list ap;
va_start(ap, fmt);
vlog_err(eval, fmt, ap); /* Does not return */
}