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