depmod: fix return in sort_modules
[mit.git] / logging.c
blob22de2871341dd68bb51ee6904bee4da88f3ec975
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 void message(const char *prefix, const char *fmt, va_list *arglist)
15 int ret;
16 char *buf, *buf2;
18 ret = vasprintf(&buf, fmt, *arglist);
19 if (ret >= 0)
20 ret = asprintf(&buf2, "%s%s", prefix, buf);
22 if (ret < 0)
23 buf2 = "FATAL: Out of memory.\n";
25 if (logging)
26 syslog(LOG_NOTICE, "%s", buf2);
27 else
28 fprintf(stderr, "%s", buf2);
30 if (ret < 0)
31 exit(1);
33 free(buf2);
34 free(buf);
37 int warned = 0;
38 void warn(const char *fmt, ...)
40 va_list arglist;
41 warned++;
42 va_start(arglist, fmt);
43 message("WARNING: ", fmt, &arglist);
44 va_end(arglist);
47 void fatal(const char *fmt, ...)
49 va_list arglist;
50 va_start(arglist, fmt);
51 message("FATAL: ", fmt, &arglist);
52 va_end(arglist);
53 exit(1);