2 Copyright (C) 2016, 2017 "IoT.bzh"
4 author: José Bollo <jose.bollo@iot.bzh>
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
10 http://www.apache.org/licenses/LICENSE-2.0
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.
26 #define LEVEL(x) ((x) < 0 ? 0 : (x) > 7 ? 7 : (x))
28 #if defined(VERBOSE_WITH_SYSLOG)
32 void vverbose(int level, const char *file, int line, const char *function, const char *fmt, va_list args)
36 if (file == NULL || vasprintf(&p, fmt, args) < 0)
37 vsyslog(level, fmt, args);
39 syslog(LEVEL(level), "%s [%s:%d, function]", p, file, line, function);
44 void verbose_set_name(const char *name, int authority)
46 openlog(name, LOG_PERROR, authority ? LOG_AUTH : LOG_USER);
49 #elif defined(VERBOSE_WITH_SYSTEMD)
51 #define SD_JOURNAL_SUPPRESS_LOCATION
53 #include <systemd/sd-journal.h>
55 static const char *appname;
57 static int appauthority;
59 void vverbose(int level, const char *file, int line, const char *function, const char *fmt, va_list args)
64 sd_journal_printv(level, fmt, args);
66 sprintf(lino, "%d", line);
67 sd_journal_printv_with_location(level, file, lino, function, fmt, args);
71 void verbose_set_name(const char *name, int authority)
74 appauthority = authority;
81 static const char *appname;
83 static int appauthority;
85 static const char *prefixes[] = {
96 void vverbose(int level, const char *file, int line, const char *function, const char *fmt, va_list args)
98 int tty = isatty(fileno(stderr));
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);
105 fprintf(stderr, "\n");
108 void verbose_set_name(const char *name, int authority)
111 appauthority = authority;
116 void verbose(int level, const char *file, int line, const char *function, const char *fmt, ...)
121 vverbose(level, file, line, function, fmt, ap);