mirror of
https://github.com/thepigeongenerator/tetris_clone.git
synced 2025-12-17 14:05:45 +01:00
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:
12
src/error.c
12
src/error.c
@@ -16,7 +16,7 @@
|
|||||||
(void)vsnprintf(buf, PRINT_BUFFER_SIZE, fmt, args); \
|
(void)vsnprintf(buf, PRINT_BUFFER_SIZE, fmt, args); \
|
||||||
va_end(args);
|
va_end(args);
|
||||||
|
|
||||||
void debug(char const* fmt, ...) {
|
void debug(char const* restrict fmt, ...) {
|
||||||
char const* env = getenv("DEBUG");
|
char const* env = getenv("DEBUG");
|
||||||
if (env == NULL || *env != '1')
|
if (env == NULL || *env != '1')
|
||||||
return;
|
return;
|
||||||
@@ -27,25 +27,25 @@ void debug(char const* fmt, ...) {
|
|||||||
(void)fprintf(stdout, "\033[95m%s\033[0m\n", buf);
|
(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};
|
char buf[PRINT_BUFFER_SIZE] = {0};
|
||||||
write_args(buf, fmt);
|
write_args(buf, fmt);
|
||||||
(void)fprintf(stdout, "\033[0m%s\033[0m\n", buf); // write colour here for consistency
|
(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};
|
char buf[PRINT_BUFFER_SIZE] = {0};
|
||||||
write_args(buf, fmt);
|
write_args(buf, fmt);
|
||||||
(void)fprintf(stderr, "\033[93mW: %s\033[0m\n", buf);
|
(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};
|
char buf[PRINT_BUFFER_SIZE] = {0};
|
||||||
write_args(buf, fmt);
|
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};
|
char buf1[PRINT_BUFFER_SIZE] = {0};
|
||||||
write_args(buf1, fmt);
|
write_args(buf1, fmt);
|
||||||
|
|
||||||
|
|||||||
11
src/error.h
11
src/error.h
@@ -28,12 +28,11 @@ enum gamestatus {
|
|||||||
STATUS_RUNNING = -1,
|
STATUS_RUNNING = -1,
|
||||||
// clang-format on
|
// 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__((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__((format(printf, 1, 2))) void info(char const*, ...); // prints an info message to stdout
|
__attribute__((nonnull(1))) __attribute__((format(printf, 1, 2))) void info(char const* restrict, ...); // prints an info message to stdout
|
||||||
__attribute__((format(printf, 1, 2))) void warn(char const*, ...); // prints a warning message to stderr
|
__attribute__((nonnull(1))) __attribute__((format(printf, 1, 2))) void warn(char const* restrict, ...); // 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 error(char const* restrcit, ...); // prints an warning message to stderr
|
||||||
|
|
||||||
// prints an error message to stderr before exiting
|
// 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, ...);
|
||||||
|
|||||||
Reference in New Issue
Block a user