depmod: fix tabs in help output
[module-init-tools.git] / logging.c
blob43302696b4e39b28b66158ea1ccecbe6c75f4396
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 /* Do we want informative messages as well as errors? */
17 int verbose = 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 va_start(arglist, fmt);
47 if (!quiet)
48 message("WARNING: ", fmt, &arglist);
49 va_end(arglist);
52 void error(const char *fmt, ...)
54 va_list arglist;
55 va_start(arglist, fmt);
56 message("ERROR: ", fmt, &arglist);
57 va_end(arglist);
60 void fatal(const char *fmt, ...)
62 va_list arglist;
63 va_start(arglist, fmt);
64 message("FATAL: ", fmt, &arglist);
65 va_end(arglist);
66 exit(1);
69 /* If we don't flush, then child processes print before we do */
70 void info(const char *fmt, ...)
72 va_list arglist;
73 va_start(arglist, fmt);
74 if (verbose) {
75 vfprintf(stdout, fmt, arglist);
76 fflush(stdout);
78 va_end(arglist);