improve error.c functions

Tue, 22 Apr 2025 19:36:27 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 22 Apr 2025 19:36:27 +0200
changeset 91
8433c87c0f51
parent 90
aa8e7a38905c
child 92
78ce93fb46e5

improve error.c functions

src/ascension/error.h file | annotate | diff | comparison | revisions
src/error.c file | annotate | diff | comparison | revisions
src/texture.c file | annotate | diff | comparison | revisions
--- a/src/ascension/error.h	Mon Apr 21 17:52:01 2025 +0200
+++ b/src/ascension/error.h	Tue Apr 22 19:36:27 2025 +0200
@@ -31,16 +31,8 @@
 #include <cx/string.h>
 #include <stdio.h>
 
-void asc_error_cxstr(cxstring text);
-void asc_error_cchar(char const* text);
-void asc_error_cuchar(unsigned char const* text);
-
-#define asc_error(text) _Generic((text),     \
-    char const*: asc_error_cchar,            \
-    unsigned char const*: asc_error_cuchar,  \
-    char*: asc_error_cchar,                  \
-    unsigned char*: asc_error_cuchar,        \
-    cxstring: asc_error_cxstr)(text)
+void asc_error_impl(const char* file, unsigned line, char const* fmt, ...);
+#define asc_error(...) asc_error_impl(__FILE__, __LINE__, __VA_ARGS__)
 
 void asc_error_gl(unsigned code, cxstring message);
 
@@ -50,10 +42,11 @@
 char const* asc_get_error(void);
 void asc_clear_error(void);
 
+#define asc_wprintf(...) printf("[WARNING %s %u] ", __FILE__, __LINE__); printf(__VA_ARGS__); putchar('\n')
 #ifdef NDEBUG
 #define asc_dprintf(...)
 #else
-#define asc_dprintf(...) printf(__VA_ARGS__); putchar('\n')
+#define asc_dprintf(...) printf("[DEBUG %s %u] ", __FILE__, __LINE__); printf(__VA_ARGS__); putchar('\n')
 #endif
 
 #endif /* ASCENSION_ERROR_H */
--- a/src/error.c	Mon Apr 21 17:52:01 2025 +0200
+++ b/src/error.c	Tue Apr 22 19:36:27 2025 +0200
@@ -30,28 +30,25 @@
 #include "ascension/utils.h"
 
 #include <cx/buffer.h>
+#include <cx/printf.h>
 #include <GL/gl.h>
 
-void asc_error_cchar(char const* text) {
-    asc_error_cxstr(cx_str(text));
-}
-
-void asc_error_cuchar(unsigned char const* text) {
-    asc_error_cxstr(cx_str((char const*)text));
-}
+void asc_error_impl(const char* file, unsigned line, char const* fmt, ...) {
+    asc_set_flag(asc_context.flags, ASC_FLAG_HAS_ERROR);
 
-void asc_error_cxstr(cxstring text) {
-    if (text.length == 0) return;
-
-    // write error to debug output
-    asc_dprintf("ERROR: %.*s", (int)text.length, text.ptr);
+    // write to error buffer
+    va_list args;
+    va_start(args, fmt);
+    CxBuffer* buf = &asc_context.error_buffer;
+    size_t bufpos = buf->pos;
+    int written = cx_vfprintf(buf, cxBufferWriteFunc, fmt, args);
+    cxBufferPut(buf, '\n');
+    va_end(args);
 
-    // write error to buffer
-    CxBuffer* buf = &asc_context.error_buffer;
-    cxBufferWrite(text.ptr, 1, text.length, buf);
-    cxBufferPut(buf, '\n');
-
-    asc_set_flag(asc_context.flags, ASC_FLAG_HAS_ERROR);
+    // also print to stderr
+    // avoid double-formatting, get it directly from the buffer
+    fprintf(stderr, "[ERROR %s %u] %.*s\n",
+        file, line, written, buf->space+bufpos);
 }
 
 bool asc_has_error(void) {
@@ -100,9 +97,7 @@
         default:
             glerr = "unknown GL error";
     }
-    cxmutstr msg = cx_strcat(3, message, CX_STR(" GL Error: "), cx_str(glerr));
-    asc_error_cxstr(cx_strcast(msg));
-    cx_strfree(&msg);
+    asc_error("%s\nGL Error: %s", message, glerr);
 }
 
 int asc_error_catch_all_gl(void) {
--- a/src/texture.c	Mon Apr 21 17:52:01 2025 +0200
+++ b/src/texture.c	Tue Apr 22 19:36:27 2025 +0200
@@ -99,8 +99,7 @@
     SDL_Surface *image = IMG_Load(filepath.ptr);
     cx_strfree(&filepath);
     if (image == NULL) {
-        asc_error("Failed to load texture.");
-        asc_error(IMG_GetError());
+        asc_error("Failed to load texture: %s", IMG_GetError());
         return;
     }
     asc_texture_from_surface(tex, image);

mercurial