xref: /dpdk/lib/log/log_syslog.c (revision c1d145834f287aa8cf53de914618a7312f2c360e)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 
5 #include <stdbool.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <sys/types.h>
9 #include <syslog.h>
10 
11 #include <rte_common.h>
12 #include <rte_log.h>
13 
14 #include "log_internal.h"
15 #include "log_private.h"
16 
17 static int log_facility;
18 
19 /*
20  * Usable list of facilities
21  * Skip kern, mark, and security
22  */
23 static const struct {
24 	const char *name;
25 	int value;
26 } facilitys[] = {
27 	{ "auth", LOG_AUTH },
28 	{ "cron", LOG_CRON },
29 	{ "daemon", LOG_DAEMON },
30 	{ "ftp", LOG_FTP },
31 	{ "kern", LOG_KERN },
32 	{ "lpr", LOG_LPR },
33 	{ "mail", LOG_MAIL },
34 	{ "news", LOG_NEWS },
35 	{ "syslog", LOG_SYSLOG },
36 	{ "user", LOG_USER },
37 	{ "uucp", LOG_UUCP },
38 	{ "local0", LOG_LOCAL0 },
39 	{ "local1", LOG_LOCAL1 },
40 	{ "local2", LOG_LOCAL2 },
41 	{ "local3", LOG_LOCAL3 },
42 	{ "local4", LOG_LOCAL4 },
43 	{ "local5", LOG_LOCAL5 },
44 	{ "local6", LOG_LOCAL6 },
45 	{ "local7", LOG_LOCAL7 },
46 };
47 
48 int
49 eal_log_syslog(const char *name)
50 {
51 	unsigned int i;
52 
53 	if (name == NULL) {
54 		log_facility = LOG_DAEMON;
55 		return 0;
56 	}
57 
58 	for (i = 0; i < RTE_DIM(facilitys); i++) {
59 		if (!strcmp(name, facilitys[i].name)) {
60 			log_facility = facilitys[i].value;
61 			return 0;
62 		}
63 	}
64 	return -1;
65 }
66 
67 /* syslog is enabled if facility is set */
68 bool
69 log_syslog_enabled(void)
70 {
71 	return log_facility != 0; /* LOG_KERN is 0 */
72 }
73 
74 /*
75  * default log function
76  */
77 static ssize_t
78 log_syslog_write(__rte_unused void *c, const char *buf, size_t size)
79 {
80 	/* Syslog error levels are from 0 to 7, so subtract 1 to convert */
81 	syslog(rte_log_cur_msg_loglevel() - 1, "%.*s", (int)size, buf);
82 
83 	return size;
84 }
85 
86 static int
87 log_syslog_close(__rte_unused void *c)
88 {
89 	closelog();
90 	return 0;
91 }
92 
93 static cookie_io_functions_t log_syslog_func = {
94 	.write = log_syslog_write,
95 	.close = log_syslog_close,
96 };
97 
98 
99 FILE *
100 log_syslog_open(const char *id)
101 {
102 	int option = LOG_CONS | LOG_NDELAY | LOG_PID | LOG_PERROR;
103 
104 	openlog(id, option, log_facility);
105 
106 	/* redirect other log messages to syslog as well */
107 	return fopencookie(NULL, "w", log_syslog_func);
108 }
109