1
0
mirror of https://gitlab.xiph.org/xiph/ezstream.git synced 2024-11-03 04:17:18 -05:00

Add a simple logging API

Uses syslog
This commit is contained in:
Moritz Grimm 2015-04-16 19:22:58 +02:00
parent 85c90a3c80
commit 19af508eae
5 changed files with 231 additions and 0 deletions

View File

@ -8,6 +8,7 @@ noinst_HEADERS = \
cfg.h \ cfg.h \
configfile.h \ configfile.h \
ezstream.h \ ezstream.h \
log.h \
metadata.h \ metadata.h \
playlist.h \ playlist.h \
util.h \ util.h \
@ -16,6 +17,7 @@ ezstream_SOURCES = \
cfg.c \ cfg.c \
configfile.c \ configfile.c \
ezstream.c \ ezstream.c \
log.c \
metadata.c \ metadata.c \
playlist.c \ playlist.c \
util.c \ util.c \

View File

@ -1037,6 +1037,7 @@ ez_shutdown(int exitval)
playlist_shutdown(); playlist_shutdown();
freeConfig(pezConfig); freeConfig(pezConfig);
xalloc_shutdown(); xalloc_shutdown();
log_exit();
return (exitval); return (exitval);
} }
@ -1061,6 +1062,7 @@ main(int argc, char *argv[])
ret = 1; ret = 1;
if (0 > cfg_cmdline_parse(argc, argv, &ret)) if (0 > cfg_cmdline_parse(argc, argv, &ret))
return (ret); return (ret);
log_init();
#ifdef XALLOC_DEBUG #ifdef XALLOC_DEBUG
xalloc_initialize_debug(2, NULL); xalloc_initialize_debug(2, NULL);

View File

@ -51,6 +51,7 @@
#endif #endif
#include "cfg.h" #include "cfg.h"
#include "log.h"
#ifndef STDIN_FILENO #ifndef STDIN_FILENO
# define STDIN_FILENO 0 # define STDIN_FILENO 0

171
src/log.c Normal file
View File

@ -0,0 +1,171 @@
/*
* Copyright (c) 2015 Moritz Grimm <mgrimm@mrsserver.net>
*
* 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 <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#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);
}

55
src/log.h Normal file
View File

@ -0,0 +1,55 @@
/*
* Copyright (c) 2015 Moritz Grimm <mgrimm@mrsserver.net>
*
* 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__ */