From d471c4238888f1dff70e02762219fa415ff07495 Mon Sep 17 00:00:00 2001 From: Quinn Date: Tue, 9 Sep 2025 16:31:17 +0200 Subject: [PATCH] rework `error` script --- src/error.h | 50 ----------------------------------------------- src/util/error.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ src/util/error.h | 22 +++++++++++++++++++++ 3 files changed, 73 insertions(+), 50 deletions(-) delete mode 100644 src/error.h create mode 100644 src/util/error.c create mode 100644 src/util/error.h diff --git a/src/error.h b/src/error.h deleted file mode 100644 index 657506f..0000000 --- a/src/error.h +++ /dev/null @@ -1,50 +0,0 @@ -#pragma once - -#include -#include - -/* defines statuses in the 0..127, any higher/negative values are POSIX-reserved. - * The max value (or -1) shall mean the application is running, anything else shall mean an exit code of some kind */ -enum gamestatus { - // clang-format off - STATUS_SUCCESS = 0, // 0; successful exit - STATUS_ERROR = 1, // miscellaneous error - ERROR_INIT = STATUS_ERROR | 2, // initialisation error - - ERROR_STD = STATUS_ERROR | 64, // standard library error - ERROR_STD_INIT = ERROR_INIT | 64, // standard library initialisation error - ERROR_STD_MEMORY = ERROR_STD | 32, // memory error - ERROR_STD_MEMORY_INIT = ERROR_STD_INIT | 32, // memory initialization error - - ERROR_SDL = STATUS_ERROR | 32, // SDL error - ERROR_SDL_INIT = ERROR_INIT | 32, // SDL initialization error - ERROR_SDL_RENDERING = ERROR_SDL | 16, // rendering error - ERROR_SDL_RENDERING_INIT = ERROR_SDL_INIT | 16, // rendering initialization error - ERROR_SDL_AUDIO = ERROR_SDL | 8, // audio error - ERROR_SDL_AUDIO_INIT = ERROR_SDL_INIT | 8, // audio initialization error - ERROR_SDL_FONT = ERROR_SDL | 4, // font error - ERROR_SDL_FONT_INIT = ERROR_SDL_INIT | 4, // font initialization error - - STATUS_RUNNING = -1, - // clang-format on -}; - -#if __INCLUDE_LEVEL__ > 0 -#include -#include -#include - -#include "util/macro.h" -#endif - -#define debug(s, ...) printf("\033[95m" __FILE__ ":" MACRO_STR2(__LINE__) ": [DBG]: " s "\033[0m\n" __VA_OPT__(, __VA_ARGS__)) -#define info(s, ...) printf(__FILE__ ":" MACRO_STR2(__LINE__) ": [INF]: " s "\n", __VA_OPT__(, __VA_ARGS__)) -#define warn(s, ...) fprintf(stderr, "\033[93m" __FILE__ ":" MACRO_STR2(__LINE__) ": [WAR]: " s "\033[0m\n" __VA_OPT__(, __VA_ARGS__)) -#define error(s, ...) fprintf(stderr, "\033[91m" __FILE__ ":" MACRO_STR2(__LINE__) ": [ERR]: " s "\033[0m\n" __VA_OPT__(, __VA_ARGS__)) - -#define fatal(c, s, ...) \ - do { \ - printf("\033[101m" __FILE__ ":" MACRO_STR2(__LINE__) ": [FAT]: " s "\033[0m\n" __VA_OPT__(, __VA_ARGS__)); \ - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "something went wrong! :O", "view stderr for full details: \n" s, NULL); \ - exit(c); \ - } while (0) diff --git a/src/util/error.c b/src/util/error.c new file mode 100644 index 0000000..ef76947 --- /dev/null +++ b/src/util/error.c @@ -0,0 +1,51 @@ +#include "error.h" + +#include +#include + +#include "intdef.h" + +static void error_log(FILE *restrict stream, const char *restrict pfx, uint ln, const char *restrict file, const char *restrict fmt, va_list ap) { + fprintf(stream, "(%s:%u) [%s] '", file, ln, pfx); + + vfprintf(stream, fmt, ap); + + fprintf(stream, "'\n"); +} + +void error_dbg(uint ln, const char *restrict file, const char *restrict fmt, ...) { + va_list ap; + va_start(ap, fmt); + error_log(stdout, "\033[95mDBG\033[0m", ln, file, fmt, ap); + va_end(ap); +} + +void error_inf(uint ln, const char *restrict file, const char *restrict fmt, ...) { + va_list ap; + va_start(ap, fmt); + error_log(stdout, "\033[93mINF\033[0m", ln, file, fmt, ap); + va_end(ap); +} + +void error_war(uint ln, const char *restrict file, const char *restrict fmt, ...) { + va_list ap; + va_start(ap, fmt); + error_log(stdout, "\033[91mWAR\033[0m", ln, file, fmt, ap); + va_end(ap); +} + +void error_err(uint ln, const char *restrict file, const char *restrict fmt, ...) { + va_list ap; + va_start(ap, fmt); + error_log(stdout, "\033[mFAT\033[0m", ln, file, fmt, ap); + va_end(ap); +} + +void error_fat(uint ln, const char *restrict file, const char *restrict fmt, ...) { + va_list ap; + va_start(ap, fmt); + error_log(stdout, "\033[mFAT\033[0m", ln, file, fmt, ap); + va_end(ap); + + exit(EXIT_FAILURE); +} diff --git a/src/util/error.h b/src/util/error.h new file mode 100644 index 0000000..47f1359 --- /dev/null +++ b/src/util/error.h @@ -0,0 +1,22 @@ + +/* Copyright (c) 2025 Quinn + * Licensed under the MIT Licence. See LICENSE for details */ +#pragma once + +#include +#include + +#include "atrb.h" +#include "intdef.h" + +void error_dbg(uint ln, const char *restrict file, const char *restrict fmt, ...); +void error_inf(uint ln, const char *restrict file, const char *restrict fmt, ...); +void error_war(uint ln, const char *restrict file, const char *restrict fmt, ...); +void error_err(uint ln, const char *restrict file, const char *restrict fmt, ...); +void error_fat(uint ln, const char *restrict file, const char *restrict fmt, ...) NORET; + +#define debug(s, ...) error_dbg(__LINE__, __FILE__, s, ##__VA_ARGS__) +#define info(s, ...) error_inf(__LINE__, __FILE__, s, ##__VA_ARGS__) +#define warn(s, ...) error_war(__LINE__, __FILE__, s, ##__VA_ARGS__) +#define error(s, ...) error_err(__LINE__, __FILE__, s, ##__VA_ARGS__) +#define fatal(s, ...) error_fat(__LINE__, __FILE__, s, ##__VA_ARGS__)