Improve log messages
[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
81 static const char *appname;
82
83 static int appauthority;
84
85 static const char *prefixes[] = {
86         "<0> EMERGENCY",
87         "<1> ALERT",
88         "<2> CRITICAL",
89         "<3> ERROR",
90         "<4> WARNING",
91         "<5> NOTICE",
92         "<6> INFO",
93         "<7> DEBUG"
94 };
95
96 void vverbose(int level, const char *file, int line, const char *function, const char *fmt, va_list args)
97 {
98         int tty = isatty(fileno(stderr));
99
100         fprintf(stderr, "%s: ", prefixes[LEVEL(level)] + (tty ? 4 : 0));
101         vfprintf(stderr, fmt, args);
102         if (file != NULL && (!tty || verbosity >5))
103                 fprintf(stderr, " [%s:%d,%s]\n", file, line, function);
104         else
105                 fprintf(stderr, "\n");
106 }
107
108 void verbose_set_name(const char *name, int authority)
109 {
110         appname = name;
111         appauthority = authority;
112 }
113
114 #endif
115
116 void verbose(int level, const char *file, int line, const char *function, const char *fmt, ...)
117 {
118         va_list ap;
119
120         va_start(ap, fmt);
121         vverbose(level, file, line, function, fmt, ap);
122         va_end(ap);
123 }
124