Skip to content

Commit

Permalink
Merge pull request #11 from alidiusk/logger-improvements
Browse files Browse the repository at this point in the history
Add Logger channels
  • Loading branch information
TheCandianVendingMachine authored Sep 16, 2023
2 parents 09f4060 + dc49859 commit 3f635ce
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 30 deletions.
44 changes: 39 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,43 @@ typedef enum LogLevel {
ERROR = 1 << 0,
} 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,
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;
LogChannel suppressed_channels_stdout;
} 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
19 changes: 11 additions & 8 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_to_channel(LOG_CHANNEL_RENDERER, "GLFW initialised");

if (!create_window(&ENGINE->window)) {
engine_crash(SHUTDOWN_CANT_INIT_WINDOW);
}
log_info_to_channel(LOG_CHANNEL_RENDERER, "Window created");

glfwMakeContextCurrent(ENGINE->window.window);

Expand All @@ -113,26 +115,26 @@ 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_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) {
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
45 changes: 28 additions & 17 deletions src/logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,62 @@
#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_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;
}

int level_index = 0;
for (; ((level >> level_index) & 1) == 0; level_index++) { }

printf("%s", LOG_LEVEL_STR[level_index]);
vprintf(format, args);
printf("\n");
unsigned long long channel_index = 0;
for (; ((channel >> channel_index) & 1) == 0; channel_index++) { }

if ((channel & LOGGER->suppressed_channels_stdout) == 0) {
log_to_file(stdout, channel_index, level_index, format, args);
}
}

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");
LOGGER->suppressed_channels_stdout = LOG_CHANNEL_NONE;
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 3f635ce

Please sign in to comment.