chiark / gitweb /
879cb537336e8461fec1c02ea79bceb79b41eeab
[elogind.git] / log.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <errno.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <sys/socket.h>
28 #include <sys/un.h>
29
30 #include "log.h"
31 #include "util.h"
32 #include "macro.h"
33
34 #define SYSLOG_TIMEOUT_USEC (5*USEC_PER_SEC)
35 #define LOG_BUFFER_MAX 1024
36
37 static LogTarget log_target = LOG_TARGET_CONSOLE;
38 static int log_max_level = LOG_DEBUG;
39
40 static int syslog_fd = -1;
41 static int kmsg_fd = -1;
42
43 void log_close_kmsg(void) {
44
45         if (kmsg_fd >= 0) {
46                 close_nointr(kmsg_fd);
47                 kmsg_fd = -1;
48         }
49 }
50
51 int log_open_kmsg(void) {
52
53         if (log_target != LOG_TARGET_KMSG) {
54                 log_close_kmsg();
55                 return 0;
56         }
57
58         if (kmsg_fd >= 0)
59                 return 0;
60
61         if ((kmsg_fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0)
62                 return -errno;
63
64         return 0;
65 }
66
67 void log_close_syslog(void) {
68
69         if (syslog_fd >= 0) {
70                 close_nointr(syslog_fd);
71                 syslog_fd = -1;
72         }
73 }
74
75 int log_open_syslog(void) {
76         union {
77                 struct sockaddr sa;
78                 struct sockaddr_un un;
79         } sa;
80         struct timeval tv;
81         int r;
82
83         if (log_target != LOG_TARGET_SYSLOG) {
84                 log_close_syslog();
85                 return 0;
86         }
87
88         if (syslog_fd >= 0)
89                 return 0;
90
91         if ((syslog_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0)
92                 return -errno;
93
94         /* Make sure we don't block for more than 5s when talking to
95          * syslog */
96         timeval_store(&tv, SYSLOG_TIMEOUT_USEC);
97         if (setsockopt(syslog_fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0) {
98                 r = -errno;
99                 log_close_syslog();
100                 return r;
101         }
102
103         zero(sa);
104         sa.un.sun_family = AF_UNIX;
105         strncpy(sa.un.sun_path, "/dev/log", sizeof(sa.un.sun_path));
106
107         if (connect(syslog_fd, &sa.sa, sizeof(sa)) < 0) {
108                 r = -errno;
109                 log_close_syslog();
110                 return -errno;
111         }
112
113         return 0;
114 }
115
116 void log_set_target(LogTarget target) {
117         assert(target >= 0);
118         assert(target < _LOG_TARGET_MAX);
119
120         log_target = target;
121 }
122
123 void log_set_max_level(int level) {
124         assert((level & LOG_PRIMASK) == level);
125
126         log_max_level = level;
127 }
128
129 static void write_to_console(
130         int level,
131         const char*file,
132         int line,
133         const char *func,
134         const char *format,
135         va_list ap) {
136
137         const char *prefix, *suffix;
138
139         if (LOG_PRI(level) <= LOG_ERR) {
140                 prefix = "\x1B[1;31m";
141                 suffix = "\x1B[0m";
142         } else {
143                 prefix = "";
144                 suffix = "";
145         }
146
147         fprintf(stderr, "(%s:%u) %s", file, line, prefix);
148         vfprintf(stderr, format, ap);
149         fprintf(stderr, "%s\n", suffix);
150 }
151
152 static int write_to_syslog(
153         int level,
154         const char*file,
155         int line,
156         const char *func,
157         const char *format,
158         va_list ap) {
159
160         char header_priority[16], header_time[64], header_pid[16];
161         char buffer[LOG_BUFFER_MAX];
162         struct iovec iovec[5];
163         struct msghdr msghdr;
164         time_t t;
165         struct tm *tm;
166
167         if (syslog_fd < 0)
168                 return -EIO;
169
170         snprintf(header_priority, sizeof(header_priority), "<%i>", LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(level)));
171         char_array_0(header_priority);
172
173         t = (time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC);
174         if (!(tm = localtime(&t)))
175                 return -EINVAL;
176
177         if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
178                 return -EINVAL;
179
180         snprintf(header_pid, sizeof(header_pid), "[%llu]: ", (unsigned long long) getpid());
181         char_array_0(header_pid);
182
183         vsnprintf(buffer, sizeof(buffer), format, ap);
184         char_array_0(buffer);
185
186         zero(iovec);
187         IOVEC_SET_STRING(iovec[0], header_priority);
188         IOVEC_SET_STRING(iovec[1], header_time);
189         IOVEC_SET_STRING(iovec[2], __progname);
190         IOVEC_SET_STRING(iovec[3], header_pid);
191         IOVEC_SET_STRING(iovec[4], buffer);
192
193         zero(msghdr);
194         msghdr.msg_iov = iovec;
195         msghdr.msg_iovlen = ELEMENTSOF(iovec);
196
197         if (sendmsg(syslog_fd, &msghdr, 0) < 0)
198                 return -errno;
199
200         return 0;
201 }
202
203 static int write_to_kmsg(
204         int level,
205         const char*file,
206         int line,
207         const char *func,
208         const char *format,
209         va_list ap) {
210
211         char header_priority[16], header_pid[16];
212         char buffer[LOG_BUFFER_MAX];
213         struct iovec iovec[5];
214
215         if (kmsg_fd < 0)
216                 return -EIO;
217
218         snprintf(header_priority, sizeof(header_priority), "<%i>", LOG_PRI(level));
219         char_array_0(header_priority);
220
221         snprintf(header_pid, sizeof(header_pid), "[%llu]: ", (unsigned long long) getpid());
222         char_array_0(header_pid);
223
224         vsnprintf(buffer, sizeof(buffer), format, ap);
225         char_array_0(buffer);
226
227         zero(iovec);
228         IOVEC_SET_STRING(iovec[0], header_priority);
229         IOVEC_SET_STRING(iovec[1], __progname);
230         IOVEC_SET_STRING(iovec[2], header_pid);
231         IOVEC_SET_STRING(iovec[3], buffer);
232         IOVEC_SET_STRING(iovec[4], (char*) "\n");
233
234         if (writev(kmsg_fd, iovec, ELEMENTSOF(iovec)) < 0)
235                 return -errno;
236
237         return 0;
238 }
239
240 void log_meta(
241         int level,
242         const char*file,
243         int line,
244         const char *func,
245         const char *format, ...) {
246
247         va_list ap;
248         bool written;
249         int saved_errno;
250
251         if (LOG_PRI(level) > log_max_level)
252                 return;
253
254         saved_errno = errno;
255         written = false;
256
257         if (log_target == LOG_TARGET_KMSG) {
258                 va_start(ap, format);
259                 written = write_to_kmsg(level, file, line, func, format, ap) >= 0;
260                 va_end(ap);
261         } else if (log_target == LOG_TARGET_SYSLOG) {
262                 va_start(ap, format);
263                 written = write_to_syslog(level, file, line, func, format, ap) >= 0;
264                 va_end(ap);
265         }
266
267         if (!written) {
268                 va_start(ap, format);
269                 write_to_console(level, file, line, func, format, ap);
270                 va_end(ap);
271         }
272
273         errno = saved_errno;
274 }