Fix bad error reporting
[src/app-framework-binder.git] / src / verbose.c
1 /*
2  Copyright (C) 2016, 2017 "IoT.bzh"
3
4  author: José Bollo <jose.bollo@iot.bzh>
5
6  Licensed under the Apache License, Version 2.0 (the "License");
7  you may not use this file except in compliance with the License.
8  You may obtain a copy of the License at
9
10      http://www.apache.org/licenses/LICENSE-2.0
11
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  See the License for the specific language governing permissions and
16  limitations under the License.
17 */
18
19 #include <stdio.h>
20 #include <stdarg.h>
21
22 #include "verbose.h"
23
24 int verbosity = 1;
25
26 #define LEVEL(x) ((x) < 0 ? 0 : (x) > 7 ? 7 : (x))
27
28 #if defined(VERBOSE_WITH_SYSLOG)
29
30 #include <syslog.h>
31
32 void vverbose(int level, const char *file, int line, const char *function, const char *fmt, va_list args)
33 {
34         char *p;
35
36         if (file == NULL || vasprintf(&p, fmt, args) < 0)
37                 vsyslog(level, fmt, args);
38         else {
39                 syslog(LEVEL(level), "%s [%s:%d, function]", p, file, line, function);
40                 free(p);
41         }
42 }
43
44 void verbose_set_name(const char *name, int authority)
45 {
46         openlog(name, LOG_PERROR, authority ? LOG_AUTH : LOG_USER);
47 }
48
49 #elif defined(VERBOSE_WITH_SYSTEMD)
50
51 #define SD_JOURNAL_SUPPRESS_LOCATION
52
53 #include <systemd/sd-journal.h>
54
55 static const char *appname;
56
57 static int appauthority;
58
59 void vverbose(int level, const char *file, int line, const char *function, const char *fmt, va_list args)
60 {
61         char lino[20];
62
63         if (file == NULL) {
64                 sd_journal_printv(level, fmt, args);
65         } else {
66                 sprintf(lino, "%d", line);
67                 sd_journal_printv_with_location(level, file, lino, function, fmt, args);
68         }
69 }
70
71 void verbose_set_name(const char *name, int authority)
72 {
73         appname = name;
74         appauthority = authority;
75 }
76
77 #else
78
79 #include <unistd.h>
80 #include <errno.h>
81
82 static const char *appname;
83
84 static int appauthority;
85
86 static const char *prefixes[] = {
87         "<0> EMERGENCY",
88         "<1> ALERT",
89         "<2> CRITICAL",
90         "<3> ERROR",
91         "<4> WARNING",
92         "<5> NOTICE",
93         "<6> INFO",
94         "<7> DEBUG"
95 };
96
97 void vverbose(int level, const char *file, int line, const char *function, const char *fmt, va_list args)
98 {
99         int saverr = errno;
100         int tty = isatty(fileno(stderr));
101         errno = saverr;
102
103         fprintf(stderr, "%s: ", prefixes[LEVEL(level)] + (tty ? 4 : 0));
104         vfprintf(stderr, fmt, args);
105         if (file != NULL && (!tty || verbosity > 2))
106                 fprintf(stderr, " [%s:%d,%s]\n", file, line, function);
107         else
108                 fprintf(stderr, "\n");
109 }
110
111 void verbose_set_name(const char *name, int authority)
112 {
113         appname = name;
114         appauthority = authority;
115 }
116
117 #endif
118
119 void verbose(int level, const char *file, int line, const char *function, const char *fmt, ...)
120 {
121         va_list ap;
122
123         va_start(ap, fmt);
124         vverbose(level, file, line, function, fmt, ap);
125         va_end(ap);
126 }
127