-
Notifications
You must be signed in to change notification settings - Fork 11
/
logging.c
79 lines (64 loc) · 1.36 KB
/
logging.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
#define _GNU_SOURCE /* asprintf */
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <syslog.h>
#include "logging.h"
/* Do we use syslog for messages or stderr? */
int logging = 0;
/* Do we want to silently drop all warnings? */
int quiet = 0;
/* Do we want informative messages as well as errors? */
int verbose = 0;
void message(const char *prefix, const char *fmt, va_list *arglist)
{
int ret;
char *buf, *buf2;
ret = vasprintf(&buf, fmt, *arglist);
if (ret >= 0)
ret = asprintf(&buf2, "%s%s", prefix, buf);
if (ret < 0)
buf2 = "FATAL: Out of memory.\n";
if (logging)
syslog(LOG_NOTICE, "%s", buf2);
else
fprintf(stderr, "%s", buf2);
if (ret < 0)
exit(1);
free(buf2);
free(buf);
}
void warn(const char *fmt, ...)
{
va_list arglist;
va_start(arglist, fmt);
if (!quiet)
message("WARNING: ", fmt, &arglist);
va_end(arglist);
}
void error(const char *fmt, ...)
{
va_list arglist;
va_start(arglist, fmt);
message("ERROR: ", fmt, &arglist);
va_end(arglist);
}
void fatal(const char *fmt, ...)
{
va_list arglist;
va_start(arglist, fmt);
message("FATAL: ", fmt, &arglist);
va_end(arglist);
exit(1);
}
/* If we don't flush, then child processes print before we do */
void info(const char *fmt, ...)
{
va_list arglist;
va_start(arglist, fmt);
if (verbose) {
vfprintf(stdout, fmt, arglist);
fflush(stdout);
}
va_end(arglist);
}