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

Make log testable: return true value when something was logged

This commit is contained in:
Moritz Grimm 2015-05-24 09:05:40 +02:00
parent 2ccc72c963
commit 4c6a879303
2 changed files with 55 additions and 29 deletions

View File

@ -29,23 +29,26 @@
#include "cfg.h" #include "cfg.h"
#include "log.h" #include "log.h"
static void _log(enum log_levels, const char *, ...) static int _log(enum log_levels, const char *, ...)
ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(2)
ATTRIBUTE_FORMAT(printf, 2, 3); ATTRIBUTE_FORMAT(printf, 2, 3);
static void _vlog(enum log_levels, const char *, va_list) static int _vlog(enum log_levels, const char *, va_list)
ATTRIBUTE_NONNULL(2); ATTRIBUTE_NONNULL(2);
static void static int
_log(enum log_levels lvl, const char *fmt, ...) _log(enum log_levels lvl, const char *fmt, ...)
{ {
va_list ap; va_list ap;
int ret;
va_start(ap, fmt); va_start(ap, fmt);
_vlog(lvl, fmt, ap); ret = _vlog(lvl, fmt, ap);
va_end(ap); va_end(ap);
return (ret);
} }
static void static int
_vlog(enum log_levels lvl, const char *fmt, va_list ap) _vlog(enum log_levels lvl, const char *fmt, va_list ap)
{ {
va_list ap2; va_list ap2;
@ -63,18 +66,18 @@ _vlog(enum log_levels lvl, const char *fmt, va_list ap)
break; break;
case NOTICE: case NOTICE:
if (cfg_get_program_verbosity() < 1) if (cfg_get_program_verbosity() < 1)
return; return (0);
p = LOG_NOTICE; p = LOG_NOTICE;
break; break;
case INFO: case INFO:
if (cfg_get_program_verbosity() < 2) if (cfg_get_program_verbosity() < 2)
return; return (0);
p = LOG_INFO; p = LOG_INFO;
break; break;
case DEBUG: case DEBUG:
default: default:
if (cfg_get_program_verbosity() < 3) if (cfg_get_program_verbosity() < 3)
return; return (0);
p = LOG_DEBUG; p = LOG_DEBUG;
break; break;
}; };
@ -82,6 +85,8 @@ _vlog(enum log_levels lvl, const char *fmt, va_list ap)
va_copy(ap2, ap); va_copy(ap2, ap);
vsyslog(p, fmt, ap2); vsyslog(p, fmt, ap2);
va_end(ap2); va_end(ap2);
return (1);
} }
int int
@ -99,75 +104,96 @@ log_exit(void)
closelog(); closelog();
} }
void int
log_syserr(enum log_levels lvl, int error, const char *pfx) log_syserr(enum log_levels lvl, int error, const char *pfx)
{ {
char errbuf[1024]; char errbuf[1024];
int ret;
if (0 != strerror_r(error, errbuf, sizeof(errbuf))) if (0 != strerror_r(error, errbuf, sizeof(errbuf)))
abort(); abort();
_log(lvl, "%s%s%s", ret = _log(lvl, "%s%s%s",
pfx ? pfx : "", pfx ? pfx : "",
pfx ? ": " : "", pfx ? ": " : "",
errbuf); errbuf);
return (ret);
} }
void int
log_alert(const char *fmt, ...) log_alert(const char *fmt, ...)
{ {
va_list ap; va_list ap;
int ret;
va_start(ap, fmt); va_start(ap, fmt);
_vlog(ALERT, fmt, ap); ret = _vlog(ALERT, fmt, ap);
va_end(ap); va_end(ap);
return (ret);
} }
void int
log_error(const char *fmt, ...) log_error(const char *fmt, ...)
{ {
va_list ap; va_list ap;
int ret;
va_start(ap, fmt); va_start(ap, fmt);
_vlog(ERROR, fmt, ap); ret = _vlog(ERROR, fmt, ap);
va_end(ap); va_end(ap);
return (ret);
} }
void int
log_warning(const char *fmt, ...) log_warning(const char *fmt, ...)
{ {
va_list ap; va_list ap;
int ret;
va_start(ap, fmt); va_start(ap, fmt);
_vlog(WARNING, fmt, ap); ret = _vlog(WARNING, fmt, ap);
va_end(ap); va_end(ap);
return (ret);
} }
void int
log_notice(const char *fmt, ...) log_notice(const char *fmt, ...)
{ {
va_list ap; va_list ap;
int ret;
va_start(ap, fmt); va_start(ap, fmt);
_vlog(NOTICE, fmt, ap); ret = _vlog(NOTICE, fmt, ap);
va_end(ap); va_end(ap);
return (ret);
} }
void int
log_info(const char *fmt, ...) log_info(const char *fmt, ...)
{ {
va_list ap; va_list ap;
int ret;
va_start(ap, fmt); va_start(ap, fmt);
_vlog(INFO, fmt, ap); ret = _vlog(INFO, fmt, ap);
va_end(ap); va_end(ap);
return (ret);
} }
void int
log_debug(const char *fmt, ...) log_debug(const char *fmt, ...)
{ {
va_list ap; va_list ap;
int ret;
va_start(ap, fmt); va_start(ap, fmt);
_vlog(DEBUG, fmt, ap); ret = _vlog(DEBUG, fmt, ap);
va_end(ap); va_end(ap);
return (ret);
} }

View File

@ -31,24 +31,24 @@ enum log_levels {
int log_init(void); int log_init(void);
void log_exit(void); void log_exit(void);
void log_syserr(enum log_levels, int, const char *); int log_syserr(enum log_levels, int, const char *);
void log_alert(const char *, ...) int log_alert(const char *, ...)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(1)
ATTRIBUTE_FORMAT(printf, 1, 2); ATTRIBUTE_FORMAT(printf, 1, 2);
void log_error(const char *, ...) int log_error(const char *, ...)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(1)
ATTRIBUTE_FORMAT(printf, 1, 2); ATTRIBUTE_FORMAT(printf, 1, 2);
void log_warning(const char *, ...) int log_warning(const char *, ...)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(1)
ATTRIBUTE_FORMAT(printf, 1, 2); ATTRIBUTE_FORMAT(printf, 1, 2);
void log_notice(const char *, ...) int log_notice(const char *, ...)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(1)
ATTRIBUTE_FORMAT(printf, 1, 2); ATTRIBUTE_FORMAT(printf, 1, 2);
void log_info(const char *, ...) int log_info(const char *, ...)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(1)
ATTRIBUTE_FORMAT(printf, 1, 2); ATTRIBUTE_FORMAT(printf, 1, 2);
void log_debug(const char *, ...) int log_debug(const char *, ...)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(1)
ATTRIBUTE_FORMAT(printf, 1, 2); ATTRIBUTE_FORMAT(printf, 1, 2);