ap

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

commit d778bb09ac134db3cab68b0c8bc2087e877a43f2
parent c541c95146f7b072b56df2c0970953e0896ad1de
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date:   Wed, 14 Jul 2021 05:55:42 -0700

Add server logging function

Diffstat:
Maps/aps.c | 18++++++++++++++++++
Maps/aps.h | 9+++++++++
Maps/command.c | 4+---
Maps/main.c | 43+++++++++++++++++++++++++++----------------
4 files changed, 55 insertions(+), 19 deletions(-)

diff --git a/aps/aps.c b/aps/aps.c @@ -22,6 +22,7 @@ #include <errno.h> #include <limits.h> #include <poll.h> +#include <stdio.h> #include <stdlib.h> #include "aps.h" @@ -139,3 +140,20 @@ aps_update(struct aps *aps) return 0; } + +void +aps_log(struct aps *aps, int mask, char *message, int fd) +{ + if (mask & aps->logmask) + fprintf(stderr, "aps[%d]: (%d) %s\n", mask, fd, message); + + if (mask == FATAL || (fd == 0 && mask == ERROR)) { + aps_close(aps); + exit(1); + } + + if (mask == ERROR) { + if (aps_drop(aps, fd)) + aps_log(aps, FATAL, "unable to drop client", 0); + } +} diff --git a/aps/aps.h b/aps/aps.h @@ -7,10 +7,18 @@ struct aps; #include "player.h" #include "response.h" +enum apsloglevel { + INFO = 1, + WARN = 2, + ERROR = 3, + FATAL = 4 +}; + struct aps { char *next[AP_ARGV_MAX]; int nextlen; int close; + int logmask; int nfds; struct apcon *con; struct client { @@ -28,3 +36,4 @@ 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 @@ -198,9 +198,7 @@ aps_read(struct aps *aps, int s) buf = aps->clients[s].request; if (buf->len == buf->size && bufresize(buf, MAX(buf->size, 128) * 2)) { - buf->len = 0; - /* TODO: handle error */ - respfinish(aps, s, strerror(errno)); + aps_log(aps, ERROR, strerror(errno), s); return NULL; } diff --git a/aps/main.c b/aps/main.c @@ -51,37 +51,48 @@ run(struct aps *aps) return 0; } -int -main(int argc, char *argv[]) +struct aps * +setup(char *path) { int i; + struct aps *aps; -#ifdef __OpenBSD__ - if (pledge("stdio rpath cpath proc exec unix", NULL)) - die("pledge"); -#endif - - aps = aps_open(NULL); + aps = aps_open(path); if (aps == NULL) - die("unable to open connection"); + return NULL; signal(SIGCHLD, sigchld); signal(SIGINT, sigclose); signal(SIGTERM, sigclose); - if (aps_next(aps, -1, LEN(next), next)) - die("aps_next"); + if (aps_next(aps, -1, LEN(next), next)) { + aps_close(aps); + return NULL; + } aps->player.argv = player + 1; aps->player.prog = player[0]; aps->player.state = STOPPED; - if (run(aps)) { - perror("error"); - aps_close(aps); - return 1; - } + aps->logmask = INFO | WARN | ERROR | FATAL; + + return aps; +} + +int +main(int argc, char *argv[]) +{ +#ifdef __OpenBSD__ + if (pledge("stdio rpath cpath proc exec unix", NULL)) + die("pledge"); +#endif + + aps = setup(NULL); + if (aps == NULL) + die("unable to setup server"); + if (run(aps)) + aps_log(aps, FATAL, NULL, 0); aps_close(aps); return 0; }