aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2024-06-14 18:41:21 +0200
committerBad Diode <bd@badd10de.dev>2024-06-14 18:41:21 +0200
commit2cad3b5b21fe1644239b03fc8fd3c4329d98e606 (patch)
tree152cb5c64a89539fcfff86a3a26770964c7d4830 /src
parent3aae2f36047e9adc6a59b886492254eb4370777d (diff)
downloadbdl-2cad3b5b21fe1644239b03fc8fd3c4329d98e606.tar.gz
bdl-2cad3b5b21fe1644239b03fc8fd3c4329d98e606.zip
Add initial custom logging functions
Diffstat (limited to 'src')
-rw-r--r--src/badlib.h48
-rw-r--r--src/main.c42
2 files changed, 84 insertions, 6 deletions
diff --git a/src/badlib.h b/src/badlib.h
index f7a7974..2bc1807 100644
--- a/src/badlib.h
+++ b/src/badlib.h
@@ -873,4 +873,52 @@ platform_time(void) {
873 return ts.tv_sec * 1000000000 + ts.tv_nsec; 873 return ts.tv_sec * 1000000000 + ts.tv_nsec;
874} 874}
875 875
876//
877// Custom logger.
878//
879
880#define LOG_BUF_SIZE KB(4)
881
882typedef struct Logger {
883 Buf buf;
884 FILE *dest;
885 Arena *storage;
886} Logger;
887
888void
889log_init(Logger *l) {
890 buf_reserve(&l->buf, LOG_BUF_SIZE, l->storage);
891}
892
893void
894log_flush(Logger *l) {
895 if (l->buf.size) {
896 fprintf(l->dest, "%.*s", (int)l->buf.size, l->buf.mem);
897 }
898}
899
900void
901log_str(Logger *l, Str str) {
902 assert(l);
903 Buf *buf = &l->buf;
904 if (buf->size == 0) log_init(l);
905 while (str.size > 0) {
906 sz avail = buf->cap - buf->size;
907 if (avail == 0) {
908 log_flush(l);
909 avail = buf->cap - buf->size;
910 assert(avail > 0);
911 }
912 if (str.size < avail) {
913 buf_insert(&l->buf, str.mem, str.size, l->storage);
914 str.size = 0;
915 return;
916 }
917 buf_insert(&l->buf, str.mem, avail, l->storage);
918 str.mem += avail;
919 str.size -= avail;
920 }
921}
922
923
876#endif // BADLIB_H 924#endif // BADLIB_H
diff --git a/src/main.c b/src/main.c
index db833e4..e65c9e7 100644
--- a/src/main.c
+++ b/src/main.c
@@ -15,6 +15,7 @@ typedef enum ExecMode {
15static ExecMode mode = RUN_NORMAL; 15static ExecMode mode = RUN_NORMAL;
16 16
17#define LEXER_MEM GB(2) 17#define LEXER_MEM GB(2)
18#define LOGGER_MEM MB(1)
18 19
19void 20void
20process_file(Str path) { 21process_file(Str path) {
@@ -31,13 +32,42 @@ process_file(Str path) {
31 printf("reading file...\n"); 32 printf("reading file...\n");
32 FileContents file = platform_read_file(path, &lexer_arena); 33 FileContents file = platform_read_file(path, &lexer_arena);
33 34
34 // NOTE: Testing file read line by line.
35 Str scanner = file.data; 35 Str scanner = file.data;
36 for (sz i = 0; scanner.size != 0; i++) { 36 Str line = str_split(&scanner, cstr("\n"));
37 Str line = str_split(&scanner, cstr("\n")); 37 printf("%04d ", 1);
38 printf("%04ld ", i); 38 printstrln(&line);
39 printstrln(&line); 39 // NOTE: Testing file read line by line.
40 } 40 // for (sz i = 0; scanner.size != 0; i++) {
41 // Str line = str_split(&scanner, cstr("\n"));
42 // printf("%04ld ", i + 1);
43 // printstrln(&line);
44 // }
45
46 // Testing custom logger functions.
47 Arena logger_arena = arena_create(LOGGER_MEM, os_allocator);
48 Logger logger_info = {
49 .dest = stdout,
50 .storage = &logger_arena,
51 };
52 log_str(&logger_info, cstr("<<< "));
53 log_str(&logger_info, cstr("processing file: "));
54 log_str(&logger_info, path);
55 log_str(&logger_info, cstr(" >>>"));
56 log_str(&logger_info, cstr("\n"));
57
58 Logger logger_err = {
59 .dest = stderr,
60 .storage = &logger_arena,
61 };
62 log_str(&logger_err, path);
63 log_str(&logger_err, cstr(":1:1:"));
64 log_str(&logger_err, cstr("error: testing string logger\n"));
65 log_flush(&logger_info);
66 log_flush(&logger_err);
67
68 // TODO: add numeric logging
69 // TODO: add custom struct logging
70 // TODO: add a vaargs function with a little dsl
41 71
42 // TODO: run lexer. 72 // TODO: run lexer.
43} 73}