src/error.c

changeset 91
8433c87c0f51
parent 81
84a546e282b7
--- 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) {

mercurial