From a91f26259ebf5aaaf4cea6957b84975657cfa6f0 Mon Sep 17 00:00:00 2001 From: Quinn Date: Thu, 13 Feb 2025 14:51:01 +0100 Subject: [PATCH] implement / re-implement error-handling --- .vscode/launch.json | 3 +++ src/error_handling.c | 54 ++++++++++++++++++++++++++++++++++++++++++++ src/error_handling.h | 33 +++++++++++++++++++++++++++ src/main.c | 15 +++++++++--- src/main.h | 5 ++++ 5 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 src/error_handling.c create mode 100644 src/error_handling.h create mode 100644 src/main.h diff --git a/.vscode/launch.json b/.vscode/launch.json index ea127f6..8f20541 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -5,6 +5,9 @@ "name": "(lldb) debug project", "type": "lldb", "request": "launch", + "env": { + "DEBUG": "1" + }, "program": "", "linux": { "preLaunchTask": "(dbg) compile linux-x86_64", diff --git a/src/error_handling.c b/src/error_handling.c new file mode 100644 index 0000000..b4015fd --- /dev/null +++ b/src/error_handling.c @@ -0,0 +1,54 @@ +#include "error_handling.h" + +#include +#include +#include +#include + +#include "SDL_messagebox.h" +#include "main.h" + +#define PRINT_BUFFER_SIZE 128 // defines the buffer size for printing + +// writes the arguments to the specified buffer +#define write_args(buf, fmt) \ + va_list args; \ + va_start(args, fmt); \ + (void)vsnprintf(buf, PRINT_BUFFER_SIZE, fmt, args); \ + va_end(args); + +// sets the game status (in turn exiting (if not set to STATUS_RUNNING)) +static void stop(gamestatus status) { + set_gamestatus(status); +} + +void debug(char const* fmt, ...) { + char const* env = getenv("DEBUG"); + if (env == NULL || *env != '1') + return; + + char buf[PRINT_BUFFER_SIZE] = {0}; + write_args(buf, fmt); + + (void)fprintf(stdout, "\033[95m%s\033[0m\n", buf); +} + +void info(char const* fmt, ...) { + char buf[PRINT_BUFFER_SIZE] = {0}; + write_args(buf, fmt); + (void)fprintf(stdout, "\033[0m%s\033[0m\n", buf); // write colour here for consistency +} + +void warn(char const* fmt, ...) { + char buf[PRINT_BUFFER_SIZE] = {0}; + write_args(buf, fmt); + (void)fprintf(stderr, "\033[93mW: %s\033[0m\n", buf); +} + +void error(gamestatus status, char const* fmt, ...) { + char buf[PRINT_BUFFER_SIZE] = {0}; + write_args(buf, fmt); + (void)fprintf(stderr, "\033[91mE: %s\033[0m\n", buf); + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "something went wrong! :O", buf, NULL); + stop(status); +} diff --git a/src/error_handling.h b/src/error_handling.h new file mode 100644 index 0000000..725d441 --- /dev/null +++ b/src/error_handling.h @@ -0,0 +1,33 @@ +#pragma once + +#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 { + // 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 + + STATUS_RUNNING = -1, + // clang-format on +}; +typedef int8_t gamestatus; + +void debug(char const*, ...); // prints a debug message to stdout if the DEBUG environment variable is set, otherwise the call is ignored. +void info(char const*, ...); // prints an info message to stdout +void warn(char const*, ...); // prints a warning message to stderr +void error(gamestatus, char const*, ...); // prints an error message to stderr diff --git a/src/main.c b/src/main.c index 3e4165a..94e72b7 100644 --- a/src/main.c +++ b/src/main.c @@ -1,8 +1,17 @@ +#include "main.h" + #include -#include + +#include "error_handling.h" + +static gamestatus status = STATUS_RUNNING; + +void set_gamestatus(gamestatus new_status) { + status = new_status; +} int32_t main(int32_t argc, char** argv) { (void)argc, (void)argv; - printf("%s\n", "Hello, World!"); - return 0; + debug("%s", "Hello, World!"); + return status; } diff --git a/src/main.h b/src/main.h new file mode 100644 index 0000000..12338fb --- /dev/null +++ b/src/main.h @@ -0,0 +1,5 @@ +#pragma once + +#include "error_handling.h" + +void set_gamestatus(gamestatus); // sets the status of the game (allows for quitting the game)