log.c revision 5b6319dceedd81f3f1ce7eb70ea5defaef43bcec
/*-*- Mode: C; c-basic-offset: 8 -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <stdarg.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include "log.h"
#include "util.h"
#include "macro.h"
#define LOG_BUFFER_MAX 1024
static int log_max_level = LOG_DEBUG;
static int console_fd = STDERR_FILENO;
static int syslog_fd = -1;
static int kmsg_fd = -1;
/* Akin to glibc's __abort_msg; which is private and we hance cannot
* use here. */
static char *log_abort_msg = NULL;
void log_close_console(void) {
if (console_fd < 0)
return;
if (getpid() == 1) {
if (console_fd >= 3)
console_fd = -1;
}
}
static int log_open_console(void) {
if (console_fd >= 0)
return 0;
if (getpid() == 1) {
return console_fd;
}
} else
return 0;
}
void log_close_kmsg(void) {
if (kmsg_fd < 0)
return;
kmsg_fd = -1;
}
static int log_open_kmsg(void) {
if (kmsg_fd >= 0)
return 0;
return -errno;
}
return 0;
}
void log_close_syslog(void) {
if (syslog_fd < 0)
return;
syslog_fd = -1;
}
static int log_open_syslog(void) {
union {
struct sockaddr_un un;
} sa;
int r;
if (syslog_fd >= 0)
return 0;
r = -errno;
goto fail;
}
/* Make sure we don't block for more than 5s when talking to
* syslog */
r = -errno;
goto fail;
}
r = -errno;
goto fail;
}
log_info("Succesfully opened syslog for logging.");
return 0;
fail:
return r;
}
int log_open(void) {
int r;
/* If we don't use the console we close it here, to not get
* killed by SAK. If we don't use syslog we close it here so
* that we are not confused by somebody deleting the socket in
* because there is no reason to close it. */
if (log_target == LOG_TARGET_NULL) {
return 0;
}
if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
if ((r = log_open_syslog()) >= 0) {
return r;
}
if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
if ((r = log_open_kmsg()) >= 0) {
return r;
}
return log_open_console();
}
log_target = target;
}
void log_set_max_level(int level) {
}
static int write_to_console(
int level,
const char*file,
int line,
const char *func,
const char *buffer) {
char location[64];
unsigned n = 0;
bool highlight;
if (console_fd < 0)
return 0;
if (highlight)
if (highlight)
return -errno;
return 1;
}
static int write_to_syslog(
int level,
const char*file,
int line,
const char *func,
const char *buffer) {
time_t t;
if (syslog_fd < 0)
return 0;
snprintf(header_priority, sizeof(header_priority), "<%i>", LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(level)));
return -EINVAL;
return -EINVAL;
return -errno;
return 1;
}
static int write_to_kmsg(
int level,
const char*file,
int line,
const char *func,
const char *buffer) {
if (kmsg_fd < 0)
return 0;
return -errno;
return 1;
}
static int log_dispatch(
int level,
const char*file,
int line,
const char *func,
char *buffer) {
int r = 0;
if (log_target == LOG_TARGET_NULL)
return 0;
do {
char *e;
int k;
if (buffer[0] == 0)
break;
*(e++) = 0;
if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
log_target == LOG_TARGET_SYSLOG) {
} else if (r > 0)
r++;
}
if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
log_target == LOG_TARGET_KMSG) {
} else if (r > 0)
r++;
}
return k;
buffer = e;
} while (buffer);
return r;
}
int log_dump_internal(
int level,
const char*file,
int line,
const char *func,
char *buffer) {
int saved_errno, r;
/* This modifies the buffer... */
return 0;
saved_errno = errno;
errno = saved_errno;
return r;
}
int log_meta(
int level,
const char*file,
int line,
const char *func,
const char *format, ...) {
char buffer[LOG_BUFFER_MAX];
int saved_errno, r;
return 0;
saved_errno = errno;
errno = saved_errno;
return r;
}
void log_assert(
const char*file,
int line,
const char *func,
const char *format, ...) {
static char buffer[LOG_BUFFER_MAX];
int saved_errno = errno;
abort();
/* If the user chose to ignore this SIGABRT, we are happy to go on, as if nothing happened. */
errno = saved_errno;
}
int log_set_target_from_string(const char *e) {
LogTarget t;
if ((t = log_target_from_string(e)) < 0)
return -EINVAL;
log_set_target(t);
return 0;
}
int log_set_max_level_from_string(const char *e) {
int t;
if ((t = log_level_from_string(e)) < 0)
return -EINVAL;
return 0;
}
void log_parse_environment(void) {
const char *e;
if ((e = getenv("SYSTEMD_LOG_TARGET")))
if (log_set_target_from_string(e) < 0)
log_warning("Failed to parse log target %s. Ignoring.", e);
if ((e = getenv("SYSTEMD_LOG_LEVEL")))
if (log_set_max_level_from_string(e) < 0)
log_warning("Failed to parse log level %s. Ignoring.", e);
}
LogTarget log_get_target(void) {
return log_target;
}
int log_get_max_level(void) {
return log_max_level;
}
static const char *const log_target_table[] = {
[LOG_TARGET_CONSOLE] = "console",
[LOG_TARGET_SYSLOG] = "syslog",
[LOG_TARGET_KMSG] = "kmsg",
[LOG_TARGET_SYSLOG_OR_KMSG] = "syslog-or-kmsg",
[LOG_TARGET_NULL] = "null"
};