Skip to content

Commit

Permalink
logger.c: Allow max debug level per console.
Browse files Browse the repository at this point in the history
Allow a maximum debug level to be configured per console, so that
multiple consoles may be used without all of them receiving the
same intensity of debug messages.
  • Loading branch information
InterLinked1 committed Oct 6, 2024
1 parent b41f4cb commit 457fdcf
Showing 1 changed file with 43 additions and 2 deletions.
45 changes: 43 additions & 2 deletions bbs/logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,13 @@ static int cli_maxdebug(struct bbs_cli_args *a)
return 0;
}

static int cli_localdebug(struct bbs_cli_args *a);

static struct bbs_cli_entry cli_commands_logger[] = {
BBS_CLI_COMMAND(cli_verbose, "verbose", 2, "Set verbose log level", "verbose <newlevel>"),
BBS_CLI_COMMAND(cli_debug, "debug", 2, "Set debug log level", "debug <newlevel>"),
BBS_CLI_COMMAND(cli_maxdebug, "maxdebug", 2, "Set max file debug log level", "maxdebug <newlevel>"),
BBS_CLI_COMMAND(cli_localdebug, "localdebug", 2, "Set max debug log level for current console session", "localdebug <newlevel>"),
};

int bbs_log_init(int nofork)
Expand Down Expand Up @@ -168,7 +171,8 @@ int bbs_set_stdout_logging(int enabled)

/*! \note int lists, anyone? */
struct remote_log_fd {
int fd;
int fd; /* File descriptor for this console */
int maxdebug; /* Max debug level for this console */
RWLIST_ENTRY(remote_log_fd) entry; /* Next entry */
};

Expand All @@ -189,6 +193,38 @@ int bbs_set_fd_logging(int fd, int enabled)
return 0;
}

static int set_fd_logging_max_debug(int fd, int maxdebug)
{
int oldmax;
struct remote_log_fd *rfd;
RWLIST_WRLOCK(&remote_log_fds);
RWLIST_TRAVERSE(&remote_log_fds, rfd, entry) {
if (rfd->fd == fd) {
oldmax = rfd->maxdebug;
rfd->maxdebug = maxdebug;
break;
}
}
RWLIST_UNLOCK(&remote_log_fds);
return rfd ? oldmax : -1;
}

static int cli_localdebug(struct bbs_cli_args *a)
{
int oldmax, newmax;

newmax = atoi(a->argv[1]);
if (newmax < 0 || newmax > MAX_DEBUG) {
return -1;
}
oldmax = set_fd_logging_max_debug(a->fdout, newmax);
if (oldmax == -1) {
return -1;
}
bbs_debug(1, "Max local debug level for fd %d changed from %d to %d\n", a->fdout, oldmax, newmax);
return 0;
}

int bbs_add_logging_fd(int fd)
{
struct remote_log_fd *rfd;
Expand All @@ -215,6 +251,7 @@ int bbs_add_logging_fd(int fd)
return -1;
}
rfd->fd = fd;
rfd->maxdebug = MAX_DEBUG; /* By default, there is no local restriction on the max debug level */
RWLIST_INSERT_HEAD(&remote_log_fds, rfd, entry);
fd_logging[fd] = 1; /* Initialize to enabled */
RWLIST_UNLOCK(&remote_log_fds);
Expand Down Expand Up @@ -483,14 +520,18 @@ void __attribute__ ((format (gnu_printf, 6, 7))) __bbs_log(enum bbs_log_level lo
term_puts(fullbuf);
RWLIST_RDLOCK(&remote_log_fds);
RWLIST_TRAVERSE(&remote_log_fds, rfd, entry) {
if (loglevel == LOG_DEBUG && level > rfd->maxdebug) {
/* Skip consoles that don't want debug messages of this debug level */
continue;
}
/* Prevent libc_write from blocking if there's a ton of logging going on. */
bbs_unblock_fd(rfd->fd);
if (fd_logging[rfd->fd]) {
ssize_t wres = write(rfd->fd, fullbuf, (size_t) bytes);
if (wres != (ssize_t) bytes) {
/* Well, we can't log a message if we failed to log a message.
* That would probably not work out well. */
fprintf(stderr, "Failed to log %d-byte message\n", bytes);
fprintf(stderr, "Failed to log %d-byte message (wrote %ld): %s\n", bytes, wres, strerror(errno));
}
}
bbs_block_fd(rfd->fd);
Expand Down

0 comments on commit 457fdcf

Please sign in to comment.