aboutsummaryrefslogtreecommitdiffstats
path: root/src/verbose.c
blob: e0f951001d8e3619d94f1b013a0172749f4edd5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
 Copyright (C) 2015-2020 "IoT.bzh"

 author: José Bollo <jose.bollo@iot.bzh>

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
*/

#include <stdio.h>
#include <stdarg.h>

#include "verbose.h"

#define MASKOF(x)		(1 << (x))

#if !defined(DEFAULT_LOGLEVEL)
# define DEFAULT_LOGLEVEL	Log_Level_Warning
#endif

#if !defined(DEFAULT_LOGMASK)
# define DEFAULT_LOGMASK	(MASKOF((DEFAULT_LOGLEVEL) + 1) - 1)
#endif

#if !defined(MINIMAL_LOGLEVEL)
# define MINIMAL_LOGLEVEL	Log_Level_Error
#endif

#if !defined(MINIMAL_LOGMASK)
# define MINIMAL_LOGMASK	(MASKOF((MINIMAL_LOGLEVEL) + 1) - 1)
#endif

static const char *names[] = {
	"emergency",
	"alert",
	"critical",
	"error",
	"warning",
	"notice",
	"info",
	"debug"
};

static const char asort[] = { 1, 2, 7, 0, 3, 6, 5, 4 };

int logmask = DEFAULT_LOGMASK | MINIMAL_LOGMASK;

void (*verbose_observer)(int loglevel, const char *file, int line, const char *function, const char *fmt, va_list args);

#define CROP_LOGLEVEL(x) \
	((x) < Log_Level_Emergency ? Log_Level_Emergency \
	                           : (x) > Log_Level_Debug ? Log_Level_Debug : (x))

#if defined(VERBOSE_WITH_SYSLOG)

#include <syslog.h>

static void _vverbose_(int loglevel, const char *file, int line, const char *function, const char *fmt, va_list args)
{
	char *p;

	if (file == NULL || vasprintf(&p, fmt, args) < 0)
		vsyslog(loglevel, fmt, args);
	else {
		syslog(CROP_LOGLEVEL(loglevel), "%s [%s:%d, %s]", p, file, line, function?:"?");
		free(p);
	}
}

void verbose_set_name(const char *name, int authority)
{
	openlog(name, LOG_PERROR, authority ? LOG_AUTH : LOG_USER);
}

#elif defined(VERBOSE_WITH_SYSTEMD)

#define SD_JOURNAL_SUPPRESS_LOCATION

#include <systemd/sd-journal.h>

static const char *appname;

static int appauthority;

static void _vverbose_(int loglevel, const char *file, int line, const char *function, const char *fmt, va_list args)
{
	char lino[20];

	if (file == NULL) {
		sd_journal_printv(loglevel, fmt, args);
	} else {
		sprintf(lino, "%d", line);
		sd_journal_printv_with_location(loglevel, file, lino, function, fmt, args);
	}
}

void verbose_set_name(const char *name, int authority)
{
	appname = name;
	appauthority = authority;
}

#else

#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/uio.h>
#include <pthread.h>

static const char *appname;

static int appauthority;

static const char *prefixes[] = {
	"<0> EMERGENCY",
	"<1> ALERT",
	"<2> CRITICAL",
	"<3> ERROR",
	"<4> WARNING",
	"<5> NOTICE",
	"<6> INFO",
	"<7> DEBUG"
};

static const char *prefixes_colorized[] = {
	"<0> " COLOR_EMERGENCY	"EMERGENCY"	COLOR_DEFAULT,
	"<1> " COLOR_ALERT	"ALERT"		COLOR_DEFAULT,
	"<2> " COLOR_CRITICAL	"CRITICAL"	COLOR_DEFAULT,
	"<3> " COLOR_ERROR	"ERROR"		COLOR_DEFAULT,
	"<4> " COLOR_WARNING	"WARNING"	COLOR_DEFAULT,
	"<5> " COLOR_NOTICE	"NOTICE"	COLOR_DEFAULT,
	"<6> " COLOR_INFO	"INFO"		COLOR_DEFAULT,
	"<7> " COLOR_DEBUG	"DEBUG"		COLOR_DEFAULT
};

static int colorize = 0;

static int tty;

static const char chars[] = { '\n', '?', ':', ' ', '[', ',', ']' };

static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

static void _vverbose_(int loglevel, const char *file, int line, const char *function, const char *fmt, va_list args)
{
	char buffer[4000];
	char lino[40];
	int saverr, n, rc;
	struct iovec iov[20];

	/* save errno */
	saverr = errno;

	/* check if tty (2) or not (1) */
	if (!tty)
		tty = 1 + isatty(STDERR_FILENO);

	/* prefix */
	if (colorize)
		iov[0].iov_base = (void*)prefixes_colorized[CROP_LOGLEVEL(loglevel)] + (tty - 1 ? 4 : 0);
	else
		iov[0].iov_base = (void*)prefixes[CROP_LOGLEVEL(loglevel)] + (tty - 1 ? 4 : 0);
	iov[0].iov_len = strlen(iov[0].iov_base);

	/* " " */
	iov[1].iov_base = (void*)&chars[2];
	iov[1].iov_len = 2;

	n = 2;
	if (fmt) {
		iov[n].iov_base = buffer;
		errno = saverr;
		rc = vsnprintf(buffer, sizeof buffer, fmt, args);
		if (rc < 0)
			rc = 0;
		else if ((size_t)rc > sizeof buffer) {
			/* if too long, ellipsis the end with ... */
			rc = (int)sizeof buffer;
			buffer[rc - 1] = buffer[rc - 2]  = buffer[rc - 3] = '.';
		}
		iov[n++].iov_len = (size_t)rc;
	}
	if (file && (!fmt || tty == 1 || loglevel <= Log_Level_Warning)) {

		if (colorize)
		{
			iov[n].iov_base = (void*)COLOR_FILE;
			iov[n++].iov_len = strlen(COLOR_FILE);
		}

		/* "[" (!fmt) or " [" (fmt) */
		iov[n].iov_base = (void*)&chars[3 + !fmt];
		iov[n++].iov_len = 2 - !fmt;
		/* file */
		iov[n].iov_base = (void*)file;
		iov[n++].iov_len = strlen(file);
		/* ":" */
		iov[n].iov_base = (void*)&chars[2];
		iov[n++].iov_len = 1;
		if (line) {
			/* line number */
			iov[n].iov_base = lino;
			iov[n++].iov_len = snprintf(lino, sizeof lino, "%d", line);
		} else {
			/* "?" */
			iov[n].iov_base = (void*)&chars[1];
			iov[n++].iov_len = 1;
		}
		/* "," */
		iov[n].iov_base = (void*)&chars[5];
		iov[n++].iov_len = 1;
		if (function) {
			/* function name */
			iov[n].iov_base = (void*)function;
			iov[n++].iov_len = strlen(function);
		} else {
			/* "?" */
			iov[n].iov_base = (void*)&chars[1];
			iov[n++].iov_len = 1;
		}
		iov[n].iov_base = (void*)&chars[6];
		iov[n++].iov_len = 1;

		if (colorize)
		{
			iov[n].iov_base = (void*)COLOR_DEFAULT;
			iov[n++].iov_len = strlen(COLOR_DEFAULT);
		}
	}
	if (n == 2) {
		/* "?" */
		iov[n].iov_base = (void*)&chars[1];
		iov[n++].iov_len = 1;
	}
	/* "\n" */
	iov[n].iov_base = (void*)&chars[0];
	iov[n++].iov_len = 1;

	/* emit the message */
	pthread_mutex_lock(&mutex);
	writev(STDERR_FILENO, iov, n);
	pthread_mutex_unlock(&mutex);

	/* restore errno */
	errno = saverr;
}

void verbose_set_name(const char *name, int authority)
{
	appname = name;
	appauthority = authority;
}

#endif

void vverbose(int loglevel, const char *file, int line, const char *function, const char *fmt, va_list args)
{
	void (*observer)(int loglevel, const char *file, int line, const char *function, const char *fmt, va_list args) = verbose_observer;

	if (!observer)
		_vverbose_(loglevel, file, line, function, fmt, args);
	else {
		va_list ap;
		va_copy(ap, args);
		_vverbose_(loglevel, file, line, function, fmt, args);
		observer(loglevel, file, line, function, fmt, ap);
		va_end(ap);
	}
}

void verbose(int loglevel, const char *file, int line, const char *function, const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	vverbose(loglevel, file, line, function, fmt, ap);
	va_end(ap);
}

void set_logmask(int lvl)
{
	logmask = lvl | MINIMAL_LOGMASK;
}

void verbose_add(int level)
{
	set_logmask(logmask | MASKOF(level));
}

void verbose_sub(int level)
{
	set_logmask(logmask & ~MASKOF(level));
}

void verbose_clear()
{
	set_logmask(0);
}

void verbose_dec()
{
	verbosity_set(verbosity_get() - 1);
}

void verbose_inc()
{
	verbosity_set(verbosity_get() + 1);
}

int verbosity_to_mask(int verbo)
{
	int x = verbo + Log_Level_Error;
	int l = CROP_LOGLEVEL(x);
	return (1 << (l + 1)) - 1;
}

int verbosity_from_mask(int mask)
{
	int v = 0;
	while (mask > verbosity_to_mask(v))
		v++;
	return v;
}

void verbosity_set(int verbo)
{
	set_logmask(verbosity_to_mask(verbo));
}

int verbosity_get()
{
	return verbosity_from_mask(logmask);
}

int verbose_level_of_name(const char *name)
{
	int c, i, r, l = 0, u = sizeof names / sizeof * names;
	while (l < u) {
		i = (l + u) >> 1;
		r = (int)asort[i];
		c = strcasecmp(names[r], name);
		if (!c)
			return r;
		if (c < 0)
			l = i + 1;
		else
			u = i;
	}
	return -1;
}

const char *verbose_name_of_level(int level)
{
	return level == CROP_LOGLEVEL(level) ? names[level] : NULL;
}

void verbose_colorize()
{
	colorize = 1;
}

int verbose_is_colorized()
{
	return colorize;
}