File: log_syslog.c

package info (click to toggle)
libqb 2.0.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,092 kB
  • sloc: ansic: 22,191; sh: 5,232; makefile: 607
file content (128 lines) | stat: -rw-r--r-- 3,042 bytes parent folder | download | duplicates (4)
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
/*
 * Copyright (C) 2011 Red Hat, Inc.
 *
 * All rights reserved.
 *
 * Author: Angus Salkeld <asalkeld@redhat.com>
 *
 * libqb is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * libqb is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with libqb.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "os_base.h"

#ifdef HAVE_SYSLOG_H
#include <syslog.h>
#endif /* HAVE_SYSLOG_H */
#ifdef USE_JOURNAL
#define SD_JOURNAL_SUPPRESS_LOCATION
#include <systemd/sd-journal.h>
#endif

#include "log_int.h"

static void
_syslog_logger(int32_t target,
	       struct qb_log_callsite *cs, struct timespec *timestamp, const char *msg)
{
	char buffer[QB_LOG_MAX_LEN];
	char *output_buffer = buffer;
	struct qb_log_target *t = qb_log_target_get(target);
	int32_t final_priority = cs->priority;

	if (final_priority > LOG_INFO) {
		/*
		 * only bump the priority if it is greater than info.
		 */
		final_priority += t->priority_bump;
	}
	if (final_priority > LOG_DEBUG) {
		return;
	}

	if (t->max_line_length > QB_LOG_MAX_LEN) {
		output_buffer = malloc(t->max_line_length);
		if (!output_buffer) {
			return;
		}
	}

	output_buffer[0] = '\0';
	qb_log_target_format(target, cs, timestamp, msg, output_buffer);

	if (final_priority < LOG_EMERG) {
		final_priority = LOG_EMERG;
	}
#ifdef USE_JOURNAL
	if (t->use_journal) {
		if (cs->message_id) {
			sd_journal_send("MESSAGE_ID=%s", cs->message_id,
				"PRIORITY=%d", final_priority,
				"CODE_LINE=%d", cs->lineno,
				"CODE_FILE=%s", cs->filename,
				"CODE_FUNC=%s", cs->function,
				"SYSLOG_IDENTIFIER=%s", t->name,
				"MESSAGE=%s", output_buffer,
				NULL);
		} else {
			sd_journal_send("PRIORITY=%d", final_priority,
				"CODE_LINE=%d", cs->lineno,
				"CODE_FILE=%s", cs->filename,
				"CODE_FUNC=%s", cs->function,
				"SYSLOG_IDENTIFIER=%s", t->name,
				"MESSAGE=%s", output_buffer,
				NULL);
		}
	} else {
#endif
		syslog(final_priority, "%s", output_buffer);
#ifdef USE_JOURNAL
	}
#endif
	if (t->max_line_length > QB_LOG_MAX_LEN) {
		free(output_buffer);
	}
}

static void
_syslog_close(int32_t target)
{
	struct qb_log_target *t = qb_log_target_get(target);

	if (!t->use_journal) {
		closelog();
	}
}

static void
_syslog_reload(int32_t target)
{
	struct qb_log_target *t = qb_log_target_get(target);

	closelog();
	if (!t->use_journal) {
		openlog(t->name, LOG_PID, t->facility);
	}
}

int32_t
qb_log_syslog_open(struct qb_log_target *t)
{
	t->logger = _syslog_logger;
	t->reload = _syslog_reload;
	t->close = _syslog_close;

	if (!t->use_journal) {
		openlog(t->name, LOG_PID, t->facility);
	}
	return 0;
}