From b3156c4cd600e5067608e58138cf2d986e1ce439 Mon Sep 17 00:00:00 2001 From: Bailey Danyluk Date: Sat, 16 Sep 2023 17:06:34 -0600 Subject: [PATCH] suppress channel output to stdout selectively --- include/logger.h | 1 + src/logger.c | 15 +++++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/include/logger.h b/include/logger.h index 79a2328..5750755 100644 --- a/include/logger.h +++ b/include/logger.h @@ -49,6 +49,7 @@ static const char* LOG_CHANNEL_STR[] = { typedef struct Logger { LogLevel levels; + LogChannel suppressed_channels_stdout; } Logger; extern Logger* LOGGER; diff --git a/src/logger.c b/src/logger.c index ba8a8c3..72b4bd7 100644 --- a/src/logger.c +++ b/src/logger.c @@ -11,6 +11,13 @@ Logger* LOGGER = NULL; +void log_to_file(FILE* file, unsigned long long channel_index, int level_index, const char* format, va_list args) { + fprintf(file, "%s", LOG_CHANNEL_STR[channel_index]); + fprintf(file, "%s", LOG_LEVEL_STR[level_index]); + vfprintf(file, format, args); + fprintf(file, "\n"); +} + void log_impl_to_channel(LogChannel channel, LogLevel level, const char* format, va_list args) { if ((level & LOGGER->levels) == 0) { return; @@ -22,10 +29,9 @@ void log_impl_to_channel(LogChannel channel, LogLevel level, const char* format, unsigned long long channel_index = 0; for (; ((channel >> channel_index) & 1) == 0; channel_index++) { } - printf("%s", LOG_CHANNEL_STR[channel_index]); - printf("%s", LOG_LEVEL_STR[level_index]); - vprintf(format, args); - printf("\n"); + if ((channel & LOGGER->suppressed_channels_stdout) == 0) { + log_to_file(stdout, channel_index, level_index, format, args); + } } void log_debug_to_channel(LogChannel channel, const char* format, ...) { @@ -51,6 +57,7 @@ void log_level_to_channel(LogChannel channel, LogLevel level, const char* format void logger_start(void) { LOGGER = malloc(sizeof(Logger)); LOGGER->levels = DEBUG | INFO | WARN | ERROR; + LOGGER->suppressed_channels_stdout = 0; log_info_to_channel(LOG_CHANNEL_CORE, "Logger Started"); }