Merge branch 'master' of git://github.com/sourcejedi/module-init-tools
[mit.git] / logging.c
blob2582321920e5ca1e8718ef8ac556c69bfe97cc72
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 /* Do we want informative messages as well as errors? */
20 int verbose = 0;
22 void message(const char *prefix, const char *fmt, va_list *arglist)
24 int ret;
25 char *buf, *buf2;
27 ret = vasprintf(&buf, fmt, *arglist);
28 if (ret >= 0)
29 ret = asprintf(&buf2, "%s%s", prefix, buf);
31 if (ret < 0)
32 buf2 = "FATAL: Out of memory.\n";
34 if (logging)
35 syslog(LOG_NOTICE, "%s", buf2);
36 else
37 fprintf(stderr, "%s", buf2);
39 if (ret < 0)
40 exit(1);
42 free(buf2);
43 free(buf);
46 void warn(const char *fmt, ...)
48 va_list arglist;
49 warned++;
50 va_start(arglist, fmt);
51 if (!quiet)
52 message("WARNING: ", fmt, &arglist);
53 va_end(arglist);
56 void error(const char *fmt, ...)
58 va_list arglist;
59 va_start(arglist, fmt);
60 message("ERROR: ", fmt, &arglist);
61 va_end(arglist);
64 void fatal(const char *fmt, ...)
66 va_list arglist;
67 va_start(arglist, fmt);
68 message("FATAL: ", fmt, &arglist);
69 va_end(arglist);
70 exit(1);
73 /* If we don't flush, then child processes print before we do */
74 void info(const char *fmt, ...)
76 va_list arglist;
77 va_start(arglist, fmt);
78 if (verbose) {
79 vfprintf(stdout, fmt, arglist);
80 fflush(stdout);
82 va_end(arglist);