1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
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.
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.
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/>.
27 #include <sys/socket.h>
34 #define SYSLOG_TIMEOUT_USEC (5*USEC_PER_SEC)
35 #define LOG_BUFFER_MAX 1024
37 static LogTarget log_target = LOG_TARGET_CONSOLE;
38 static int log_max_level = LOG_DEBUG;
40 static int syslog_fd = -1;
41 static int kmsg_fd = -1;
43 void log_close_kmsg(void) {
46 close_nointr(kmsg_fd);
51 int log_open_kmsg(void) {
53 if (log_target != LOG_TARGET_KMSG) {
61 if ((kmsg_fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0)
64 log_info("Succesfully opened /dev/kmsg for logging.");
69 void log_close_syslog(void) {
72 close_nointr(syslog_fd);
77 int log_open_syslog(void) {
80 struct sockaddr_un un;
85 if (log_target != LOG_TARGET_SYSLOG) {
93 if ((syslog_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0)
96 /* Make sure we don't block for more than 5s when talking to
98 timeval_store(&tv, SYSLOG_TIMEOUT_USEC);
99 if (setsockopt(syslog_fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0) {
106 sa.un.sun_family = AF_UNIX;
107 strncpy(sa.un.sun_path, "/dev/log", sizeof(sa.un.sun_path));
109 if (connect(syslog_fd, &sa.sa, sizeof(sa)) < 0) {
115 log_info("Succesfully opened syslog for logging.");
120 void log_set_target(LogTarget target) {
122 assert(target < _LOG_TARGET_MAX);
127 void log_set_max_level(int level) {
128 assert((level & LOG_PRIMASK) == level);
130 log_max_level = level;
133 static void write_to_console(
141 const char *prefix, *suffix;
143 if (LOG_PRI(level) <= LOG_ERR) {
144 prefix = "\x1B[1;31m";
151 fprintf(stderr, "(%s:%u) %s", file, line, prefix);
152 vfprintf(stderr, format, ap);
153 fprintf(stderr, "%s\n", suffix);
156 static int write_to_syslog(
164 char header_priority[16], header_time[64], header_pid[16];
165 char buffer[LOG_BUFFER_MAX];
166 struct iovec iovec[5];
167 struct msghdr msghdr;
174 snprintf(header_priority, sizeof(header_priority), "<%i>", LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(level)));
175 char_array_0(header_priority);
177 t = (time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC);
178 if (!(tm = localtime(&t)))
181 if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
184 snprintf(header_pid, sizeof(header_pid), "[%llu]: ", (unsigned long long) getpid());
185 char_array_0(header_pid);
187 vsnprintf(buffer, sizeof(buffer), format, ap);
188 char_array_0(buffer);
191 IOVEC_SET_STRING(iovec[0], header_priority);
192 IOVEC_SET_STRING(iovec[1], header_time);
193 IOVEC_SET_STRING(iovec[2], __progname);
194 IOVEC_SET_STRING(iovec[3], header_pid);
195 IOVEC_SET_STRING(iovec[4], buffer);
198 msghdr.msg_iov = iovec;
199 msghdr.msg_iovlen = ELEMENTSOF(iovec);
201 if (sendmsg(syslog_fd, &msghdr, 0) < 0)
207 static int write_to_kmsg(
215 char header_priority[16], header_pid[16];
216 char buffer[LOG_BUFFER_MAX];
217 struct iovec iovec[5];
222 snprintf(header_priority, sizeof(header_priority), "<%i>", LOG_PRI(level));
223 char_array_0(header_priority);
225 snprintf(header_pid, sizeof(header_pid), "[%llu]: ", (unsigned long long) getpid());
226 char_array_0(header_pid);
228 vsnprintf(buffer, sizeof(buffer), format, ap);
229 char_array_0(buffer);
232 IOVEC_SET_STRING(iovec[0], header_priority);
233 IOVEC_SET_STRING(iovec[1], __progname);
234 IOVEC_SET_STRING(iovec[2], header_pid);
235 IOVEC_SET_STRING(iovec[3], buffer);
236 IOVEC_SET_STRING(iovec[4], (char*) "\n");
238 if (writev(kmsg_fd, iovec, ELEMENTSOF(iovec)) < 0)
249 const char *format, ...) {
255 if (LOG_PRI(level) > log_max_level)
261 if (log_target == LOG_TARGET_KMSG) {
262 va_start(ap, format);
263 written = write_to_kmsg(level, file, line, func, format, ap) >= 0;
265 } else if (log_target == LOG_TARGET_SYSLOG) {
266 va_start(ap, format);
267 written = write_to_syslog(level, file, line, func, format, ap) >= 0;
272 va_start(ap, format);
273 write_to_console(level, file, line, func, format, ap);
280 int log_set_target_from_string(const char *e) {
283 if ((t = log_target_from_string(e)) < 0)
290 int log_set_max_level_from_string(const char *e) {
293 if ((t = log_level_from_string(e)) < 0)
296 log_set_max_level(t);
300 void log_parse_environment(void) {
303 if ((e = getenv("SYSTEMD_LOG_TARGET")))
304 if (log_set_target_from_string(e) < 0)
305 log_warning("Failed to parse log target %s. Ignoring.", e);
307 if ((e = getenv("SYSTEMD_LOG_LEVEL")))
308 if (log_set_max_level_from_string(e) < 0)
309 log_warning("Failed to parse log level %s. Ignoring.", e);
312 static const char *const log_target_table[] = {
313 [LOG_TARGET_CONSOLE] = "console",
314 [LOG_TARGET_SYSLOG] = "syslog",
315 [LOG_TARGET_KMSG] = "kmsg",
318 DEFINE_STRING_TABLE_LOOKUP(log_target, LogTarget);