added more parameters to default logger
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 24 Dec 2022 04:50:09 +0000 (22:50 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 24 Dec 2022 04:50:09 +0000 (22:50 -0600)
compiler/src/checker.c
core/builtin.onyx
core/os/file.onyx
core/runtime/common.onyx
docs/plan

index 12171d0080fdfef451e37abd6db37397aeb61bed..61685cb77276943a49d2299c4b100219d1719f66 100644 (file)
@@ -1649,7 +1649,7 @@ CheckStatus check_address_of(AstAddressOf** paof) {
 
     aof->type = type_make_pointer(context.ast_alloc, expr->type);
 
-    if (expr->kind == Ast_Kind_Memres) {
+    if (expr->kind == Ast_Kind_Memres && !((AstMemRes *) expr)->threadlocal) {
         aof->flags |= Ast_Flag_Comptime;
     }
 
index b6afe460172e884d3af0614ed062376cf9022a6f..509867a807dbf24ba4a8c3ead05a5fb84a5204b6 100644 (file)
@@ -54,7 +54,7 @@ OnyxContext :: struct {
     allocator      : Allocator;
     temp_allocator : Allocator;
 
-    logger         : Logger = .{ default_logger, null };
+    logger         : Logger = .{ default_logger_proc, ^default_logger };
 
     assert_handler : (msg: str, site: CallSite) -> void;
 
@@ -65,18 +65,6 @@ OnyxContext :: struct {
 }
 
 
-#if runtime.runtime != .Custom {
-    #local default_logger :: (data: rawptr, msg: str) {
-        use package core
-        println(msg);
-    }
-
-} else {
-    #local default_logger :: (data: rawptr, msg: str) {
-        // In a custom runtime, there is no way to know how to log something.
-    }
-}
-
 #thread_local context : OnyxContext;
 
 assert :: (cond: bool, msg: str, site := #callsite) {
@@ -101,16 +89,65 @@ assert :: (cond: bool, msg: str, site := #callsite) {
 // Basic logging
 //
 
+Log_Level :: enum {
+    Debug;
+    Info;
+    Warning;
+    Error;
+    Critical;
+}
+
 Logger :: struct {
-    func : (data: rawptr, msg: str) -> void;
+    func : (data: rawptr, level: Log_Level, msg: str, module: str) -> void;
     data : rawptr;
 }
 
-log :: (msg: str, use logger: Logger = context.logger) {
-    func(data, msg);
+log :: #match #local {}
+
+#overload
+log :: (level: Log_Level, msg: str) {
+    context.logger.func(context.logger.data, level, msg, "");
+}
+
+#overload
+log :: (level: Log_Level, module, msg: str) {
+    context.logger.func(context.logger.data, level, msg, module);
 }
 
 
+
+//
+// A sensible default logger.
+//
+Default_Logger :: struct {
+    minimum_level: Log_Level;
+}
+
+#local #thread_local default_logger: Default_Logger;
+
+default_log_level :: (level: Log_Level) {
+    default_logger.minimum_level = level;
+}
+
+#if runtime.runtime != .Custom {
+    #local default_logger_proc :: (logger: ^Default_Logger, level: Log_Level, msg: str, module: str) {
+        if level < logger.minimum_level do return;
+
+        if module {
+            core.printf("[{}][{}] {}\n", level, module, msg);
+        } else {
+            core.printf("[{}] {}\n", level, msg);
+        }
+    }
+
+} else {
+    #local default_logger_proc :: (data: rawptr, level: Log_Level, msg: str, module: str) {
+        // In a custom runtime, there is no way to know how to log something.
+    }
+}
+
+
+
 //
 // Basic allocation structures.
 // The implementations of all of the allocators can be found in core/alloc/.
index 9e71615cb65caf6664239de632a7c0ac8a3e3ddf..0d61e5508467a0e3e7d65378bb66157d94187626 100644 (file)
@@ -154,7 +154,7 @@ file_logger_close :: (logger := context.logger) {
 }
 
 #local
-file_logger_proc :: (data: ^File, msg: str) {
+file_logger_proc :: (data: ^File, level: Log_Level, msg: str, module: str) {
     writer := io.writer_make(data, 0);
     io.write(^writer, msg);
     io.write(^writer, "\n");
index 07730cafc1a880b29383f9e47b809b690db60732..6ffc349799d5080a85a4cda68773529ea9a1ba30 100644 (file)
@@ -56,6 +56,12 @@ __thread_initialize :: macro () {
     context.temp_allocator = alloc.temp_allocator;
     context.assert_handler = __assert_handler;
 
+    //
+    // The default log level is Info. This seems reasonable, but it does
+    // mean that all Debug messages will be omitted unless it is changed
+    // manually.
+    default_log_level(.Info);
+
     __stdio_init();
 }
 
index 86b84216df95b9420ac32f200d18752be042f3d0..1442e06f09c01c5d5d979c1e9c6bfe9e738a81cb 100644 (file)
--- a/docs/plan
+++ b/docs/plan
@@ -57,7 +57,7 @@ HOW:
 
         [ ] transmute
 
-        [ ] look into creating a source map
+        [X] look into creating a source map
             - first-look looks really gross
             - whoever came up with the source map spec should be fired... why are people so afraid of binary files??
             - DWARF looks like it might be easier, but it still doesn't look fun.
@@ -82,7 +82,7 @@ HOW:
             - Checking which things are allowed to cast to/from should be checked in the checker,
                 not in the wasm generatation
 
-        [ ] Interop with C
+        [X] Interop with C
             - Difficult feature
             - Would be nice to use existing C code (such as stb headers)
             - Some way to make this happen without needing a full C compiler would be nice