aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjustanothercatgirl <sotov@twistea.su>2025-02-02 20:28:12 +0300
committerjustanothercatgirl <sotov@twistea.su>2025-02-02 20:33:05 +0300
commite14c6bc3801be29eb5a64c813754b170a67d4956 (patch)
treed50139560fa85aa431ac934c03f19905327141e7
parent8989b7dc005af7960aa66fd99b197a3b0b7a4fbc (diff)
Added log header
-rw-r--r--Makefile14
-rw-r--r--include/log.h84
-rw-r--r--tests/log.c22
3 files changed, 116 insertions, 4 deletions
diff --git a/Makefile b/Makefile
index 21f4a17..c24e590 100644
--- a/Makefile
+++ b/Makefile
@@ -1,8 +1,8 @@
CC := gcc
-CFLAGS := -g -Wall -Wextra -Wpedantic -Werror -Wno-language-extension-token
+CFLAGS := -g -Wall -Wextra -Wpedantic -Werror -Wno-language-extension-token -Wno-stringop-overread -Iinclude
-tests: container types
+tests: container types log
container: tests/dynarray.c tests/dynarray_struct.c tests/obscure.c \
tests/binary_search.c tests/linked_list.c tests/hashset.c \
@@ -30,8 +30,14 @@ container: tests/dynarray.c tests/dynarray_struct.c tests/obscure.c \
rm $@
-types:
- $(CC) -o $@ tests/types.c
+types: tests/types.c
+ $(CC) -o $@ tests/types.c $(CFLAGS)
./$@
rm $@
+
+log: tests/log.c
+ $(CC) -o $@ tests/log.c $(CFLAGS)
+ - ./$@
+ rm $@
+
diff --git a/include/log.h b/include/log.h
new file mode 100644
index 0000000..59d2ed3
--- /dev/null
+++ b/include/log.h
@@ -0,0 +1,84 @@
+#pragma once
+
+#include <stdio.h>
+#include <stdlib.h>
+
+// ANSII colors
+#define BLK "\x1b[30m"
+#define RED "\x1b[31m"
+#define GRN "\x1b[32m"
+#define YLW "\x1b[33m"
+#define BLU "\x1b[34m"
+#define VIO "\x1b[35m"
+#define CYN "\x1b[36m"
+#define WHT "\x1b[37m"
+// ANSII "Bright" colors
+#define BBLK "\x1b[90m"
+#define BRED "\x1b[91m"
+#define BGRN "\x1b[92m"
+#define BYLW "\x1b[93m"
+#define BBLU "\x1b[94m"
+#define BVIO "\x1b[95m"
+#define BCYN "\x1b[96m"
+#define BWHT "\x1b[97m"
+// Reset sequence
+#define NL "\n"
+#define RST "\x1b[0m"
+#define NLRST "\n\x1b[0m"
+
+// Naming convention:
+// L<log-level>[ |V|F|VF|P|PV]
+// L stands for Log
+// log-level is: DEBUG, INFO, WARN, ERR, CRIT
+// V stands for Verbose: include information about location in source
+// F stands for format: needed to have printf-style formatting
+// P stands for perror
+
+#ifdef LOG_DEBUG
+// Log debug
+# define LDEBUG(msg) fputs("[DEBUG] " msg NL, stderr)
+# define LDEBUGV(msg) fprintf(stderr, "[DEBUG %s:%i@%s] " msg NL, __FILE__, __LINE__, __func__)
+# define LDEBUGF(fmt, ...) fprintf(stderr, "[DEBUG] " fmt NL, __VA_ARGS__)
+# define LDEBUGVF(fmt, ...) fprintf(stderr, "[DEBUG %s:%i@%s] " fmt NL, __FILE__, __LINE__, __func__, __VA_ARGS__)
+#else
+# define LDEBUG(msg)
+# define LDEBUGV(msg)
+# define LDEBUGF(fmt, ...)
+# define LDEBUGVF(fmt, ...)
+#endif // LOG_DEBUG
+
+// Log info
+#define LINFO(msg) fputs(WHT "[INFO] " msg NLRST, stderr)
+#define LINFOV(msg) fprintf(stderr, WHT "[INFO %s:%i@%s] " msg NLRST, __FILE__, __LINE__, __func__)
+#define LINFOF(fmt, ...) fprintf(stderr, WHT "[INFO] " fmt NLRST , __VA_ARGS__)
+#define LINFOVF(fmt, ...) fprintf(stderr, WHT "[INFO %s:%i@%s] " fmt NLRST, __FILE__, __LINE__, __func__, __VA_ARGS__)
+
+// Log warning
+#define LWARN(msg) fputs(YLW "[WARNING] " msg NLRST, stderr)
+#define LWARNV(msg) fprintf(stderr, YLW "[WARNING %s:%i@%s] " msg NLRST, __FILE__, __LINE__, __func__)
+#define LWARNF(fmt, ...) fprintf(stderr, YLW "[WARNING] " fmt NLRST, __VA_ARGS__)
+#define LWARNVF(fmt, ...) fprintf(stderr, YLW "[WARNING %s:%i@%s] " fmt NLRST, __FILE__, __LINE__, __func__, __VA_ARGS__)
+#define LWARNP(msg) do { fputs(YLW "[WARNING]", stderr); perror(msg); fputs(RST, stderr); } while (0)
+#define LWARNPV(msg) do { fprintf(stderr, YLW "[WARNING %s:%i@%s] ", __FILE__, __LINE__, __func__); perror(msg); fputs(RST, stderr); } while(0)
+
+// Log error
+#define LERR(msg) fputs(RED "[ERROR] " msg NLRST, stderr)
+#define LERRV(msg) fprintf(stderr, RED "[ERROR %s:%i@%s] " msg NLRST, __FILE__, __LINE__, __func__)
+#define LERRF(fmt, ...) fprintf(stderr, RED "[ERROR] " fmt NLRST, __VA_ARGS__)
+#define LERRVF(fmt, ...) fprintf(stderr, RED "[ERROR %s:%i@%s] " fmt NLRST, __FILE__, __LINE__, __func__, __VA_ARGS__)
+#define LERRP(msg) do { fputs(RED "[ERROR] ", stderr); perror(msg); fputs(RST, stderr); } while (0)
+#define LERRPV(msg) do { fprintf(stderr, RED "[ERROR %s:%i@%s] ", __FILE__, __LINE__, __func__); perror(msg); fputs(RST, stderr); } while(0)
+
+// Log critical: log and abort with exit status `ex`
+#define LCRIT(ex, msg) do { fputs(RED "[CRITICAL] " msg NLRST, stderr); exit(ex); } while (0)
+#define LCRITV(ex, msg) do { fprintf(stderr, RED "[CRITICAL %s:%i@%s] " msg NLRST, __FILE__, __LINE__, __func__); exit(ex); } while (0)
+#define LCRITF(ex, fmt, ...) do { fprintf(stderr, RED "[CRITICAL] " fmt NLRST, __VA_ARGS__); exit(ex); } while (0)
+#define LCRITVF(ex, fmt, ...) do { fprintf(stderr, RED "[CRITICAL %s:%i@%s] " fmt NLRST, __FILE__, __LINE__, __func__, __VA_ARGS__); exit(ex); } while (0)
+#define LCRITP(ex, msg) do { fputs(RED "[CRITICAL] ", stderr); perror(msg); fputs(NLRST, stderr); exit(ex); } while (0)
+#define LCRITPV(ex, msg) do { fprintf(stderr, RED "[CRITICAL %s:%i@%s] ", __FILE__, __LINE__, __func__); perror(msg); fputs(NLRST, stderr); exit(ex); } while(0)
+
+// exit status already specified to EXIT_FAILURE
+#define LFAIL(msg) do { fputs(RED "[CRITICAL] ", stderr); perror(msg); fputs(NLRST, stderr); exit(EXIT_FAILURE); } while (0)
+#define LFAILV(msg) do { fprintf(stderr, RED "[CRITICAL %s:%i@%s] ", __FILE__, __LINE__, __func__); perror(msg); fputs(NLRST, stderr); exit(EXIT_FAILURE); } while(0)
+
+
diff --git a/tests/log.c b/tests/log.c
new file mode 100644
index 0000000..e4d1c1e
--- /dev/null
+++ b/tests/log.c
@@ -0,0 +1,22 @@
+#include <log.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+void urmom() {
+ LERRV("This is error!");
+ LERRV("How could this be!");
+ LERRV("Impossible...");
+}
+
+int main() {
+ LDEBUGV("1st debug message");
+ LDEBUGF("Formatted message: %i, %.2f", 69, 420.69696969);
+ LINFOVF("literally checking %s", "colors");
+ open("/dev/asdjasfhkd", O_RDWR);
+ LWARNPV("open");
+ write(696969696, "", 1203192391823);
+ LERRP("write");
+ urmom();
+ LCRIT(1, "This is considered critical");
+}