summaryrefslogtreecommitdiffstats
path: root/liblog_use_syslog.c
blob: 09b6ca9411cf7f46682f9224a8d588482224d32c (plain) (blame)
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
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST

#if defined(__clang__)
# pragma clang diagnostic ignored "-Wassign-enum"
#endif

int
liblog_use_syslog(struct liblog_context *ctx, const char *prefixfmt, int use_backtrace)
{
	void *new;
	size_t old_noutputs = ctx->noutputs;
	struct liblog_output output = {
		.userdata = NULL,
		.prefixfmt = prefixfmt,
		.lowest_verbosity_unlimited = 1U,
		.highest_verbosity_unlimited = 0U,
		.use_backtrace = (use_backtrace ? 1U : 0U),
		.lowest_verbosity = 0,
		.highest_verbosity = LIBLOG_ALERT - 1,
		.sink_type = LIBLOG_SYSLOG,
		.sink.syslog.level = LOG_EMERG
	};

	if (!ctx) {
		errno = EINVAL;
		return -1;
	}

	if (ctx->noutputs > SIZE_MAX / sizeof(*ctx->outputs) - 8)
		goto enomem;
	new = realloc(ctx->outputs, (ctx->noutputs + 8) * sizeof(*ctx->outputs));
	if (!new) {
	enomem:
		errno = ENOMEM;
		return -1;
	}
	ctx->outputs = new;

	if (liblog_use_output(ctx, &output))
		goto fail;

	output.lowest_verbosity_unlimited = 0;
	output.lowest_verbosity = output.highest_verbosity + 1;
	output.highest_verbosity = LIBLOG_CRITICAL - 1;
	output.sink.syslog.level = LOG_ALERT;
	if (liblog_use_output(ctx, &output))
		goto fail;

	output.lowest_verbosity = output.highest_verbosity + 1;
	output.highest_verbosity = LIBLOG_ERROR - 1;
	output.sink.syslog.level = LOG_CRIT;
	if (liblog_use_output(ctx, &output))
		goto fail;

	output.lowest_verbosity = output.highest_verbosity + 1;
	output.highest_verbosity = LIBLOG_WARNING - 1;
	output.sink.syslog.level = LOG_ERR;
	if (liblog_use_output(ctx, &output))
		goto fail;

	output.lowest_verbosity = output.highest_verbosity + 1;
	output.highest_verbosity = LIBLOG_NOTICE - 1;
	output.sink.syslog.level = LOG_WARNING;
	if (liblog_use_output(ctx, &output))
		goto fail;

	output.lowest_verbosity = output.highest_verbosity + 1;
	output.highest_verbosity = LIBLOG_INFO - 1;
	output.sink.syslog.level = LOG_NOTICE;
	if (liblog_use_output(ctx, &output))
		goto fail;

	output.lowest_verbosity = output.highest_verbosity + 1;
	output.highest_verbosity = LIBLOG_TRACE - 1;
	output.sink.syslog.level = LOG_INFO;
	if (liblog_use_output(ctx, &output))
		goto fail;

	output.lowest_verbosity = output.highest_verbosity + 1;
	output.highest_verbosity_unlimited = 1;
	output.sink.syslog.level = LOG_DEBUG;
	if (liblog_use_output(ctx, &output))
		goto fail;

	return 0;

fail:
	while (ctx->noutputs > old_noutputs)
		liblog_destroy_output(&ctx->outputs[--ctx->noutputs]);
	return -1;
}

#else

int main(void) {return 0;} /* TODO test */

#endif