add better error handling to include line and file in error messages

This commit is contained in:
2025-03-04 14:29:58 +01:00
parent 3164fe1661
commit c0bdc6ca17
2 changed files with 19 additions and 11 deletions

View File

@@ -49,11 +49,15 @@ void warn(char const* fmt, ...) {
(void)fprintf(stderr, "\033[93mW: %s\033[0m\n", buf); (void)fprintf(stderr, "\033[93mW: %s\033[0m\n", buf);
} }
noreturn void error(gamestatus error_code, char const* fmt, ...) { noreturn void i_error(gamestatus error_code, uint32_t ln, char const* fname, char const* fmt, ...) {
char buf[PRINT_BUFFER_SIZE] = {0}; char buf1[PRINT_BUFFER_SIZE] = {0};
write_args(buf, fmt); write_args(buf1, fmt);
(void)fprintf(stderr, "\033[91mE: %s\033[0m\n", buf);
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "something went wrong! :O", buf, NULL); char buf2[PRINT_BUFFER_SIZE * 2] = {0};
sprintf(buf2, "%s\n at %s:%u", buf1, fname, ln);
(void)fprintf(stderr, "\033[91mE: %s\033[0m\n", buf2);
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "something went wrong! :O", buf2, NULL);
// set status, but exit immediately, as code is not allowed to execute beyond this point // set status, but exit immediately, as code is not allowed to execute beyond this point
set_gamestatus(error_code); set_gamestatus(error_code);

View File

@@ -1,6 +1,7 @@
#pragma once #pragma once
#include <stdint.h> #include <stdint.h>
#include <stdnoreturn.h>
/* defines statuses in the 0..127, any higher/negative values are POSIX-reserved. /* 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 */ * The max value (or -1) shall mean the application is running, anything else shall mean an exit code of some kind */
@@ -27,9 +28,12 @@ enum {
}; };
typedef int8_t gamestatus; typedef int8_t gamestatus;
void set_gamestatus(gamestatus); // sets the current status of the game void set_gamestatus(gamestatus); // sets the current status of the game
gamestatus get_gamestatus(void); // gets the current status of the game gamestatus get_gamestatus(void); // gets the current status of the game
void debug(char const*, ...); // prints a debug message to stdout if the DEBUG environment variable is set, otherwise the call is ignored. 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 info(char const*, ...); // prints an info message to stdout
void warn(char const*, ...); // prints a warning message to stderr void warn(char const*, ...); // prints a warning message to stderr
void error(gamestatus, char const*, ...); // prints an error message to stderr before exiting
// prints an error message to stderr before exiting
#define error(status, fmt, ...) i_error(status, __LINE__, __FILE__, fmt, __VA_ARGS__)
noreturn void i_error(gamestatus, uint32_t, char const*, char const*, ...);