apply new logging macros

Wed, 23 Apr 2025 23:43:45 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 23 Apr 2025 23:43:45 +0200
changeset 92
78ce93fb46e5
parent 91
8433c87c0f51
child 93
52611a99e574

apply new logging macros

src/context.c file | annotate | diff | comparison | revisions
src/font.c file | annotate | diff | comparison | revisions
src/glcontext.c file | annotate | diff | comparison | revisions
src/shader.c file | annotate | diff | comparison | revisions
src/text.c file | annotate | diff | comparison | revisions
src/texture.c file | annotate | diff | comparison | revisions
src/window.c file | annotate | diff | comparison | revisions
--- a/src/context.c	Tue Apr 22 19:36:27 2025 +0200
+++ b/src/context.c	Wed Apr 23 23:43:45 2025 +0200
@@ -79,11 +79,11 @@
     // initialize SDL
     const int supported_img_flags = IMG_INIT_PNG | IMG_INIT_JPG;
     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
-        asc_error(SDL_GetError());
+        asc_error("Failed to initialize SDL: %s", SDL_GetError());
     } else if (TTF_Init() < 0) {
-        asc_error(TTF_GetError());
+        asc_error("Failed to initialize SDL_ttf: %s", TTF_GetError());
     } else if (IMG_Init(supported_img_flags) != supported_img_flags) {
-        asc_error(IMG_GetError());
+        asc_error("Failed to initialize SDL_img: %s", IMG_GetError());
     }
     SDL_ClearError();
     asc_context.total_nanos = asc_nanos();
--- a/src/font.c	Tue Apr 22 19:36:27 2025 +0200
+++ b/src/font.c	Wed Apr 23 23:43:45 2025 +0200
@@ -89,16 +89,17 @@
 
     struct asc_font_cache_entry entry;
     entry.font = font;
-    cxmutstr fpath = asc_filesystem_combine_paths(cx_strcast(asc_context.font_path), cx_str(asc_font_filename(font.style)));
+    const char *font_name = asc_font_filename(font.style);
+    cxmutstr fpath = asc_filesystem_combine_paths(cx_strcast(asc_context.font_path), cx_str(font_name));
     asc_dprintf("Load font from %" CX_PRIstr, CX_SFMT(fpath));
     entry.ttf = TTF_OpenFont(fpath.ptr, font.size);
     cx_strfree(&fpath);
     if (entry.ttf == NULL) {
-        asc_error(TTF_GetError());
+        asc_error("Failed to load font %s: %s", font_name, TTF_GetError());
         return NULL;
     } else {
         cxListAdd(asc_font_cache, &entry);
-        asc_dprintf("Loaded font size %d, style %d from disk", font.size, font.style);
+        asc_dprintf("Loaded font size %d, style %d from %s", font.size, font.style, font_name);
         return entry.ttf;
     }
 }
--- a/src/glcontext.c	Tue Apr 22 19:36:27 2025 +0200
+++ b/src/glcontext.c	Wed Apr 23 23:43:45 2025 +0200
@@ -37,15 +37,13 @@
         GLsizei length, const GLchar* message,
         const void* userParam
 ) {
-    cxmutstr buf = cx_asprintf(
-            "source = %d, id = %u, type = %d, severity= %d, message = %.*s",
+    if (type == GL_DEBUG_TYPE_ERROR) {
+        asc_error("OpenGL source = %d, id = %u, type = %d, severity= %d, message = %.*s",
             source, id, type, severity, length, message);
-    if (type == GL_DEBUG_TYPE_ERROR) {
-        asc_error(buf.ptr);
     } else {
-        asc_dprintf("GL debug: %.*s", (int)buf.length, buf.ptr);
+        asc_dprintf("OpenGL source = %d, id = %u, type = %d, severity= %d, message = %.*s",
+            source, id, type, severity, length, message);
     }
-    cx_strfree(&buf);
 }
 
 static int asc_primitives_init(AscGLContext *context) {
@@ -83,9 +81,7 @@
         0, 1, 1, 32,
         0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
     if (white1x1 == NULL) {
-        // TODO: merge error messages once asc_error allows formatting
-        asc_error("Failed to create surface");
-        asc_error(SDL_GetError());
+        asc_error("Failed to create surface: %s", SDL_GetError());
         return 1;
     }
     SDL_FillRect(white1x1, NULL, 0xFFFFFFFF);
@@ -144,7 +140,7 @@
 
         return true;
     } else {
-        asc_error(glewGetErrorString(err));
+        asc_error("glewInit failed: %s", glewGetErrorString(err));
         SDL_GL_DeleteContext(ctx->glctx);
         return false;
     }
--- a/src/shader.c	Tue Apr 22 19:36:27 2025 +0200
+++ b/src/shader.c	Wed Apr 23 23:43:45 2025 +0200
@@ -47,7 +47,7 @@
 static unsigned asc_shader_compile(unsigned int type, char const *code) {
     GLuint id = glCreateShader(type);
     if (id == 0) {
-        asc_error("glCreateShader failed");
+        asc_error("glCreateShader failed: %s", glGetError());
         return 0;
     }
 
@@ -63,7 +63,7 @@
         char *log = malloc(1024);
         glGetShaderInfoLog(id, 1024, NULL, log);
         glDeleteShader(id);
-        asc_error(log);
+        asc_error("Shader %u compilation failed.\n%s", id, log);
         free(log);
         return 0;
     }
@@ -82,7 +82,7 @@
     GLint success;
     GLint id = glCreateProgram();
     if (id <= 0) {
-        asc_error("glCreateProgram failed");
+        asc_error("glCreateProgram failed: %s", glGetError());
         return (AscShaderProgram) {0};
     }
     for (unsigned i = 0; i < n; i++) {
@@ -106,7 +106,7 @@
         char *log = malloc(1024);
         glGetProgramInfoLog(id, 1024, NULL, log);
         glDeleteProgram(id);
-        asc_error(log);
+        asc_error("Linking shader program %u failed.\n%s", id, log);
         free(log);
         return (AscShaderProgram) {0};
     }
--- a/src/text.c	Tue Apr 22 19:36:27 2025 +0200
+++ b/src/text.c	Wed Apr 23 23:43:45 2025 +0200
@@ -51,7 +51,7 @@
             font, text->text.ptr, asc_col_sdl(text->color), text->max_width
     );
     if (surface == NULL) {
-        asc_error(SDL_GetError());
+        asc_error("Rendering TTF surface failed: %s", SDL_GetError());
         return;
     }
     asc_set_scale2d(node, surface->w, surface->h);
--- a/src/texture.c	Tue Apr 22 19:36:27 2025 +0200
+++ b/src/texture.c	Wed Apr 23 23:43:45 2025 +0200
@@ -39,7 +39,9 @@
     glActiveTexture(GL_TEXTURE0 + unit);
     GLenum error = glGetError();
     if (error == GL_INVALID_ENUM) {
-        asc_error("Tried to use more texture units than available.");
+        GLint max_units;
+        glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_units);
+        asc_error("Tried to use more texture units than available (%u/%u).", unit, max_units);
     }
     glBindTexture(tex->target, tex->tex_id);
     glUniform1i(uniform_location, unit);
@@ -48,8 +50,7 @@
 
 void asc_texture_from_surface(AscTexture *tex, SDL_Surface const *surface) {
     if (tex->tex_id == 0) {
-        asc_error("Tried to use uninitialized texture.");
-        asc_dprintf("Texture address: %"PRIxPTR, (uintptr_t) tex);
+        asc_error("Tried to use uninitialized texture (%"PRIxPTR ").", (uintptr_t) tex);
         return;
     }
     tex->width = surface->w;
@@ -81,8 +82,7 @@
             format = GL_BGRA;
             break;
         default:
-            // TODO: add more output once asc_error allows format strings
-            asc_error("Unsupported pixel format.");
+            asc_error("Unsupported pixel format: %x", surface->format->format);
             return;
     }
     glTexImage2D(tex->target, 0, internal_format,
@@ -153,8 +153,7 @@
     GLuint textures[count];
     for (unsigned i = 0; i < count; ++i) {
         if (tex[i].refcount > 0) {
-            // TODO: asc_wprintf() for warnings
-            asc_dprintf("Texture %u still in use by %u objects.",
+            asc_wprintf("Texture %u still in use by %u objects.",
                 tex[i].tex_id, tex[i].refcount);
         }
         asc_dprintf("Destroy texture: %u", tex[i].tex_id);
--- a/src/window.c	Tue Apr 22 19:36:27 2025 +0200
+++ b/src/window.c	Wed Apr 23 23:43:45 2025 +0200
@@ -45,17 +45,16 @@
 
 void asc_window_initialize(unsigned int index, AscWindowSettings const *settings) {
     if (index >= ASC_MAX_WINDOWS) {
-        asc_error("Maximum number of windows exceeded.");
+        asc_error("Maximum number of windows exceeded (%u/%u).", index, ASC_MAX_WINDOWS);
         return;
     }
     AscWindow *window = &asc_context.windows[index];
     if (window->id > 0) {
-        asc_error("Cannot create window - slot already occupied.");
-        asc_dprintf("Tried to create window with index %u twice", index);
+        asc_error("Cannot create window - slot %u already occupied.", index);
         return;
     }
     if (window->ui != NULL) {
-        asc_dprintf("Window with index %u has a dangling UI pointer", index);
+        asc_wprintf("Window with index %u has a dangling UI pointer", index);
         asc_scene_node_free(window->ui);
     }
 
@@ -71,7 +70,7 @@
             flags
     );
     if (window->window == NULL) {
-        asc_error(SDL_GetError());
+        asc_error("Creating Window failed: %s", SDL_GetError());
         return;
     }
 
@@ -87,7 +86,7 @@
         asc_dprintf("Window %u initialized", window->id);
         asc_context.active_window = index;
     } else {
-        asc_dprintf("Creating GL context failed for window %u", window->id);
+        asc_error("Creating GL context failed for window %u", window->id);
         // cleanup on error
         SDL_DestroyWindow(window->window);
         window->window = NULL;

mercurial