ap

An audio player suited to my tastes
Log | Files | Refs | README | LICENSE

commit 5e827efe9d5a25691e887d0d6950141ef9c89d84
parent 98dfcc57058e294b995cadedd46db587f5b19aa5
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date:   Wed, 14 Jul 2021 15:34:59 -0700

Use writev(2) instead of fprintf(3) for logging

This allows easy omission of uninteresting or missing information
(such as when errno is unset). It also makes it safe for signal
handlers, probably.

Diffstat:
Maps/aps.c | 17-----------------
Maps/aps.h | 1-
Maps/command.c | 1+
Aaps/log.c | 67+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aaps/log.h | 2++
Maps/main.c | 6+++---
Maps/mkfile | 2+-
7 files changed, 74 insertions(+), 22 deletions(-)

diff --git a/aps/aps.c b/aps/aps.c @@ -22,9 +22,7 @@ #include <errno.h> #include <limits.h> #include <poll.h> -#include <stdio.h> #include <stdlib.h> -#include <string.h> #include "aps.h" #include "command.h" @@ -141,18 +139,3 @@ aps_update(struct aps *aps) return 0; } - -void -aps_log(struct aps *aps, int level, char *message, int fd) -{ - if (aps == NULL || level & aps->logmask) - fprintf(stderr, "aps.%d #%d: %s: %s.\n", level, fd, message, - strerror(errno)); - - if (aps != NULL) { - if (level == FATAL || (fd == 0 && level == ERROR)) - aps->close = 1; - else if (level == ERROR && aps_drop(aps, fd)) - aps_log(aps, FATAL, "unable to drop client", 0); - } -} diff --git a/aps/aps.h b/aps/aps.h @@ -36,4 +36,3 @@ int aps_drop(struct aps *, int); int aps_accept(struct aps *); int aps_handle(struct aps *, int); int aps_update(struct aps *); -void aps_log(struct aps *, int, char *, int); diff --git a/aps/command.c b/aps/command.c @@ -26,6 +26,7 @@ #include "aps.h" #include "find.h" +#include "log.h" #include "queue.h" #include "split.h" #include "util.h" diff --git a/aps/log.c b/aps/log.c @@ -0,0 +1,67 @@ +#include <sys/uio.h> +#include <sys/socket.h> + +#include <errno.h> +#include <limits.h> +#include <poll.h> +#include <string.h> + +#include "aps.h" + +void +aploadiov(struct iovec *v, char *s) +{ + v->iov_base = s; + v->iov_len = strlen(s); +} + +char * +apstrll(int level) +{ + switch (level) { + case INFO: + return "info"; + case WARN: + return "warn"; + case ERROR: + return "error"; + case FATAL: + return "fatal"; + default: + return "undefined"; + } +} + +void +aplog(int level, char *message, int erno) +{ + struct iovec *v; + struct iovec iov[7]; + + v = iov; + aploadiov(v++, "aplog."); + aploadiov(v++, apstrll(level)); + if (message) { + aploadiov(v++, ": "); + aploadiov(v++, message); + } + if (errno) { + aploadiov(v++, ": "); + aploadiov(v++, strerror(errno)); + } + aploadiov(v++, ".\n"); + writev(2, iov, v - iov); +} + +void +aps_log(struct aps *aps, int level, char *message, int fd) +{ + if (aps == NULL || level & aps->logmask) + aplog(level, message, errno); + if (aps != NULL) { + if (level == FATAL || (fd == 0 && level == ERROR)) + aps->close = 1; + else if (level == ERROR && aps_drop(aps, fd)) + aps_log(aps, FATAL, "unable to drop client", 0); + } +} diff --git a/aps/log.h b/aps/log.h @@ -0,0 +1,2 @@ +void aplog(int, char *, int); +void aps_log(struct aps *, int, char *, int); diff --git a/aps/main.c b/aps/main.c @@ -14,6 +14,7 @@ #include "aps.h" #include "command.h" #include "config.h" +#include "log.h" #include "util.h" static struct aps *aps; @@ -21,13 +22,13 @@ static struct aps *aps; void slog(char *s) { - aps_log(NULL, FATAL, s, 0); + aplog(FATAL, s, errno); } void sigclose(int sig) { - aps->close = 1; + aps_log(aps, FATAL, "signal", 0); } void @@ -58,7 +59,6 @@ run(struct aps *aps) struct aps * setup(char *path) { - int i; struct aps *aps; aps = aps_open(path); diff --git a/aps/mkfile b/aps/mkfile @@ -1,7 +1,7 @@ # Copyright 2021 Jacob R. Edwards name = aps -src = aps.c buf.c command.c find.c item.c main.c match.c player.c queue.c response.c split.c +src = aps.c buf.c command.c find.c item.c log.c main.c match.c player.c queue.c response.c split.c obj = ${src:%.c=%.o} lib = ap mk = ../mk