diff options
Diffstat (limited to 'nslcd/log.c')
-rw-r--r-- | nslcd/log.c | 183 |
1 files changed, 31 insertions, 152 deletions
diff --git a/nslcd/log.c b/nslcd/log.c index 0676cf6..f24abd5 100644 --- a/nslcd/log.c +++ b/nslcd/log.c @@ -21,6 +21,7 @@ #include "config.h" +#include <systemd/sd-daemon.h> #include <stdio.h> #include <stdlib.h> #include <sys/types.h> @@ -35,20 +36,7 @@ #include "log.h" -/* set the logname */ -#undef PACKAGE -#define PACKAGE "nslcd" - -/* storage for logging modes */ -static struct log_cfg { - int loglevel; - const char *scheme; - FILE *fp; /* NULL == syslog */ - struct log_cfg *next; -} *loglist = NULL; - -/* default loglevel when no logging is configured */ -static int prelogging_loglevel = LOG_INFO; +static int loglevel = LOG_DEBUG; #define MAX_REQUESTID_LENGTH 40 @@ -75,77 +63,23 @@ static void tls_init_keys(void) #endif /* no TLS, use pthreads */ /* set loglevel when no logging is configured */ -void log_setdefaultloglevel(int loglevel) -{ - prelogging_loglevel = loglevel; -} - -/* add logging method to configuration list */ -static void addlogging(int loglevel, const char *scheme, FILE *fp) +void log_setdefaultloglevel(int pri) { - struct log_cfg *tmp, *lst; - /* create new logstruct */ - tmp = (struct log_cfg *)malloc(sizeof(struct log_cfg)); - if (tmp == NULL) - { - log_log(LOG_CRIT, "malloc() returned NULL"); - exit(EXIT_FAILURE); - } - tmp->loglevel = loglevel; - tmp->scheme = scheme; - tmp->fp = fp; - tmp->next = NULL; - /* save the struct in the list */ - if (loglist == NULL) - loglist = tmp; - else - { - for (lst = loglist; lst->next != NULL; lst = lst->next); - lst->next = tmp; - } + loglevel = pri; } -/* configure logging to a file */ -void log_addlogging_file(int loglevel, const char *filename) -{ - FILE *fp; - filename = strdup(filename); - if (filename == NULL) - { - log_log(LOG_CRIT, "strdup() returned NULL"); - exit(EXIT_FAILURE); +static char *loglevel2sd(int pri) { + switch(pri) { + case LOG_EMERG: return SD_EMERG; + case LOG_ALERT: return SD_ALERT; + case LOG_CRIT: return SD_CRIT; + case LOG_ERR: return SD_ERR; + case LOG_WARNING: return SD_WARNING; + case LOG_NOTICE: return SD_NOTICE; + case LOG_INFO: return SD_INFO; + case LOG_DEBUG: return SD_DEBUG; } - fp = fopen(filename, "a"); - if (fp == NULL) - { - log_log(LOG_ERR, "cannot open logfile (%s) for appending: %s", - filename, strerror(errno)); - exit(1); - } - addlogging(loglevel, filename, fp); -} - -/* configure logging to syslog */ -void log_addlogging_syslog(int loglevel) -{ - openlog(PACKAGE, LOG_PID, LOG_DAEMON); - addlogging(loglevel, "syslog", NULL); -} - -/* configure a null logging mode (no logging) */ -void log_addlogging_none() -{ - /* this is a hack, but it's so easy */ - addlogging(LOG_EMERG, "none", NULL); -} - -/* start the logging with the configured logging methods - if no method is configured yet, logging is done to syslog */ -void log_startlogging(void) -{ - if (loglist == NULL) - log_addlogging_syslog(LOG_INFO); - prelogging_loglevel = -1; + return "<?>"; } /* indicate that we should clear any session identifiers set by @@ -228,9 +162,7 @@ void log_setrequest(const char *format, ...) /* log the given message using the configured logging method */ void log_log(int pri, const char *format, ...) { - int res; - struct log_cfg *lst; - char buffer[200]; + char *msg = NULL; va_list ap; #ifndef TLS char *sessionid, *requestid; @@ -240,67 +172,24 @@ void log_log(int pri, const char *format, ...) #endif /* no TLS */ /* make the message */ va_start(ap, format); - res = vsnprintf(buffer, sizeof(buffer), format, ap); - if ((res < 0) || (res >= (int)sizeof(buffer))) - { - /* truncate with "..." */ - buffer[sizeof(buffer) - 2] = '.'; - buffer[sizeof(buffer) - 3] = '.'; - buffer[sizeof(buffer) - 4] = '.'; + if (vasprintf(&msg, format, ap) < 0) { + fprintf(stderr, SD_ERR "vasprintf() in logger failed"); } - buffer[sizeof(buffer) - 1] = '\0'; va_end(ap); /* do the logging */ - if (prelogging_loglevel >= 0) - { - /* if logging is not yet defined, log to stderr */ - if (pri <= prelogging_loglevel) - { - if ((requestid != NULL) && (requestid[0] != '\0')) - fprintf(stderr, "%s: [%s] <%s> %s%s\n", PACKAGE, sessionid, requestid, - pri == LOG_DEBUG ? "DEBUG: " : "", buffer); - else if ((sessionid != NULL) && (sessionid[0] != '\0')) - fprintf(stderr, "%s: [%s] %s%s\n", PACKAGE, sessionid, - pri == LOG_DEBUG ? "DEBUG: " : "", buffer); - else - fprintf(stderr, "%s: %s%s\n", PACKAGE, - pri == LOG_DEBUG ? "DEBUG: " : "", buffer); - } - } - else + + if (pri <= loglevel) { - for (lst = loglist; lst != NULL; lst = lst->next) - { - if (pri <= lst->loglevel) - { - if (lst->fp == NULL) - { - if ((requestid != NULL) && (requestid[0] != '\0')) - syslog(pri, "[%s] <%s> %s%s", sessionid, requestid, - pri == LOG_DEBUG ? "DEBUG: " : "", buffer); - else if ((sessionid != NULL) && (sessionid[0] != '\0')) - syslog(pri, "[%s] %s%s", sessionid, - pri == LOG_DEBUG ? "DEBUG: " : "", buffer); - else - syslog(pri, "%s%s", - pri == LOG_DEBUG ? "DEBUG: " : "", buffer); - } - else - { - if ((requestid != NULL) && (requestid[0] != '\0')) - fprintf(lst->fp, "%s: [%s] <%s> %s%s\n", PACKAGE, sessionid, requestid, - pri == LOG_DEBUG ? "DEBUG: " : "", buffer); - else if ((sessionid != NULL) && (sessionid[0] != '\0')) - fprintf(lst->fp, "%s: [%s] %s%s\n", PACKAGE, sessionid, - pri == LOG_DEBUG ? "DEBUG: " : "", buffer); - else - fprintf(lst->fp, "%s: %s%s\n", PACKAGE, - pri == LOG_DEBUG ? "DEBUG: " : "", buffer); - fflush(lst->fp); - } - } - } + if ((requestid != NULL) && (requestid[0] != '\0')) + fprintf(stderr, "%s[sess:%s] <req:%s> %s\n", loglevel2sd(pri), sessionid, requestid, msg); + else if ((sessionid != NULL) && (sessionid[0] != '\0')) + fprintf(stderr, "%s[sess:%s] %s\n", loglevel2sd(pri), sessionid, msg); + else + fprintf(stderr, "%s%s\n", loglevel2sd(pri), msg); + fflush(stderr); } + + free(msg); } static const char *loglevel2str(int loglevel) @@ -317,16 +206,6 @@ static const char *loglevel2str(int loglevel) } } -/* log the logging configuration on DEBUG loglevel */ -void log_log_config(void) -{ - struct log_cfg *lst; - for (lst = loglist; lst != NULL; lst = lst->next) - { - if (lst->loglevel == LOG_EMERG) - log_log(LOG_DEBUG, "CFG: log %s", lst->scheme); - else - log_log(LOG_DEBUG, "CFG: log %s %s", lst->scheme, - loglevel2str(lst->loglevel)); - } +void log_log_config(void) { + log_log(LOG_DEBUG, "CFG: log %s", loglevel2str(loglevel)); } |