diff --git a/src/error.h b/src/error.h index a8ab993..1a3464a 100644 --- a/src/error.h +++ b/src/error.h @@ -1,69 +1,17 @@ // Copyright (c) 2025 Quinn // Licensed under the MIT Licence. See LICENSE for details #pragma once -#include -#include -#include -#include "util/atrb.h" +#define ERROR_STR_PRE(v) #v +#define ERROR_STR(v) ERROR_STR_PRE(v) -#define ERROR_BUF_SIZE 128 +#define debug(s, ...) printf("\033[95m" __FILE__ ":" ERROR_STR(__LINE__) ": [DBG]: " s "\033[0m\n", ##__VA_ARGS__) +#define info(s, ...) printf(__FILE__ ":" ERROR_STR(__LINE__) ": [INF]: " s "\n", ##__VA_ARGS__) +#define warn(s, ...) fprintf(stderr, "\033[93m" __FILE__ ":" ERROR_STR(__LINE__) ": [WAR]: " s "\033[0m\n", ##__VA_ARGS__) +#define error(s, ...) fprintf(stderr, "\033[91m" __FILE__ ":" ERROR_STR(__LINE__) ": [ERR]: " s "\033[0m\n", ##__VA_ARGS__) -atrb_nonnull(1) atrb_format(printf, 1, 2) static inline void debug(char const*, ...); -atrb_nonnull(1) atrb_format(printf, 1, 2) static inline void info(char const*, ...); -atrb_nonnull(1) atrb_format(printf, 1, 2) static inline void warn(char const*, ...); -atrb_nonnull(1) atrb_format(printf, 1, 2) static inline void error(char const*, ...); -atrb_nonnull(1) atrb_format(printf, 1, 2) atrb_noreturn static inline void fatal(char const*, ...); -atrb_nonnull(2) static inline void error_callback(int err, char const* const msg); - -// macro to write fmt vardiac args to buf -#define WRITE_VA_ARGS(buf, fmt) \ - { \ - va_list args; \ - va_start(args, fmt); \ - vsnprintf(buf, ERROR_BUF_SIZE, fmt, args); \ - va_end(args); \ - }; - -// prints a '\n' suffixed debug msg to stdout with a maximum buf size of @ref ERROR_BUF_SIZE -void debug(char const* restrict fmt, ...) { - char buf[ERROR_BUF_SIZE] = {0}; - WRITE_VA_ARGS(buf, fmt); - printf("\033[95mD: \"%s\"\033[0m\n", buf); // reduced parameter use by not using fprintf -} - -// prints a '\n' suffixed info msg to stdout with a maximum buf size of @ref ERROR_BUF_SIZE -void info(char const* fmt, ...) { - char buf[ERROR_BUF_SIZE] = {0}; - WRITE_VA_ARGS(buf, fmt); - printf("I: \"%s\"\n", buf); -} - -// prints a '\n' suffixed warn msg to stderr with a maximum buf size of @ref ERROR_BUF_SIZE -void warn(char const* fmt, ...) { - char buf[ERROR_BUF_SIZE] = {0}; - WRITE_VA_ARGS(buf, fmt); - fprintf(stderr, "\033[93mW: \"%s\"\033[0m\n", buf); -} - -// prints a '\n' suffixed error msg to stderr with a maximum buf size of @ref ERROR_BUF_SIZE -void error(char const* fmt, ...) { - char buf[ERROR_BUF_SIZE] = {0}; - WRITE_VA_ARGS(buf, fmt); - fprintf(stderr, "\033[91mE: \"%s\"\033[0m\n", buf); -} - -// prints a '\n' suffixed fatal msg to stderr with a maximum buf size of @ref ERROR_BUF_SIZE and immediately aborts execution -void fatal(char const* fmt, ...) { - char buf[ERROR_BUF_SIZE] = {0}; - WRITE_VA_ARGS(buf, fmt); - fprintf(stderr, "\033[101mF: \"%s\"\033[0m\n", buf); - abort(); -} - -// callback for GLFW errors -void error_callback(int err, char const* const msg) { - fprintf(stderr, "\033[91mE: glfw returned (%i); \"%s\"\033[0m\n", err, msg); -} - -#undef WRITE_VA_ARGS +#define fatal(s, ...) \ + do { \ + printf("\033[101m" __FILE__ ":" ERROR_STR(__LINE__) ": [FAT]: " s "\033[0m\n", ##__VA_ARGS__); \ + abort(); \ + } while (0) diff --git a/src/io/render.c b/src/io/render.c index edfab46..c6f4d2a 100644 --- a/src/io/render.c +++ b/src/io/render.c @@ -6,6 +6,7 @@ #include #include +#include #include #include "../error.h" diff --git a/src/io/shader.c b/src/io/shader.c index 6b44c08..e0ce01b 100644 --- a/src/io/shader.c +++ b/src/io/shader.c @@ -4,6 +4,7 @@ #include #include +#include #include "../error.h" diff --git a/src/io/window.c b/src/io/window.c index 7c98741..e37aaca 100644 --- a/src/io/window.c +++ b/src/io/window.c @@ -6,6 +6,7 @@ #include #include +#include #include #include "../error.h" @@ -46,11 +47,16 @@ int window_init(void) { glfwSetKeyCallback(win, key_callback); // print the OpenGL version information - debug("version info:"); - debug("\tvendor: %s", glGetString(GL_VENDOR)); - debug("\trenderer: %s", glGetString(GL_RENDERER)); - debug("\tversion: %s", glGetString(GL_VERSION)); - debug("\tshading lang: %s", glGetString(GL_SHADING_LANGUAGE_VERSION)); + debug( + "version info:\n" + "\tvendor: %s\n" + "\trenderer: %s\n" + "\tversion: %s\n" + "\tshading lang: %s\n", + glGetString(GL_VENDOR), + glGetString(GL_RENDERER), + glGetString(GL_VERSION), + glGetString(GL_SHADING_LANGUAGE_VERSION)); return 0; } diff --git a/src/main.c b/src/main.c index 62da5aa..143eab7 100644 --- a/src/main.c +++ b/src/main.c @@ -5,6 +5,7 @@ #undef GLAD_GL_IMPLEMENTATION #include +#include #include #include "error.h" @@ -15,6 +16,11 @@ #define WIN_DEFAULT_WIDTH 640 #define WIN_DEFAULT_HEIGHT 480 +// callback for GLFW errors +static void error_callback(int err, char const* const msg) { + fprintf(stderr, "\033[91mE: glfw returned (%i); \"%s\"\033[0m\n", err, msg); +} + static inline int init(void) { glfwSetErrorCallback(error_callback); glfwInitHint(GLFW_JOYSTICK_HAT_BUTTONS, GLFW_FALSE); // disable joystick buttons @@ -31,7 +37,7 @@ static inline void quit(void) { int main(int argc, char** argv) { (void)argc, (void)argv; - + printf("debug: [DBG], info: [INF], warning: [WAR], error: [ERR], fatal: [FAT]\n"); if (init()) fatal("failed to initialize!"); window_loop();