Skip to content

Commit

Permalink
suppress channel output to stdout selectively
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCandianVendingMachine committed Sep 16, 2023
1 parent 68ea92a commit b3156c4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
1 change: 1 addition & 0 deletions include/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ static const char* LOG_CHANNEL_STR[] = {

typedef struct Logger {
LogLevel levels;
LogChannel suppressed_channels_stdout;
} Logger;

extern Logger* LOGGER;
Expand Down
15 changes: 11 additions & 4 deletions src/logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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, ...) {
Expand All @@ -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");
}

Expand Down

0 comments on commit b3156c4

Please sign in to comment.