diff options
author | c | 2024-01-28 22:58:01 -0500 |
---|---|---|
committer | c | 2024-01-28 22:58:01 -0500 |
commit | 0b84414bdde53b367bc28563a6936513c4d3b50c (patch) | |
tree | e7f650d47d0295049d3f5e8fa9164080dacaee45 | |
parent | ca20ff4f4d0ac63856e538f9f4cef97197bf6465 (diff) |
Macro Hell.
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | src/include/util.h | 61 | ||||
-rw-r--r-- | src/main.c | 1 | ||||
-rw-r--r-- | src/util.c | 7 | ||||
-rw-r--r-- | test/include/test.h | 16 |
5 files changed, 77 insertions, 12 deletions
@@ -32,10 +32,10 @@ halk: reg_options $(OBJS) $(CC) $(OBJS) $(REG_CFLAGS) -o $(BIN).out dbg: CFLAGS := $(DBG_CFLAGS) -dbg: dbg_options $(OBJS) +dbg: clean dbg_options $(OBJS) $(CC) $(OBJS) $(DBG_CFLAGS) -o $(BIN).out -test: clean dbg $(TEST_OUTS) +test: $(TEST_OUTS) set -e for f in $(TEST_OUTS); do ./$$f; done diff --git a/src/include/util.h b/src/include/util.h index 9f8a2b0..567916c 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -6,27 +6,80 @@ #include <string.h> #include <stdio.h> +/* Welcome to macro hell. */ + +#define HIDE(x) do {x} while (0) + #define MIN(a, b) (a < b ? a : b) #define MAX(a, b) (a > b ? a : b) -/* - TODO: Make these macros, to allow for better logging (printing __FILE__, - __func__, etc.) -*/ +/* Log some file info (mostly for use in other LOG_* macros.) */ +#define LOG_FINF HIDE( \ + fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__); \ +) + /* Log some debug information. */ void log_dbg(const char*, ...); +#define LOG_DBG(fmt, ...) HIDE( \ + fprintf(stderr, "\x1b[37m[\x1b[95;1m==\x1b[0m\x1b[37m]\x1b[0m\x1b[35m "); \ + LOG_FINF; \ + fprintf(stderr, fmt, ##__VA_ARGS__); \ + fprintf(stderr, "\x1b[0m\n"); \ +) + /* c: */ void log_yay(const char*, ...); +#define LOG_YAY(fmt, ...) HIDE( \ + fprintf(stderr, "\x1b[37m[\x1b[92;1m==\x1b[0m\x1b[37m]\x1b[32m "); \ + LOG_FINF; \ + fprintf(stderr, fmt, ##__VA_ARGS__); \ + fprintf(stderr, "\x1b[0m\n"); \ +) + /* Log some information. */ void log_inf(const char*, ...); +#define LOG_INF(fmt, ...) HIDE( \ + fprintf(stderr, "\x1b[37m[\x1b[94;1m==\x1b[0m\x1b[37m]\x1b[0m "); \ + fprintf(stderr, fmt, ##__VA_ARGS__); \ + fprintf(stderr, "\x1b[0m\n"); \ +) + /* Log something with no formatting. */ void log_raw(const char*, ...); +#define LOG_RAW(fmt, ...) HIDE( \ + fprintf(stderr, fmt, ##__VA_ARGS__); \ +) + /* Log a warning. */ void log_war(const char*, ...); +#define LOG_WAR(fmt, ...) HIDE( \ + fprintf(stderr, "\x1b[37m[\x1b[93;1m==\x1b[0m\x1b[37m]\x1b[93;1m WARNING:\x1b[0m\x1b[33m "); \ + LOG_FINF; \ + fprintf(stderr, fmt, ##__VA_ARGS__); \ + fprintf(stderr, "\x1b[0m\n"); \ +) + /* Log an error. */ void log_err(const char*, ...); +#define LOG_ERR(fmt, ...) HIDE( \ + fprintf(stderr, "\x1b[37m[\x1b[91;1m==\x1b[0m\x1b[37m]\x1b[91;1m ERROR:\x1b[0m\x1b[31m "); \ + LOG_FINF; \ + fprintf(stderr, fmt, ##__VA_ARGS__); \ + fprintf(stderr, "\x1b[0m\n"); \ +) + +/* Warn of soon™-to-be-removed functions. */ +#define WFDEPRECATED LOG_WAR("Use of this function is deprecated.") + /* Die and leave message. */ void die(const char*, ...); +#define DIE(fmt, ...) HIDE( \ + fprintf(stderr, "\x1b[37m[\x1b[91;1m==\x1b[0m\x1b[37m]\x1b[91;1m CAUSE OF DEATH:\x1b[0m\x1b[31m "); \ + fprintf(stderr, fmt, ##__VA_ARGS__); \ + fprintf(stderr, "\x1b[0m\n"); \ + fprintf(stderr, "\x1b[37m[\x1b[91;1m==\x1b[0m\x1b[37m]\x1b[91;1m Exiting...\x1b[0m\n"); \ + exit(1); \ +) /* If `calloc()` returns null, die. */ void* ecalloc(size_t, size_t); @@ -1,4 +1,5 @@ #include "include/main.h" +#include "include/util.h" /* FIXME: Segfaults ~30% of the time. No idea why. Thx future self <3. */ int main(int argc, char* argv[]) { @@ -1,6 +1,7 @@ #include "include/util.h" void log_dbg(const char* fmt, ...) { + WFDEPRECATED; va_list ap; fprintf(stdout, ""); @@ -14,6 +15,7 @@ void log_dbg(const char* fmt, ...) { } void log_yay(const char* fmt, ...) { + WFDEPRECATED; va_list ap; fprintf(stdout, "\x1b[37m[\x1b[92;1m==\x1b[0m\x1b[37m]\x1b[32m "); @@ -26,6 +28,7 @@ void log_yay(const char* fmt, ...) { } void log_inf(const char* fmt, ...) { + WFDEPRECATED; va_list ap; fprintf(stderr, "\x1b[37m[\x1b[94;1m==\x1b[0m\x1b[37m]\x1b[0m "); @@ -38,6 +41,7 @@ void log_inf(const char* fmt, ...) { } void log_raw(const char* fmt, ...) { + WFDEPRECATED; va_list ap; va_start(ap, fmt); @@ -46,6 +50,7 @@ void log_raw(const char* fmt, ...) { } void log_war(const char* fmt, ...) { + WFDEPRECATED; va_list ap; fprintf(stderr, "\x1b[37m[\x1b[93;1m==\x1b[0m\x1b[37m]\x1b[93;1m WARNING:\x1b[0m\x1b[33m "); @@ -58,6 +63,7 @@ void log_war(const char* fmt, ...) { } void log_err(const char* fmt, ...) { + WFDEPRECATED; va_list ap; fprintf(stderr, "\x1b[37m[\x1b[91;1m==\x1b[0m\x1b[37m]\x1b[91;1m ERROR:\x1b[0m\x1b[31m "); @@ -70,6 +76,7 @@ void log_err(const char* fmt, ...) { } void die(const char* fmt, ...) { + WFDEPRECATED; va_list ap; fprintf(stderr, "\x1b[37m[\x1b[91;1m==\x1b[0m\x1b[37m]\x1b[91;1m CAUSE OF DEATH:\x1b[0m\x1b[31m "); diff --git a/test/include/test.h b/test/include/test.h index 8cde34d..7366aa2 100644 --- a/test/include/test.h +++ b/test/include/test.h @@ -18,13 +18,17 @@ extern unsigned int TESTS_PASSED; #define ASSERT(EXPR) \ TESTS_RUN++; \ - (EXPR && ++TESTS_PASSED) ? \ - log_inf("%s:%s:%d: Assertion passed!", __FILE__, __func__, __LINE__) : \ - log_err("%s:%s:%d: Assertion failed:\n\t%s", __FILE__, __func__, __LINE__, #EXPR); + if (EXPR && ++TESTS_PASSED) { \ + LOG_INF("%s:%s:%d: Assertion passed!", __FILE__, __func__, __LINE__); \ + } else { \ + LOG_ERR("%s:%s:%d: Assertion failed:\n\t%s", __FILE__, __func__, __LINE__, #EXPR); \ + } #define TEST_REPORT \ - (TESTS_RUN == TESTS_PASSED) ? \ - log_yay("%s: All %d tests passed!", __FILE__, TESTS_RUN) : \ - log_err("%d/%d tests failed.", TESTS_RUN - TESTS_PASSED, TESTS_RUN); + if (TESTS_RUN == TESTS_PASSED) { \ + LOG_YAY("%s: All %d tests passed!", __FILE__, TESTS_RUN); \ + } else { \ + LOG_ERR("%d/%d tests failed.", TESTS_RUN - TESTS_PASSED, TESTS_RUN); \ + } #endif |