docs: don't forget to pass -I m4 to aclocal
[mit.git] / logging.c
blob92f76835ab3cbea1fad275e4698f86c3638be301
1 #define _GNU_SOURCE /* asprintf */
3 #include <stdlib.h>
4 #include <stdarg.h>
5 #include <stdio.h>
6 #include <syslog.h>
8 #include "logging.h"
10 /* Do we use syslog for messages or stderr? */
11 int logging = 0;
13 /* Do we want to silently drop all warnings? */
14 int quiet = 0;
16 /* Number of times warn() has been called */
17 int warned = 0;
19 void message(const char *prefix, const char *fmt, va_list *arglist)
21 int ret;
22 char *buf, *buf2;
24 ret = vasprintf(&buf, fmt, *arglist);
25 if (ret >= 0)
26 ret = asprintf(&buf2, "%s%s", prefix, buf);
28 if (ret < 0)
29 buf2 = "FATAL: Out of memory.\n";
31 if (logging)
32 syslog(LOG_NOTICE, "%s", buf2);
33 else
34 fprintf(stderr, "%s", buf2);
36 if (ret < 0)
37 exit(1);
39 free(buf2);
40 free(buf);
43 void warn(const char *fmt, ...)
45 va_list arglist;
46 warned++;
47 va_start(arglist, fmt);
48 if (!quiet)
49 message("WARNING: ", fmt, &arglist);
50 va_end(arglist);
53 void fatal(const char *fmt, ...)
55 va_list arglist;
56 va_start(arglist, fmt);
57 message("FATAL: ", fmt, &arglist);
58 va_end(arglist);
59 exit(1);