summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@sbcglobal.net>2014-12-16 14:35:03 -0500
committerLuke Shumaker <lukeshu@sbcglobal.net>2014-12-16 14:35:03 -0500
commit9922d3fb47002ada1e751a1d26be9fb5b9c75dfc (patch)
treee35b565a647b90d7d284c5f341fc70618e409c27
parent2bdd7e9a0b059ddfdb58f4a58d339ae32d206877 (diff)
simplify the logger
-rw-r--r--nslcd/cfg.c16
-rw-r--r--nslcd/log.c183
-rw-r--r--nslcd/log.h25
-rw-r--r--nslcd/nslcd.c5
4 files changed, 43 insertions, 186 deletions
diff --git a/nslcd/cfg.c b/nslcd/cfg.c
index 45dc246..97c4a1b 100644
--- a/nslcd/cfg.c
+++ b/nslcd/cfg.c
@@ -220,25 +220,13 @@ static void handle_log(const char *filename, int lnr,
const char *keyword, char *line)
{
int level = LOG_INFO;
- char scheme[64];
char loglevel[32];
check_argumentcount(filename, lnr, keyword,
- get_token(&line, scheme, sizeof(scheme)) != NULL);
+ get_token(&line, loglevel, sizeof(loglevel)) != NULL);
if (get_token(&line, loglevel, sizeof(loglevel)) != NULL)
level = parse_loglevel(filename, lnr, loglevel);
get_eol(filename, lnr, keyword, &line);
- if (strcasecmp(scheme, "none") == 0)
- log_addlogging_none();
- else if (strcasecmp(scheme, "syslog") == 0)
- log_addlogging_syslog(level);
- else if (scheme[0] == '/')
- log_addlogging_file(level, scheme);
- else
- {
- log_log(LOG_ERR, "%s:%d: %s: invalid argument '%s'",
- filename, lnr, keyword, scheme);
- exit(EXIT_FAILURE);
- }
+ log_setdefaultloglevel(level);
}
static enum nss_map_selector parse_map(const char *value)
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));
}
diff --git a/nslcd/log.h b/nslcd/log.h
index 277daa7..d86ef8c 100644
--- a/nslcd/log.h
+++ b/nslcd/log.h
@@ -23,25 +23,20 @@
#ifndef NSLCD__LOG_H
#define NSLCD__LOG_H 1
-#include <syslog.h>
#include "compat/attrs.h"
+#define LOG_EMERG 0
+#define LOG_ALERT 1
+#define LOG_CRIT 2
+#define LOG_ERR 3
+#define LOG_WARNING 4
+#define LOG_NOTICE 5
+#define LOG_INFO 6
+#define LOG_DEBUG 7
+
/* set loglevel when no logging is configured */
void log_setdefaultloglevel(int loglevel);
-/* configure logging to a file */
-void log_addlogging_file(int loglevel, const char *filename);
-
-/* configure logging to syslog */
-void log_addlogging_syslog(int loglevel);
-
-/* configure a null logging mode (no logging) */
-void log_addlogging_none(void);
-
-/* start the logging with the configured logging methods
- if no method is configured yet, logging is done to syslog */
-void log_startlogging(void);
-
/* indicate that a session id should be included in the output
and set it to a new value */
void log_newsession(void);
@@ -59,7 +54,7 @@ void log_setrequest(const char *format, ...)
void log_log(int pri, const char *format, ...)
LIKE_PRINTF(2, 3);
-/* log the logging configuration on DEBUG loglevel */
+/* log the logger config at the LOG_DEBUG level */
void log_log_config(void);
#endif /* not NSLCD__LOG_H */
diff --git a/nslcd/nslcd.c b/nslcd/nslcd.c
index d7891e6..1b2ecf7 100644
--- a/nslcd/nslcd.c
+++ b/nslcd/nslcd.c
@@ -425,11 +425,6 @@ int main(int argc, char *argv[])
/* read configuration file */
cfg_init(NSLCD_CONF_PATH);
- /* intilialize logging */
- if (!nslcd_debugging)
- {
- log_startlogging();
- }
/* log start */
log_log(LOG_INFO, "version %s starting", VERSION);
/* install handler to close stuff off on exit and log notice */