commit 57a32176bbfe164acb286967765d1a9de257b4fe
parent d22b995239ef328984cb69a9ec0c8cec915bda32
Author: Christian Ermann <christianermann@gmail.com>
Date: Fri, 29 Jul 2022 14:26:03 -0700
Added logging system
Diffstat:
5 files changed, 103 insertions(+), 11 deletions(-)
diff --git a/include/logger.h b/include/logger.h
@@ -0,0 +1,45 @@
+#pragma once
+
+#define LOG_WARN_ENABLED 1
+#define LOG_INFO_ENABLED 1
+#define LOG_DEBUG_ENABLED 1
+#define LOG_TRACE_ENABLED 1
+
+typedef enum LogLevel {
+ LOG_LEVEL_TRACE = 0,
+ LOG_LEVEL_DEBUG = 1,
+ LOG_LEVEL_INFO = 2,
+ LOG_LEVEL_WARN = 3,
+ LOG_LEVEL_ERROR = 4,
+ LOG_LEVEL_FATAL = 5
+} log_level;
+
+void log_msg(log_level level, const char *message, ...);
+
+#define LOGF(message, ...) log_msg(LOG_LEVEL_FATAL, message, ##__VA_ARGS__);
+#define LOGE(message, ...) log_msg(LOG_LEVEL_ERROR, message, ##__VA_ARGS__);
+
+#if LOG_WARN_ENABLED
+#define LOGW(message, ...) log_msg(LOG_LEVEL_WARN , message, ##__VA_ARGS__);
+#else
+#define LOGW(message, ...)
+#endif
+
+#if LOG_INFO_ENABLED
+#define LOGI(message, ...) log_msg(LOG_LEVEL_INFO , message, ##__VA_ARGS__);
+#else
+#define LOGI(message, ...)
+#endif
+
+#if LOG_DEBUG_ENABLED
+#define LOGD(message, ...) log_msg(LOG_LEVEL_DEBUG, message, ##__VA_ARGS__);
+#else
+#define LOGD(message, ...)
+#endif
+
+#if LOG_TRACE_ENABLED
+#define LOGT(message, ...) log_msg(LOG_LEVEL_TRACE, message, ##__VA_ARGS__);
+#else
+#define LOGT(message, ...)
+#endif
+
diff --git a/src/app.c b/src/app.c
@@ -1,18 +1,18 @@
#include "app.h"
+#include "logger.h"
-#include <stdio.h>
#include <stdlib.h>
void error_callback(int error, const char* description)
{
- fprintf(stderr, "ERROR: %s\n", description);
+ LOGE("%s", description);
}
App *App_make(const AppInfo *app_info)
{
if (!glfwInit())
{
- fprintf(stderr, "ERROR: Failed to initialize GLFW.\n");
+ LOGF("Failed to initialize GLFW.");
return NULL;
}
@@ -37,7 +37,7 @@ App *App_make(const AppInfo *app_info)
);
if (!app->window)
{
- fprintf(stderr, "ERROR: Failed to create window.\n");
+ LOGF("Failed to create window.");
App_free(app);
return NULL;
}
@@ -48,7 +48,7 @@ App *App_make(const AppInfo *app_info)
if (!gladLoadGLLoader(glfwGetProcAddress))
{
- fprintf(stderr, "ERROR (App_make): Failed to initialize GLAD.\n");
+ LOGF("Failed to initialized GLAD.");
App_free(app);
return NULL;
}
diff --git a/src/logger.c b/src/logger.c
@@ -0,0 +1,41 @@
+#include "logger.h"
+
+#include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
+
+#define MAX_LOG_LENGTH 4096
+
+void log_msg(log_level level, const char *message, ...)
+{
+ char log_msg[MAX_LOG_LENGTH];
+ memset(log_msg, 0, sizeof(log_msg));
+
+ // Prefix with log level
+ const char *level_strings[6] = {
+ "[TRACE]: ",
+ "[DEBUG]: ",
+ "[INFO ]: ",
+ "[WARN ]: ",
+ "[ERROR]: ",
+ "[FATAL]: ",
+ };
+ const char *prefix = level_strings[level];
+ int prefix_length = strlen(prefix);
+ strncpy(log_msg, prefix, prefix_length);
+
+ // Append message
+ va_list arg_ptr;
+ va_start(arg_ptr, message);
+ vsnprintf(
+ log_msg + prefix_length,
+ MAX_LOG_LENGTH - prefix_length,
+ message,
+ arg_ptr
+ );
+ va_end(arg_ptr);
+
+ // Print log message
+ fprintf(stderr, "%s\n", log_msg);
+}
+
diff --git a/src/main.c b/src/main.c
@@ -7,15 +7,17 @@
#include "shader.h"
#include "transform.h"
#include "player.h"
+#include "logger.h"
#include "glad/glad.h"
#include "GLFW/glfw3.h"
#include <math.h>
-#include <stdio.h>
int main(int argc, char** argv)
{
+ LOGI("Application starting...");
+
AppInfo app_info = {
.title = "Marching Cubes Terrain",
.width = 1024,
@@ -131,5 +133,7 @@ int main(int argc, char** argv)
ChunkManager_free(&chunk_manager);
App_free(app);
+
+ LOGI("Application exiting...");
}
diff --git a/src/shader.c b/src/shader.c
@@ -1,4 +1,5 @@
#include "shader.h"
+#include "logger.h"
#include <stdio.h>
#include <stdlib.h>
@@ -9,7 +10,7 @@ static char* stringFromFile(const char* filename)
FILE* fp = fopen(filename, "r");
if (!fp)
{
- fprintf(stderr, "ERROR: Could not open file '%s'\n", filename);
+ LOGE("Failed to open file '%s'", filename);
return NULL;
}
@@ -27,8 +28,7 @@ static char* stringFromFile(const char* filename)
}
else
{
- fprintf(stderr, "ERROR: Failed to allocate string for file '%s'\n",
- filename);
+ LOGE("Failed to allocate string for file '%s'", filename);
}
fclose(fp);
return string;
@@ -50,7 +50,7 @@ static GLuint createAndCompileShader(const char* shader_string,
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &size);
char* string = (char*)malloc(size);
glGetShaderInfoLog(shader, size, &size, string);
- fprintf(stderr, "ERROR: Failed to compile shader: %s\n", string);
+ LOGE("Failed to compile shader: %s", string);
glDeleteShader(shader);
return 0;
}
@@ -75,7 +75,7 @@ static GLuint createAndLinkProgram(GLuint vertex_shader,
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &size);
char* string = (char*)malloc(size);
glGetProgramInfoLog(program, size, &size, string);
- fprintf(stderr, "ERROR: Failed to link program: %s\n", string);
+ LOGE("Failed to link program: %s", string);
glDeleteProgram(program);
return 0;
}
@@ -115,6 +115,8 @@ struct Shader {
Shader *Shader_make(const char* vert_filename, const char* frag_filename)
{
+ LOGI("Creating shader: %s %s", vert_filename, frag_filename);
+
GLuint program = createProgramFromFiles(vert_filename, frag_filename);
Shader *shader = malloc(sizeof *shader);