commit 3bfcaa97264f1bb6099d0df5bf4ae798a7dfbd65
parent b8338fec5882d49337a58dcb4681189c3c94de34
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date: Wed, 25 Aug 2021 23:59:54 -0700
Add command to set log level
Soon commands will be run at startup from arguments to configure
the program so a command to set the logging level is needed.
Diffstat:
6 files changed, 57 insertions(+), 21 deletions(-)
diff --git a/aps/aps.h b/aps/aps.h
@@ -10,7 +10,7 @@ struct aps;
struct aps {
char **next;
int close;
- int logmask;
+ int loglevel;
int nfds;
struct apcon *con;
struct client {
diff --git a/aps/command.c b/aps/command.c
@@ -219,3 +219,20 @@ aps_status(struct aps *aps, int s, int argc, char **argv)
return errstr;
return NULL;
}
+
+char *
+aps_loglevel(struct aps *aps, int s, int argc, char **argv)
+{
+ int i;
+ int level, mask;
+
+ level = 0;
+ for (i = 0; i < argc; ++i) {
+ if ((mask = strlevel(argv[i])) < 0)
+ return "invalid log level";
+ level |= mask;
+ }
+
+ aps->loglevel = level;
+ return NULL;
+}
diff --git a/aps/command.h b/aps/command.h
@@ -15,3 +15,4 @@ char *aps_name(struct aps *, int, int, char **);
char *aps_next(struct aps *, int, int, char **);
char *aps_previous(struct aps *, int, int, char **);
char *aps_status(struct aps *, int, int, char **);
+char *aps_loglevel(struct aps *, int, int, char **);
diff --git a/aps/log.c b/aps/log.c
@@ -22,29 +22,44 @@
#include <poll.h>
#include <stdarg.h>
#include <stdio.h>
+#include <string.h>
#include "aps.h"
#include "log.h"
+#include "util.h"
+
+static struct {
+ char *str;
+ int level;
+} binds[] = {
+ { "info", INFO },
+ { "warn", WARN },
+ { "error", ERROR },
+ { "fatal", FATAL },
+ { "command", COMMAND },
+ { "queue", QUEUE }
+};
char *
-apstrll(int level)
+levelstr(int level)
+{
+ int i;
+
+ for (i = 0; i < LEN(binds); ++i)
+ if (level == binds[i].level)
+ return binds[i].str;
+ return NULL;
+}
+
+int
+strlevel(char *str)
{
- switch (level) {
- case INFO:
- return "info";
- case WARN:
- return "warn";
- case ERROR:
- return "error";
- case FATAL:
- return "fatal";
- case COMMAND:
- return "command";
- case QUEUE:
- return "queue";
- default:
- return "undefined";
- }
+ int i;
+
+ for (i = 0; i < LEN(binds); ++i)
+ if (strcmp(str, binds[i].str) == 0)
+ return binds[i].level;
+ return -1;
}
void
@@ -52,10 +67,10 @@ aps_logfmt(struct aps *aps, int level, int fd, char *fmt, ...)
{
va_list ap;
- if (fmt == NULL || (aps && !(level & aps->logmask)))
+ if (fmt == NULL || (aps && !(level & aps->loglevel)))
return;
- fprintf(stderr, "ap(%d): %s: ", fd, apstrll(level));
+ fprintf(stderr, "ap(%d): %s: ", fd, levelstr(level));
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
diff --git a/aps/log.h b/aps/log.h
@@ -7,5 +7,7 @@ enum {
QUEUE = 1 << 5
};
+char *levelstr(int);
+int strlevel(char *);
void aps_logfmt(struct aps *, int, int, char *, ...);
void aps_log(struct aps *, int, int, char *);
diff --git a/aps/main.c b/aps/main.c
@@ -38,6 +38,7 @@ static struct aps *aps;
static struct command commands[] = {
{ "add", aps_add },
{ "list", aps_list },
+ { "log", aps_loglevel },
{ "name", aps_name },
{ "next", aps_next },
{ "pause", aps_pause },
@@ -115,7 +116,7 @@ main(int argc, char *argv[])
die("unable to setup server");
aps->player->state = STOPPED;
- aps->logmask = ~0;
+ aps->loglevel = ~0;
aps->coms = commands;
aps->ncoms = LEN(commands);