rework error.h

added nonnull attributes, removed gamestatus and other integers which
don't need an exact type, added restrict to all the pointers.
This commit is contained in:
2025-04-16 16:44:01 +02:00
parent b2e2089a59
commit 4bba7bda9f
2 changed files with 11 additions and 12 deletions

View File

@@ -16,7 +16,7 @@
(void)vsnprintf(buf, PRINT_BUFFER_SIZE, fmt, args); \
va_end(args);
void debug(char const* fmt, ...) {
void debug(char const* restrict fmt, ...) {
char const* env = getenv("DEBUG");
if (env == NULL || *env != '1')
return;
@@ -27,25 +27,25 @@ void debug(char const* fmt, ...) {
(void)fprintf(stdout, "\033[95m%s\033[0m\n", buf);
}
void info(char const* fmt, ...) {
void info(char const* restrict 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, ...) {
void warn(char const* restrict fmt, ...) {
char buf[PRINT_BUFFER_SIZE] = {0};
write_args(buf, fmt);
(void)fprintf(stderr, "\033[93mW: %s\033[0m\n", buf);
}
void error(char const* fmt, ...) {
void error(char const* restrict fmt, ...) {
char buf[PRINT_BUFFER_SIZE] = {0};
write_args(buf, fmt);
(void)fprintf(stderr, "\033[91mE: %s\033[0m", buf);
(void)fprintf(stderr, "\033[91mE: %s\033[0m\n", buf);
}
noreturn void fatal(gamestatus error_code, char const* fname, uint32_t ln, char const* fmt, ...) {
noreturn void fatal(unsigned error_code, char const* restrict fname, unsigned ln, char const* restrict fmt, ...) {
char buf1[PRINT_BUFFER_SIZE] = {0};
write_args(buf1, fmt);

View File

@@ -28,12 +28,11 @@ enum gamestatus {
STATUS_RUNNING = -1,
// clang-format on
};
typedef int8_t gamestatus;
__attribute__((format(printf, 1, 2))) void debug(char const*, ...); // prints a debug message to stdout if the DEBUG environment variable is set, otherwise the call is ignored.
__attribute__((format(printf, 1, 2))) void info(char const*, ...); // prints an info message to stdout
__attribute__((format(printf, 1, 2))) void warn(char const*, ...); // prints a warning message to stderr
__attribute__((format(printf, 1, 2))) void error(char const*, ...); // prints an warning message to stderr
__attribute__((nonnull(1))) __attribute__((format(printf, 1, 2))) void debug(char const* restrict, ...); // prints a debug message to stdout if the DEBUG environment variable is set, otherwise the call is ignored.
__attribute__((nonnull(1))) __attribute__((format(printf, 1, 2))) void info(char const* restrict, ...); // prints an info message to stdout
__attribute__((nonnull(1))) __attribute__((format(printf, 1, 2))) void warn(char const* restrict, ...); // prints a warning message to stderr
__attribute__((nonnull(1))) __attribute__((format(printf, 1, 2))) void error(char const* restrcit, ...); // prints an warning message to stderr
// prints an error message to stderr before exiting
__attribute__((format(printf, 4, 5))) noreturn void fatal(gamestatus, char const* file_name, uint32_t line, char const* fmt, ...);
__attribute__((nonnull(2, 5))) __attribute__((format(printf, 4, 5))) noreturn void fatal(unsigned, char const* restrict file_name, unsigned line, char const* restrict fmt, ...);