From 19af508eaef3179e1e90f1316b806b6bc1d5bc22 Mon Sep 17 00:00:00 2001 From: Moritz Grimm Date: Thu, 16 Apr 2015 19:22:58 +0200 Subject: [PATCH] Add a simple logging API Uses syslog --- src/Makefile.am | 2 + src/ezstream.c | 2 + src/ezstream.h | 1 + src/log.c | 171 ++++++++++++++++++++++++++++++++++++++++++++++++ src/log.h | 55 ++++++++++++++++ 5 files changed, 231 insertions(+) create mode 100644 src/log.c create mode 100644 src/log.h diff --git a/src/Makefile.am b/src/Makefile.am index cb7ca6d..91815f3 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -8,6 +8,7 @@ noinst_HEADERS = \ cfg.h \ configfile.h \ ezstream.h \ + log.h \ metadata.h \ playlist.h \ util.h \ @@ -16,6 +17,7 @@ ezstream_SOURCES = \ cfg.c \ configfile.c \ ezstream.c \ + log.c \ metadata.c \ playlist.c \ util.c \ diff --git a/src/ezstream.c b/src/ezstream.c index f024a2e..5b9f1de 100644 --- a/src/ezstream.c +++ b/src/ezstream.c @@ -1037,6 +1037,7 @@ ez_shutdown(int exitval) playlist_shutdown(); freeConfig(pezConfig); xalloc_shutdown(); + log_exit(); return (exitval); } @@ -1061,6 +1062,7 @@ main(int argc, char *argv[]) ret = 1; if (0 > cfg_cmdline_parse(argc, argv, &ret)) return (ret); + log_init(); #ifdef XALLOC_DEBUG xalloc_initialize_debug(2, NULL); diff --git a/src/ezstream.h b/src/ezstream.h index 2514b68..43b0846 100644 --- a/src/ezstream.h +++ b/src/ezstream.h @@ -51,6 +51,7 @@ #endif #include "cfg.h" +#include "log.h" #ifndef STDIN_FILENO # define STDIN_FILENO 0 diff --git a/src/log.c b/src/log.c new file mode 100644 index 0000000..e0929f3 --- /dev/null +++ b/src/log.c @@ -0,0 +1,171 @@ +/* + * Copyright (c) 2015 Moritz Grimm + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif /* HAVE_CONFIG_H */ + +#include "attributes.h" + +#include +#include +#include +#include +#include + +#include "cfg.h" +#include "log.h" + +static void _log(enum log_levels, const char *, ...) + ATTRIBUTE_NONNULL(2) + ATTRIBUTE_FORMAT(printf, 2, 3); +static void _vlog(enum log_levels, const char *, va_list) + ATTRIBUTE_NONNULL(2); + +static void +_log(enum log_levels lvl, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + _vlog(lvl, fmt, ap); + va_end(ap); +} + +static void +_vlog(enum log_levels lvl, const char *fmt, va_list ap) +{ + va_list ap2; + int p; + + switch (lvl) { + case ALERT: + p = LOG_ALERT; + break; + case ERROR: + p = LOG_ERR; + break; + case WARNING: + p = LOG_WARNING; + break; + case NOTICE: + if (cfg_verbosity() < 1) + return; + p = LOG_NOTICE; + break; + case INFO: + if (cfg_verbosity() < 2) + return; + p = LOG_INFO; + break; + case DEBUG: + default: + if (cfg_verbosity() < 3) + return; + p = LOG_DEBUG; + break; + }; + + va_copy(ap2, ap); + vsyslog(p, fmt, ap2); + va_end(ap2); +} + +void +log_init(void) +{ + openlog(cfg_progname(), LOG_PID|LOG_CONS|LOG_NDELAY|LOG_PERROR, + LOG_USER); +} + +void +log_exit(void) +{ + closelog(); +} + +void +log_syserr(enum log_levels lvl, int error, const char *pfx) +{ + char errbuf[1024]; + + if (0 != strerror_r(error, errbuf, sizeof(errbuf))) + abort(); + _log(lvl, "%s%s%s", + pfx ? pfx : "", + pfx ? ": " : "", + errbuf); +} + +void +log_alert(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + _vlog(ALERT, fmt, ap); + va_end(ap); +} + +void +log_error(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + _vlog(ERROR, fmt, ap); + va_end(ap); +} + +void +log_warning(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + _vlog(WARNING, fmt, ap); + va_end(ap); +} + +void +log_notice(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + _vlog(NOTICE, fmt, ap); + va_end(ap); +} + +void +log_info(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + _vlog(INFO, fmt, ap); + va_end(ap); +} + +void +log_debug(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + _vlog(DEBUG, fmt, ap); + va_end(ap); +} diff --git a/src/log.h b/src/log.h new file mode 100644 index 0000000..a25d66f --- /dev/null +++ b/src/log.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2015 Moritz Grimm + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef __LOG_H__ +#define __LOG_H__ + +#include "attributes.h" + +enum log_levels { + ALERT, + ERROR, + WARNING, + NOTICE, + INFO, + DEBUG +}; + +void log_init(void); +void log_exit(void); + +void log_syserr(enum log_levels, int, const char *); + +void log_alert(const char *, ...) + ATTRIBUTE_NONNULL(1) + ATTRIBUTE_FORMAT(printf, 1, 2); +void log_error(const char *, ...) + ATTRIBUTE_NONNULL(1) + ATTRIBUTE_FORMAT(printf, 1, 2); +void log_warning(const char *, ...) + ATTRIBUTE_NONNULL(1) + ATTRIBUTE_FORMAT(printf, 1, 2); +void log_notice(const char *, ...) + ATTRIBUTE_NONNULL(1) + ATTRIBUTE_FORMAT(printf, 1, 2); +void log_info(const char *, ...) + ATTRIBUTE_NONNULL(1) + ATTRIBUTE_FORMAT(printf, 1, 2); +void log_debug(const char *, ...) + ATTRIBUTE_NONNULL(1) + ATTRIBUTE_FORMAT(printf, 1, 2); + +#endif /* __LOG_H__ */