From fb5024d095c17deae5b56bce43e50b89386432ac Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Fri, 23 Dec 2022 22:50:09 -0600 Subject: [PATCH] added more parameters to default logger --- compiler/src/checker.c | 2 +- core/builtin.onyx | 69 ++++++++++++++++++++++++++++++---------- core/os/file.onyx | 2 +- core/runtime/common.onyx | 6 ++++ docs/plan | 4 +-- 5 files changed, 63 insertions(+), 20 deletions(-) diff --git a/compiler/src/checker.c b/compiler/src/checker.c index 12171d00..61685cb7 100644 --- a/compiler/src/checker.c +++ b/compiler/src/checker.c @@ -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; } diff --git a/core/builtin.onyx b/core/builtin.onyx index b6afe460..509867a8 100644 --- a/core/builtin.onyx +++ b/core/builtin.onyx @@ -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/. diff --git a/core/os/file.onyx b/core/os/file.onyx index 9e71615c..0d61e550 100644 --- a/core/os/file.onyx +++ b/core/os/file.onyx @@ -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"); diff --git a/core/runtime/common.onyx b/core/runtime/common.onyx index 07730caf..6ffc3497 100644 --- a/core/runtime/common.onyx +++ b/core/runtime/common.onyx @@ -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(); } diff --git a/docs/plan b/docs/plan index 86b84216..1442e06f 100644 --- 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 -- 2.25.1