Skip to content

Commit

Permalink
add logging channels
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCandianVendingMachine committed Sep 16, 2023
1 parent 09f4060 commit 68ea92a
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 25 deletions.
41 changes: 36 additions & 5 deletions include/logger.h
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>
Expand All @@ -9,24 +24,40 @@ 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]: ",
"[INFO]: ",
"[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);

Expand Down
15 changes: 9 additions & 6 deletions src/engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <math.h>

#include "engine.h"
#define VESTIGE_LOG_CHANNEL LOG_CHANNEL_ENGINE
#include "logger.h"
#include "render.h"
#include "render/color.h"
Expand Down Expand Up @@ -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
Expand All @@ -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);

Expand All @@ -118,21 +120,21 @@ void graphics_init(void) {

void graphics_deinit(void) {
destroy_window(&ENGINE->window);
log_info("Window destroyed");
glfwTerminate();
log_info("Graphics deinitialised");
}

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();

Expand All @@ -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(),
Expand All @@ -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)));
}
Expand Down
32 changes: 18 additions & 14 deletions src/logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,55 @@
#include <stdlib.h>
#include <stdarg.h>

#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;
}

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) {
Expand Down
1 change: 1 addition & 0 deletions src/render.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "render.h"
#include "engine.h"
#define VESTIGE_LOG_CHANNEL LOG_CHANNEL_RENDERER
#include "logger.h"
#include "render/vertex.h"

Expand Down
1 change: 1 addition & 0 deletions src/render/vertex.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <glad/glad.h>

#include "render/vertex.h"
#define VESTIGE_LOG_CHANNEL LOG_CHANNEL_RENDERER
#include "logger.h"

void bind_vertex_attributes(void) {
Expand Down

0 comments on commit 68ea92a

Please sign in to comment.