summaryrefslogtreecommitdiff
path: root/src/core/logging.c
diff options
context:
space:
mode:
author0scar <qgt268@alumni.ku.dk>2024-02-05 16:36:47 +0000
committer0scar <qgt268@alumni.ku.dk>2024-02-05 16:48:20 +0000
commite85f81ec109782a01f1f741d4c2ed5f21af0c124 (patch)
tree8708c742b296de20e2d54ef9fa8ea7c2036d3d82 /src/core/logging.c
parent457a4a3b1f00bf4d6c31b693085aa7150d2dcded (diff)
Organize the sourcefiles
Diffstat (limited to 'src/core/logging.c')
-rw-r--r--src/core/logging.c82
1 files changed, 82 insertions, 0 deletions
diff --git a/src/core/logging.c b/src/core/logging.c
new file mode 100644
index 0000000..7870258
--- /dev/null
+++ b/src/core/logging.c
@@ -0,0 +1,82 @@
+#include <engine/logging.h>
+#include <engine/types.h>
+#include <stdlib.h>
+
+char* itoa(i32 x) {
+ const i32 size = (((i32)ceil(log10((f64)x))) + 1) * sizeof(char);
+ char* retval = (char*)malloc(size);
+ if (retval == NULL) {
+ perror("Failed to allocate memory for itoa");
+ exit(EXIT_FAILURE);
+ }
+ sprintf(retval, "%d", x);
+ return retval;
+}
+
+void _log(FILE* stream, const char* prefix, const char* fmt, va_list ap) {
+ if (stream == NULL) {
+ fprintf(stderr, "_log got NULL in stream argument\n");
+ exit(EXIT_FAILURE);
+ }
+ fputs(prefix, stream);
+ vfprintf(stream, fmt, ap);
+}
+
+void LOG(const char* fmt, ...) {
+ va_list ap;
+ va_start(ap, fmt);
+ _log(stdout, "[" TERM_COLOR_BLUE "LOG" TERM_COLOR_RESET "] ", fmt, ap);
+ va_end(ap);
+ puts("");
+}
+
+void INFO_(const char* fmt, ...) {
+ va_list ap;
+ va_start(ap, fmt);
+ _log(stdout, "[" TERM_COLOR_GREEN "INFO" TERM_COLOR_RESET "] ", fmt, ap);
+ va_end(ap);
+}
+
+void INFO(const char* fmt, ...) {
+ va_list ap;
+ va_start(ap, fmt);
+ _log(stdout, "[" TERM_COLOR_GREEN "INFO" TERM_COLOR_RESET "] ", fmt, ap);
+ va_end(ap);
+ puts("");
+}
+
+void __DEBUG(const char* file, const i32 line, const char* func,
+ const char* fmt, ...) {
+ va_list ap;
+
+ const usize prefix_len = 1024;
+
+ char* prefix = malloc(sizeof(char) * 1024);
+
+ snprintf(prefix, prefix_len,
+ "[" TERM_COLOR_YELLOW "DEBUG" TERM_COLOR_RESET "] "
+ "%s:%d <%s> ",
+ file, line, func);
+
+ va_start(ap, fmt);
+ _log(stdout, prefix, fmt, ap);
+ va_end(ap);
+
+ free(prefix);
+}
+
+void WARN(const char* fmt, ...) {
+ va_list ap;
+ va_start(ap, fmt);
+ _log(stderr, "[" TERM_COLOR_PURPLE "WARN" TERM_COLOR_RESET "] ", fmt, ap);
+ va_end(ap);
+ puts("");
+}
+
+void ERROR(const char* fmt, ...) {
+ va_list ap;
+ va_start(ap, fmt);
+ _log(stderr, "[" TERM_COLOR_RED "ERROR" TERM_COLOR_RESET "] ", fmt, ap);
+ va_end(ap);
+ puts("");
+}