implement / re-implement error-handling

This commit is contained in:
2025-02-13 14:51:01 +01:00
parent 70413c38c8
commit a91f26259e
5 changed files with 107 additions and 3 deletions

3
.vscode/launch.json vendored
View File

@@ -5,6 +5,9 @@
"name": "(lldb) debug project", "name": "(lldb) debug project",
"type": "lldb", "type": "lldb",
"request": "launch", "request": "launch",
"env": {
"DEBUG": "1"
},
"program": "", "program": "",
"linux": { "linux": {
"preLaunchTask": "(dbg) compile linux-x86_64", "preLaunchTask": "(dbg) compile linux-x86_64",

54
src/error_handling.c Normal file
View File

@@ -0,0 +1,54 @@
#include "error_handling.h"
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#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);
}

33
src/error_handling.h Normal file
View File

@@ -0,0 +1,33 @@
#pragma once
#include <stdint.h>
/* 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

View File

@@ -1,8 +1,17 @@
#include "main.h"
#include <stdint.h> #include <stdint.h>
#include <stdio.h>
#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) { int32_t main(int32_t argc, char** argv) {
(void)argc, (void)argv; (void)argc, (void)argv;
printf("%s\n", "Hello, World!"); debug("%s", "Hello, World!");
return 0; return status;
} }

5
src/main.h Normal file
View File

@@ -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)