From 68ea92a4f8fc579f5a368064f392bb9e45dcaeed Mon Sep 17 00:00:00 2001 From: Bailey Danyluk Date: Sat, 16 Sep 2023 16:59:22 -0600 Subject: [PATCH 1/4] add logging channels --- include/logger.h | 41 ++++++++++++++++++++++++++++++++++++----- src/engine.c | 15 +++++++++------ src/logger.c | 32 ++++++++++++++++++-------------- src/render.c | 1 + src/render/vertex.c | 1 + 5 files changed, 65 insertions(+), 25 deletions(-) diff --git a/include/logger.h b/include/logger.h index d7ad72a..79a2328 100644 --- a/include/logger.h +++ b/include/logger.h @@ -1,3 +1,18 @@ +#ifndef VESTIGE_LOG_CHANNEL +#define VESTIGE_LOG_CHANNEL LOG_CHANNEL_GENERIC +#endif + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments" + +#define log_info(format, ...) log_info_to_channel((VESTIGE_LOG_CHANNEL), (format), ##__VA_ARGS__) +#define log_warning(format, ...) log_warning_to_channel((VESTIGE_LOG_CHANNEL), (format), ##__VA_ARGS__) +#define log_error(format, ...) log_error_to_channel((VESTIGE_LOG_CHANNEL), (format), ##__VA_ARGS__) +#define log_debug(format, ...) log_debug_to_channel((VESTIGE_LOG_CHANNEL), (format), ##__VA_ARGS__) + +#pragma clang diagnostic pop + + #ifndef LOGGER_H #define LOGGER_H #include @@ -9,6 +24,14 @@ typedef enum LogLevel { ERROR = 1 << 0, } LogLevel; +typedef enum LogChannel { + LOG_CHANNEL_GENERIC = 1 << 0, + LOG_CHANNEL_CORE = 1 << 1, + LOG_CHANNEL_ENGINE = 1 << 2, + LOG_CHANNEL_RENDERER = 1 << 3, + LOG_CHANNEL_AUDIO = 1 << 4, +} LogChannel; + static const char* LOG_LEVEL_STR[] = { "[ERROR]: ", "[WARN]: ", @@ -16,17 +39,25 @@ static const char* LOG_LEVEL_STR[] = { "[DEBUG]: ", }; +static const char* LOG_CHANNEL_STR[] = { + "(GENERIC)", + "(CORE)", + "(ENGINE)", + "(RENDERER)", + "(AUDIO)", +}; + typedef struct Logger { LogLevel levels; } Logger; extern Logger* LOGGER; -void log_debug(const char* format, ...); -void log_info(const char* format, ...); -void log_warning(const char* format, ...); -void log_error(const char* format, ...); -void log_level(LogLevel level, const char* format, ...); +void log_debug_to_channel(LogChannel channel, const char* format, ...); +void log_info_to_channel(LogChannel channel, const char* format, ...); +void log_warning_to_channel(LogChannel channel, const char* format, ...); +void log_error_to_channel(LogChannel channel, const char* format, ...); +void log_level_to_channel(LogChannel channel, LogLevel level, const char* format, ...); void logger_start(void); void logger_stop(void); diff --git a/src/engine.c b/src/engine.c index 9d02143..c737a4e 100644 --- a/src/engine.c +++ b/src/engine.c @@ -2,6 +2,7 @@ #include #include "engine.h" +#define VESTIGE_LOG_CHANNEL LOG_CHANNEL_ENGINE #include "logger.h" #include "render.h" #include "render/color.h" @@ -85,7 +86,6 @@ void engine_tick(void) { thread_sleep_for(time_from_nanoseconds(sleep_time_nanoseconds)); } } - log_debug("FPS: %.2f", 1.f / time_as_seconds(time_elapsed(tick_start, current_time()))); /* 1/f == seconds/frame 1/f * 1e6 == microseconds/frame @@ -99,10 +99,12 @@ void graphics_init(void) { if (!glfwInit()) { engine_crash(SHUTDOWN_CANT_INIT_GLFW); } + log_info("GLFW initialised"); if (!create_window(&ENGINE->window)) { engine_crash(SHUTDOWN_CANT_INIT_WINDOW); } + log_info("Window created"); glfwMakeContextCurrent(ENGINE->window.window); @@ -118,6 +120,7 @@ void graphics_init(void) { void graphics_deinit(void) { destroy_window(&ENGINE->window); + log_info("Window destroyed"); glfwTerminate(); log_info("Graphics deinitialised"); } @@ -125,14 +128,13 @@ void graphics_deinit(void) { void engine_start(void) { logger_start(); Time engine_start_time = current_time(); - log_info("Starting" ENGINE_NAME "...."); + log_info("Starting " ENGINE_NAME "...."); ENGINE = malloc(sizeof(Engine)); ENGINE->shutdown_reason = SHUTDOWN_NORMAL; ENGINE->engine_clock = new_clock(); ENGINE->fps = DEFAULT_ENGINE_FPS; - log_info("Core started"); - + log_info("Engine core started"); graphics_init(); @@ -142,7 +144,6 @@ void engine_start(void) { ._accumulator = 0.f }; log_info("Simulation started"); - log_info(ENGINE_NAME " started"); ENGINE->game = (GameManager) { .game_clock = new_clock(), @@ -151,9 +152,11 @@ void engine_start(void) { ._pops_queued = 0 }; VECTOR_PUSH(GameStateEnum, &ENGINE->game._active_states, GAME_STATE_TOMBSTONE); - log_info("Game manager started"); + + ENGINE_RUNNING = true; + log_info(ENGINE_NAME " started"); Time engine_end_time = current_time(); log_info("Startup took %.2f seconds", time_as_seconds(time_elapsed(engine_start_time, engine_end_time))); } diff --git a/src/logger.c b/src/logger.c index 27ac5aa..ba8a8c3 100644 --- a/src/logger.c +++ b/src/logger.c @@ -3,15 +3,15 @@ #include #include -#define LOG_WITH_LEVEL(level, format) \ +#define LOG_WITH_LEVEL(channel, level, format) \ va_list args; \ va_start(args, format); \ - log_impl(level, format, args); \ + log_impl_to_channel(channel, level, format, args); \ va_end(args) Logger* LOGGER = NULL; -void log_impl(LogLevel level, const char* format, va_list args) { +void log_impl_to_channel(LogChannel channel, LogLevel level, const char* format, va_list args) { if ((level & LOGGER->levels) == 0) { return; } @@ -19,35 +19,39 @@ void log_impl(LogLevel level, const char* format, va_list args) { int level_index = 0; for (; ((level >> level_index) & 1) == 0; level_index++) { } + 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"); } -void log_debug(const char* format, ...) { - LOG_WITH_LEVEL(DEBUG, format); +void log_debug_to_channel(LogChannel channel, const char* format, ...) { + LOG_WITH_LEVEL(channel, DEBUG, format); } -void log_info(const char* format, ...) { - LOG_WITH_LEVEL(INFO, format); +void log_info_to_channel(LogChannel channel, const char* format, ...) { + LOG_WITH_LEVEL(channel, INFO, format); } -void log_warning(const char* format, ...) { - LOG_WITH_LEVEL(WARN, format); +void log_warning_to_channel(LogChannel channel, const char* format, ...) { + LOG_WITH_LEVEL(channel, WARN, format); } -void log_error(const char* format, ...) { - LOG_WITH_LEVEL(ERROR, format); +void log_error_to_channel(LogChannel channel, const char* format, ...) { + LOG_WITH_LEVEL(channel, ERROR, format); } -void log_level(LogLevel level, const char* format, ...) { - LOG_WITH_LEVEL(level, format); +void log_level_to_channel(LogChannel channel, LogLevel level, const char* format, ...) { + LOG_WITH_LEVEL(channel, level, format); } void logger_start(void) { LOGGER = malloc(sizeof(Logger)); LOGGER->levels = DEBUG | INFO | WARN | ERROR; - log_info("Logger Started"); + log_info_to_channel(LOG_CHANNEL_CORE, "Logger Started"); } void logger_stop(void) { diff --git a/src/render.c b/src/render.c index 74f1d7e..429f7a7 100644 --- a/src/render.c +++ b/src/render.c @@ -2,6 +2,7 @@ #include "render.h" #include "engine.h" +#define VESTIGE_LOG_CHANNEL LOG_CHANNEL_RENDERER #include "logger.h" #include "render/vertex.h" diff --git a/src/render/vertex.c b/src/render/vertex.c index ee9f011..d49f4fe 100644 --- a/src/render/vertex.c +++ b/src/render/vertex.c @@ -1,6 +1,7 @@ #include #include "render/vertex.h" +#define VESTIGE_LOG_CHANNEL LOG_CHANNEL_RENDERER #include "logger.h" void bind_vertex_attributes(void) { From b3156c4cd600e5067608e58138cf2d986e1ce439 Mon Sep 17 00:00:00 2001 From: Bailey Danyluk Date: Sat, 16 Sep 2023 17:06:34 -0600 Subject: [PATCH 2/4] 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"); } From 4eabc5f6917e12f159c9adc7a7f67dfaa7cc7981 Mon Sep 17 00:00:00 2001 From: Bailey Danyluk Date: Sat, 16 Sep 2023 17:08:37 -0600 Subject: [PATCH 3/4] add more helper channel enums --- include/logger.h | 2 ++ src/logger.c | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/include/logger.h b/include/logger.h index 5750755..5385666 100644 --- a/include/logger.h +++ b/include/logger.h @@ -25,6 +25,8 @@ typedef enum LogLevel { } LogLevel; typedef enum LogChannel { + LOG_CHANNEL_NONE = 0, + LOG_CHANNEL_ALL = ~0, LOG_CHANNEL_GENERIC = 1 << 0, LOG_CHANNEL_CORE = 1 << 1, LOG_CHANNEL_ENGINE = 1 << 2, diff --git a/src/logger.c b/src/logger.c index 72b4bd7..fe0e23f 100644 --- a/src/logger.c +++ b/src/logger.c @@ -57,7 +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; + LOGGER->suppressed_channels_stdout = LOG_CHANNEL_NONE; log_info_to_channel(LOG_CHANNEL_CORE, "Logger Started"); } From dc4985942ef384c04a6d8bc91a9e9bec4e238e24 Mon Sep 17 00:00:00 2001 From: Bailey Danyluk Date: Sat, 16 Sep 2023 17:11:08 -0600 Subject: [PATCH 4/4] log renderer specific information from engine to channel --- src/engine.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/engine.c b/src/engine.c index c737a4e..01c9aa4 100644 --- a/src/engine.c +++ b/src/engine.c @@ -99,12 +99,12 @@ void graphics_init(void) { if (!glfwInit()) { engine_crash(SHUTDOWN_CANT_INIT_GLFW); } - log_info("GLFW initialised"); + log_info_to_channel(LOG_CHANNEL_RENDERER, "GLFW initialised"); if (!create_window(&ENGINE->window)) { engine_crash(SHUTDOWN_CANT_INIT_WINDOW); } - log_info("Window created"); + log_info_to_channel(LOG_CHANNEL_RENDERER, "Window created"); glfwMakeContextCurrent(ENGINE->window.window); @@ -115,14 +115,14 @@ void graphics_init(void) { glfwSwapInterval(0); - log_info("Graphics initalised"); + log_info_to_channel(LOG_CHANNEL_RENDERER, "Graphics initalised"); } void graphics_deinit(void) { destroy_window(&ENGINE->window); - log_info("Window destroyed"); + log_info_to_channel(LOG_CHANNEL_RENDERER, "Window destroyed"); glfwTerminate(); - log_info("Graphics deinitialised"); + log_info_to_channel(LOG_CHANNEL_RENDERER, "Graphics deinitialised"); } void engine_start(void) {