Line data Source code
1 : /*
2 : SSSD
3 :
4 : sss_log.c
5 :
6 : Authors:
7 : Stephen Gallagher <sgallagh@redhat.com>
8 :
9 : Copyright (C) 2010 Red Hat
10 :
11 : This program is free software; you can redistribute it and/or modify
12 : it under the terms of the GNU General Public License as published by
13 : the Free Software Foundation; either version 3 of the License, or
14 : (at your option) any later version.
15 :
16 : This program is distributed in the hope that it will be useful,
17 : but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : GNU General Public License for more details.
20 :
21 : You should have received a copy of the GNU General Public License
22 : along with this program. If not, see <http://www.gnu.org/licenses/>.
23 : */
24 :
25 : #include "util/util.h"
26 :
27 : #ifdef WITH_JOURNALD
28 : #include <systemd/sd-journal.h>
29 : #else /* WITH_JOURNALD */
30 : #include <syslog.h>
31 : #endif /* WITH_JOURNALD */
32 :
33 2 : static int sss_to_syslog(int priority)
34 : {
35 2 : switch(priority) {
36 : case SSS_LOG_EMERG:
37 0 : return LOG_EMERG;
38 : case SSS_LOG_ALERT:
39 0 : return LOG_ALERT;
40 : case SSS_LOG_CRIT:
41 1 : return LOG_CRIT;
42 : case SSS_LOG_ERR:
43 0 : return LOG_ERR;
44 : case SSS_LOG_WARNING:
45 1 : return LOG_WARNING;
46 : case SSS_LOG_NOTICE:
47 0 : return LOG_NOTICE;
48 : case SSS_LOG_INFO:
49 0 : return LOG_INFO;
50 : case SSS_LOG_DEBUG:
51 0 : return LOG_DEBUG;
52 : default:
53 : /* If we've been passed an invalid priority, it's
54 : * best to assume it's an emergency.
55 : */
56 0 : return LOG_EMERG;
57 : }
58 : }
59 :
60 : static void sss_log_internal(int priority, int facility, const char *format,
61 : va_list ap);
62 :
63 2 : void sss_log(int priority, const char *format, ...)
64 : {
65 : va_list ap;
66 :
67 2 : va_start(ap, format);
68 2 : sss_log_internal(priority, LOG_DAEMON, format, ap);
69 2 : va_end(ap);
70 2 : }
71 :
72 0 : void sss_log_ext(int priority, int facility, const char *format, ...)
73 : {
74 : va_list ap;
75 :
76 0 : va_start(ap, format);
77 0 : sss_log_internal(priority, facility, format, ap);
78 0 : va_end(ap);
79 0 : }
80 :
81 :
82 :
83 : #ifdef WITH_JOURNALD
84 :
85 : static void sss_log_internal(int priority, int facility, const char *format,
86 : va_list ap)
87 : {
88 : int syslog_priority;
89 : int ret;
90 : char *message;
91 : const char *domain;
92 :
93 : ret = vasprintf(&message, format, ap);
94 :
95 : if (ret == -1) {
96 : /* ENOMEM */
97 : return;
98 : }
99 :
100 : domain = getenv(SSS_DOM_ENV);
101 : if (domain == NULL) {
102 : domain = "";
103 : }
104 :
105 : syslog_priority = sss_to_syslog(priority);
106 : sd_journal_send("MESSAGE=%s", message,
107 : "SSSD_DOMAIN=%s", domain,
108 : "PRIORITY=%i", syslog_priority,
109 : "SYSLOG_FACILITY=%i", LOG_FAC(facility),
110 : "SYSLOG_IDENTIFIER=%s", debug_prg_name,
111 : NULL);
112 :
113 : free(message);
114 : }
115 :
116 : #else /* WITH_JOURNALD */
117 :
118 2 : static void sss_log_internal(int priority, int facility, const char *format,
119 : va_list ap)
120 : {
121 : int syslog_priority;
122 :
123 2 : syslog_priority = sss_to_syslog(priority);
124 :
125 2 : openlog(debug_prg_name, 0, facility);
126 :
127 2 : vsyslog(syslog_priority, format, ap);
128 :
129 2 : closelog();
130 2 : }
131 :
132 : #endif /* WITH_JOURNALD */
|