logging: add use_syslog to qlog_setup

This causes qlog to log to syslog instead of logging to stderr.

Signed-off-by: Eric Caruso <ejcaruso@chromium.org>
This commit is contained in:
Eric Caruso
2018-04-30 17:07:41 -07:00
committed by Bjorn Andersson
parent f64c25c8af
commit de5ee77b00
5 changed files with 21 additions and 11 deletions

View File

@@ -1,3 +1,4 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdarg.h>
#include <syslog.h>
@@ -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);
}

View File

@@ -1,6 +1,7 @@
#ifndef _QRTR_LOGGING_H_
#define _QRTR_LOGGING_H_
#include <stdbool.h>
#include <stdlib.h>
#include <syslog.h>
@@ -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);

View File

@@ -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);

View File

@@ -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));

View File

@@ -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) {