From 2cfae0fce46095a57df556beb9fdf24da27f4b52 Mon Sep 17 00:00:00 2001 From: Quinn Date: Fri, 5 Sep 2025 15:17:53 +0200 Subject: [PATCH] rework error.h, to decrease impact on binary size. We were statically creating a new string per log message, so re-using the same message would cause duplicate byte sizes. Now, all prefixes / suffixes are only defined once, the file name is defined once per file, the line number should be defined once per unique line. This just saves some memory in the final binary, allowing things to be optimised a little better. --- src/error.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/error.h | 23 ++++++++++++----------- 2 files changed, 63 insertions(+), 11 deletions(-) create mode 100644 src/error.c diff --git a/src/error.c b/src/error.c new file mode 100644 index 0000000..2a38774 --- /dev/null +++ b/src/error.c @@ -0,0 +1,51 @@ +#include "error.h" + +#include +#include + +#include "util/intdef.h" + +static void error_log(FILE *restrict stream, const char *restrict pfx, uint ln, const char *restrict file, const char *restrict fmt, va_list ap) { + fprintf(stream, "(%s:%u) [%s] '", file, ln, pfx); + + vfprintf(stream, fmt, ap); + + fprintf(stream, "'\n"); +} + +void error_dbg(uint ln, const char *restrict file, const char *restrict fmt, ...) { + va_list ap; + va_start(ap, fmt); + error_log(stdout, "\033[95mDBG\033[0m", ln, file, fmt, ap); + va_end(ap); +} + +void error_inf(uint ln, const char *restrict file, const char *restrict fmt, ...) { + va_list ap; + va_start(ap, fmt); + error_log(stdout, "\033[93mINF\033[0m", ln, file, fmt, ap); + va_end(ap); +} + +void error_war(uint ln, const char *restrict file, const char *restrict fmt, ...) { + va_list ap; + va_start(ap, fmt); + error_log(stdout, "\033[91mWAR\033[0m", ln, file, fmt, ap); + va_end(ap); +} + +void error_err(uint ln, const char *restrict file, const char *restrict fmt, ...) { + va_list ap; + va_start(ap, fmt); + error_log(stdout, "\033[mFAT\033[0m", ln, file, fmt, ap); + va_end(ap); +} + +void error_fat(uint ln, const char *restrict file, const char *restrict fmt, ...) { + va_list ap; + va_start(ap, fmt); + error_log(stdout, "\033[mFAT\033[0m", ln, file, fmt, ap); + va_end(ap); + + exit(EXIT_FAILURE); +} diff --git a/src/error.h b/src/error.h index e90d455..92dd94b 100644 --- a/src/error.h +++ b/src/error.h @@ -2,20 +2,21 @@ * Licensed under the MIT Licence. See LICENSE for details */ #pragma once -#if __INCLUDE_LEVEL__ > 0 #include #include +#include "util/atrb.h" +#include "util/intdef.h" #include "util/macro.h" -#endif -#define debug(s, ...) printf("\033[95m" __FILE__ ":" MACRO_STR2(__LINE__) ": [DBG]: " s "\033[0m\n", ##__VA_ARGS__) -#define info(s, ...) printf(__FILE__ ":" MACRO_STR2(__LINE__) ": [INF]: " s "\n", ##__VA_ARGS__) -#define warn(s, ...) fprintf(stderr, "\033[93m" __FILE__ ":" MACRO_STR2(__LINE__) ": [WAR]: " s "\033[0m\n", ##__VA_ARGS__) -#define error(s, ...) fprintf(stderr, "\033[91m" __FILE__ ":" MACRO_STR2(__LINE__) ": [ERR]: " s "\033[0m\n", ##__VA_ARGS__) +void error_dbg(uint ln, const char *restrict file, const char *restrict fmt, ...); +void error_inf(uint ln, const char *restrict file, const char *restrict fmt, ...); +void error_war(uint ln, const char *restrict file, const char *restrict fmt, ...); +void error_err(uint ln, const char *restrict file, const char *restrict fmt, ...); +void error_fat(uint ln, const char *restrict file, const char *restrict fmt, ...) NORET; -#define fatal(s, ...) \ - do { \ - fprintf(stderr, "\033[101m" __FILE__ ":" MACRO_STR2(__LINE__) ": [FAT]: " s "\033[0m\n", ##__VA_ARGS__); \ - exit(EXIT_FAILURE); \ - } while (0) +#define debug(s, ...) error_dbg(__LINE__, __FILE__, s, ##__VA_ARGS__) +#define info(s, ...) error_inf(__LINE__, __FILE__, s, ##__VA_ARGS__) +#define warn(s, ...) error_war(__LINE__, __FILE__, s, ##__VA_ARGS__) +#define error(s, ...) error_err(__LINE__, __FILE__, s, ##__VA_ARGS__) +#define fatal(s, ...) error_fat(__LINE__, __FILE__, s, ##__VA_ARGS__)