From de5ee77b00c0c41ca5eb1434dead6013ac834dc6 Mon Sep 17 00:00:00 2001 From: Eric Caruso Date: Mon, 30 Apr 2018 17:07:41 -0700 Subject: [PATCH] logging: add use_syslog to qlog_setup This causes qlog to log to syslog instead of logging to stderr. Signed-off-by: Eric Caruso --- lib/logging.c | 23 ++++++++++++++++------- lib/logging.h | 3 ++- src/cfg.c | 2 +- src/lookup.c | 2 +- src/ns.c | 2 +- 5 files changed, 21 insertions(+), 11 deletions(-) diff --git a/lib/logging.c b/lib/logging.c index 1c36ccd..07148b9 100644 --- a/lib/logging.c +++ b/lib/logging.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -7,9 +8,12 @@ static const char default_tag[] = "libqrtr"; static const char *current_tag = default_tag; -void qlog_setup(const char *tag) +static bool logging_to_syslog = false; + +void qlog_setup(const char *tag, bool use_syslog) { current_tag = tag; + logging_to_syslog = use_syslog; } static const char *get_priority_string(int priority) @@ -37,13 +41,18 @@ static const char *get_priority_string(int priority) void qlog(int priority, const char *format, ...) { - char buf[QLOG_BUF_SIZE]; va_list ap; - va_start(ap, format); - vsnprintf(buf, QLOG_BUF_SIZE, format, ap); - va_end(ap); - fprintf(stderr, "%s %s: %s\n", - get_priority_string(priority), current_tag, buf); + if (logging_to_syslog) { + vsyslog(priority, format, ap); + } else { + char buf[QLOG_BUF_SIZE]; + vsnprintf(buf, QLOG_BUF_SIZE, format, ap); + + fprintf(stderr, "%s %s: %s\n", + get_priority_string(priority), current_tag, buf); + } + + va_end(ap); } diff --git a/lib/logging.h b/lib/logging.h index 90ada7a..3035cae 100644 --- a/lib/logging.h +++ b/lib/logging.h @@ -1,6 +1,7 @@ #ifndef _QRTR_LOGGING_H_ #define _QRTR_LOGGING_H_ +#include #include #include @@ -10,7 +11,7 @@ #define __PRINTF__(fmt, args) #endif -void qlog_setup(const char *tag); +void qlog_setup(const char *tag, bool use_syslog); void qlog(int priority, const char *format, ...) __PRINTF__(2, 3); diff --git a/src/cfg.c b/src/cfg.c index 816c1bd..97a8352 100644 --- a/src/cfg.c +++ b/src/cfg.c @@ -29,7 +29,7 @@ int main(int argc, char **argv) char *ep; const char *progname = basename(argv[0]); - qlog_setup(progname); + qlog_setup(progname, false); if (argc != 2) usage(progname); diff --git a/src/lookup.c b/src/lookup.c index 1e68676..7fc85eb 100644 --- a/src/lookup.c +++ b/src/lookup.c @@ -107,7 +107,7 @@ int main(int argc, char **argv) int rc; const char *progname = basename(argv[0]); - qlog_setup(progname); + qlog_setup(progname, false); rc = 0; memset(&pkt, 0, sizeof(pkt)); diff --git a/src/ns.c b/src/ns.c index 706b105..1f1c7f4 100644 --- a/src/ns.c +++ b/src/ns.c @@ -705,7 +705,7 @@ int main(int argc, char **argv) int rc; const char *progname = basename(argv[0]); - qlog_setup(progname); + qlog_setup(progname, false); while ((opt = getopt(argc, argv, "f")) != -1) { switch (opt) {