]> git.lizzy.rs Git - shadowclad.git/commitdiff
Add a logger!
authoroutfrost <kotlet.bahn@gmail.com>
Sun, 27 Jan 2019 00:09:55 +0000 (01:09 +0100)
committeroutfrost <kotlet.bahn@gmail.com>
Sun, 27 Jan 2019 00:09:55 +0000 (01:09 +0100)
Variadic functions, smh

Makefile
debugutil.c
level.c
logger.c [new file with mode: 0644]
logger.h [new file with mode: 0644]
main.c
performance.c

index e2eb7c9d25ed59c1a7d02737c0f3c67372263f42..2a3dfc1a640d3872ff40ae289ab8e63fcc758410 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -4,8 +4,8 @@ LDLIBS ::= -L/usr/local/lib -lGL -lGLEW -lglut -lassimp $(LDLIBS)
 
 # Prefix all object file names with the compilation directory
 objects ::= $(addprefix out/, \
-              main.o debugutil.o level.o performance.o \
-              render.o tga.o ui.o)
+              main.o debugutil.o level.o logger.o \
+              performance.o render.o tga.o ui.o)
 
 # Set executable extension for the platform
 ifeq ($(OS),Windows_NT)
index 80f5caadb194cf321fa036651e18f6deed06a6b3..3c105db2552c10ff14c4ac4b87605e888eba6fe9 100644 (file)
@@ -3,7 +3,7 @@
 #include <stdlib.h>
 #include <string.h>
 
-#include "assimp_types.h"
+//#include "assimp_types.h"
 
 char* getGlInfoString() {
        const char* glVersion = (const char*) glGetString(GL_VERSION);
@@ -24,7 +24,7 @@ char* getGlInfoString() {
        
        return glInfoString;
 }
-
+/*
 void dumpScene(FILE* stream, const AiScene* scene) {
        if (scene == NULL) {
                fprintf(stream, "NULL");
@@ -55,3 +55,4 @@ void dumpNode(FILE* stream, const AiNode* node) {
                        (*node).mNumMeshes,
                        (void*) (*node).mMeshes);
 }
+*/
diff --git a/level.c b/level.c
index 1388a928bbd49178beee0287d4f7500b33ee5aca..3469736ca5d3770fe8dafea3abbe55dd9b589da0 100644 (file)
--- a/level.c
+++ b/level.c
@@ -1,8 +1,8 @@
 #include <GL/gl.h>
 #include <assimp/cimport.h>
-#include <stdio.h> // TODO remove
 
 #include "level.h"
+#include "logger.h"
 
 const AiScene* levelScene = NULL;
 
@@ -25,7 +25,7 @@ void setImage(TgaImage* image) {
 const AiScene* importScene(const char* path) {
        const AiScene* scene = aiImportFile(path, 0u);
        if (scene == NULL) {
-               fprintf(stderr, "Asset import failed at file %s\n", path); // TODO factor logging the heck outta here
+               logError("Asset import failed (file: %s)", path);
        }
        return scene;
        // TODO aiReleaseImport(scene);
diff --git a/logger.c b/logger.c
new file mode 100644 (file)
index 0000000..23b0dd8
--- /dev/null
+++ b/logger.c
@@ -0,0 +1,40 @@
+#include <stdarg.h>
+#include <stdio.h>
+
+#include "logger.h"
+
+LogLevel logLevel = LOGLEVEL_DEBUG;
+
+void logMessage(LogLevel msgLevel, const char* file, int line, const char* message, ...) {
+       if (msgLevel > logLevel) {
+               return;
+       }
+       
+       const char* msgLevelString;
+       switch (msgLevel) {
+               case LOGLEVEL_ERROR:
+                       msgLevelString = "error: ";
+                       break;
+               case LOGLEVEL_WARNING:
+                       msgLevelString = "warning: ";
+                       break;
+               case LOGLEVEL_INFO:
+                       msgLevelString = "";
+                       break;
+               case LOGLEVEL_DEBUG:
+                       msgLevelString = "debug: ";
+                       break;
+               default:
+                       msgLevelString = "(invalid message level!) ";
+                       break;
+       }
+       
+       va_list args;
+       va_start(args, message);
+       
+       fprintf(stderr, "%s:%i: %s", file, line, msgLevelString);
+       vfprintf(stderr, message, args);
+       fputc('\n', stderr);
+       
+       va_end(args);
+}
diff --git a/logger.h b/logger.h
new file mode 100644 (file)
index 0000000..36aa647
--- /dev/null
+++ b/logger.h
@@ -0,0 +1,20 @@
+#ifndef LOGGER_H_
+#define LOGGER_H_
+
+typedef enum {
+       LOGLEVEL_ERROR,
+       LOGLEVEL_WARNING,
+       LOGLEVEL_INFO,
+       LOGLEVEL_DEBUG
+} LogLevel;
+
+LogLevel logLevel;
+
+#define logError(...) logMessage(LOGLEVEL_ERROR, __FILE__, __LINE__, __VA_ARGS__)
+#define logWarning(...) logMessage(LOGLEVEL_WARNING, __FILE__, __LINE__, __VA_ARGS__)
+#define logInfo(...) logMessage(LOGLEVEL_INFO, __FILE__, __LINE__, __VA_ARGS__)
+#define log(...) logMessage(LOGLEVEL_DEBUG, __FILE__, __LINE__, __VA_ARGS__)
+
+void logMessage(LogLevel msgLevel, const char* file, int line, const char* message, ...);
+
+#endif
diff --git a/main.c b/main.c
index cf8dcdf17d3eac7aedc3ae84cdacf4ae1e1d29ce..9cd2a99d753678c43073fe8eaefc74fbe3dc905b 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1,9 +1,9 @@
 #include <GL/glxew.h>
 #include <GL/glut.h>
-#include <stdio.h>
 
 #include "debugutil.h"
 #include "level.h"
+#include "logger.h"
 #include "performance.h"
 #include "render.h"
 #include "ui.h"
@@ -17,14 +17,19 @@ int main(int argc, char** argv) {
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
        glutCreateWindow(NULL);
        
-       glutSetWindowTitle(getGlInfoString());
+       // glutSetWindowTitle(getGlInfoString());
+       glutSetWindowTitle("shadowclad");
+       
+       logInfo("OpenGL %s", (const char*) glGetString(GL_VERSION));
+       logInfo("GLSL %s", (const char*) glGetString(GL_SHADING_LANGUAGE_VERSION));
+       logInfo("%s", (const char*) glGetString(GL_RENDERER));
        
        GLenum glewInitStatus = glewInit();
        if (glewInitStatus != GLEW_OK) {
-               fprintf(stderr, "GLEW init failed: %s\n", glewGetErrorString(glewInitStatus));
+               logError("GLEW init failed: %s", (const char*) glewGetErrorString(glewInitStatus));
                return 1;
        }
-       printf("GLEW %s\n", glewGetString(GLEW_VERSION));
+       logInfo("GLEW %s", (const char*) glewGetString(GLEW_VERSION));
        
        if (GLXEW_EXT_swap_control) {
                Display* display = glXGetCurrentDisplay();
@@ -33,15 +38,16 @@ int main(int argc, char** argv) {
                        glXSwapIntervalEXT(display, drawable, 1);
                }
                else {
-                       fprintf(stderr, "Drawable is not here\n");
+                       logWarning("Drawable is not here ¯\\_(ツ)_/¯");
+                       logWarning("Could not enable vsync (GLX_EXT_swap_control)");
                }
        }
        else if (GLXEW_MESA_swap_control) {
                glXSwapIntervalMESA(1);
-               printf("Swap interval %d\n", glXGetSwapIntervalMESA());
+               log("Vsync enabled with GLX_MESA_swap_control, swap interval %d", glXGetSwapIntervalMESA());
        }
        else {
-               fprintf(stderr, "Could not enable vsync\n");
+               logWarning("Could not enable vsync (extensions not supported)");
        }
        
        glutDisplayFunc(renderScene);
@@ -54,13 +60,6 @@ int main(int argc, char** argv) {
        initPerformanceMetering();
        initLevel();
        
-       /*
-       fprintf(stderr, "*model = ");
-       print_struct_aiScene(stderr, model);
-       fprintf(stderr, "\n*(*model).mRootNode = ");
-       print_struct_aiNode(stderr, (*model).mRootNode);
-       fprintf(stderr, "\n");
-       */
        glutMainLoop();
        return 0;
 }
index 2a1d13a8ff7ae999bef464db4afb28ed877e268a..267cf36622229885a4296509ba9e72d86b2cb913 100644 (file)
@@ -2,6 +2,8 @@
 #include <stdio.h> // TODO remove
 #include <time.h>
 
+#include "logger.h"
+
 typedef struct timespec Timepoint;
 
 static Timepoint lastDisplayTime;
@@ -10,7 +12,7 @@ static bool meteringEnabled = false;
 
 void initPerformanceMetering() {
        if (clock_gettime(CLOCK_MONOTONIC, &lastDisplayTime) != 0) {
-               fprintf(stderr, "Clock read failed, performance metering unavailable\n");
+               logWarning("Clock read failed, performance metering unavailable");
        }
        else {
                meteringEnabled = true;
@@ -23,7 +25,7 @@ void frameRendered() {
                Timepoint now;
                
                if (clock_gettime(CLOCK_MONOTONIC, &now) != 0) {
-                       fprintf(stderr, "Clock read failed, stopping performance metering\n");
+                       logWarning("Clock read failed, stopping performance metering");
                        meteringEnabled = false;
                        return;
                }
@@ -34,6 +36,7 @@ void frameRendered() {
                if (fullSeconds > 0) {
                        float seconds = (now.tv_nsec - lastDisplayTime.tv_nsec) / 1000000000.0f;
                        seconds += (float) (now.tv_sec - lastDisplayTime.tv_sec);
+                       // This goes to STDOUT because it's, uh, temporary
                        printf("frametime avg %.1f ms; fps avg %.f\n", (seconds / frames) * 1000.0f, (frames / seconds));
                        lastDisplayTime = now;
                        frames = 0;