aboutsummaryrefslogtreecommitdiff
path: root/core/log/console_logger_js.odin
diff options
context:
space:
mode:
authorJeroen van Rijn <Kelimion@users.noreply.github.com>2026-02-12 12:02:11 +0100
committerJeroen van Rijn <Kelimion@users.noreply.github.com>2026-02-12 12:12:21 +0100
commit658e6b9d9fabe0b249f552257fe8918b010003b0 (patch)
treeb3e66caa49222e878e5c4a6124433a6513a069d4 /core/log/console_logger_js.odin
parent97683ae0146e6668d56e701c1d54a4e1737e41c8 (diff)
Add `core:log` support for js/wasm
Also add #panic when importing `core:os` on wasm.
Diffstat (limited to 'core/log/console_logger_js.odin')
-rw-r--r--core/log/console_logger_js.odin119
1 files changed, 119 insertions, 0 deletions
diff --git a/core/log/console_logger_js.odin b/core/log/console_logger_js.odin
new file mode 100644
index 000000000..1e07b6cdc
--- /dev/null
+++ b/core/log/console_logger_js.odin
@@ -0,0 +1,119 @@
+#+build js
+package log
+
+import "core:fmt"
+import "core:strings"
+import "core:time"
+
+Level_Headers := [?]string{
+ 0..<10 = "[DEBUG] --- ",
+ 10..<20 = "[INFO ] --- ",
+ 20..<30 = "[WARN ] --- ",
+ 30..<40 = "[ERROR] --- ",
+ 40..<50 = "[FATAL] --- ",
+}
+
+Default_Console_Logger_Opts :: Options{
+ .Level,
+ .Short_File_Path,
+ .Line,
+ .Procedure,
+} | Full_Timestamp_Opts
+
+Console_Logger_Data :: struct {
+ ident: string,
+}
+
+create_console_logger :: proc(lowest := Level.Debug, opt := Default_Console_Logger_Opts, ident := "", allocator := context.allocator) -> Logger {
+ data := new(Console_Logger_Data, allocator)
+ data.ident = strings.clone(ident)
+ return Logger{console_logger_proc, data, lowest, opt}
+}
+
+destroy_console_logger :: proc(log: Logger, allocator := context.allocator) {
+ data := cast(^Console_Logger_Data)log.data
+ delete(data.ident)
+ free(log.data, allocator)
+}
+
+console_logger_proc :: proc(logger_data: rawptr, level: Level, text: string, options: Options, location := #caller_location) {
+ options := options
+ data := cast(^Console_Logger_Data)logger_data
+
+ backing: [1024]byte //NOTE(Hoej): 1024 might be too much for a header backing, unless somebody has really long paths.
+ buf := strings.builder_from_bytes(backing[:])
+
+ if .Level in options {
+ fmt.sbprint(&buf, Level_Headers[level])
+ }
+
+ when time.IS_SUPPORTED {
+ do_time_header(options, &buf, time.now())
+ }
+
+ do_location_header(options, &buf, location)
+
+ if data.ident != "" {
+ fmt.sbprintf(&buf, "[%s] ", data.ident)
+ }
+
+ h := fmt.stderr if level >= .Error else fmt.stdout
+
+ //TODO(Hoej): When we have better atomics and such, make this thread-safe
+ fmt.fprintf(h, "%s%s\n", strings.to_string(buf), text)
+}
+
+do_time_header :: proc(opts: Options, buf: ^strings.Builder, t: time.Time) {
+ when time.IS_SUPPORTED {
+ if Full_Timestamp_Opts & opts != nil {
+ fmt.sbprint(buf, "[")
+ y, m, d := time.date(t)
+ h, min, s := time.clock(t)
+ if .Date in opts {
+ fmt.sbprintf(buf, "%d-%02d-%02d", y, m, d)
+ if .Time in opts {
+ fmt.sbprint(buf, " ")
+ }
+ }
+ if .Time in opts { fmt.sbprintf(buf, "%02d:%02d:%02d", h, min, s) }
+ fmt.sbprint(buf, "] ")
+ }
+ }
+}
+
+do_location_header :: proc(opts: Options, buf: ^strings.Builder, location := #caller_location) {
+ if Location_Header_Opts & opts == nil {
+ return
+ }
+ fmt.sbprint(buf, "[")
+
+ file := location.file_path
+ if .Short_File_Path in opts {
+ last := 0
+ for r, i in location.file_path {
+ if r == '/' {
+ last = i+1
+ }
+ }
+ file = location.file_path[last:]
+ }
+
+ if Location_File_Opts & opts != nil {
+ fmt.sbprint(buf, file)
+ }
+ if .Line in opts {
+ if Location_File_Opts & opts != nil {
+ fmt.sbprint(buf, ":")
+ }
+ fmt.sbprint(buf, location.line)
+ }
+
+ if .Procedure in opts {
+ if (Location_File_Opts | {.Line}) & opts != nil {
+ fmt.sbprint(buf, ":")
+ }
+ fmt.sbprintf(buf, "%s()", location.procedure)
+ }
+
+ fmt.sbprint(buf, "] ")
+} \ No newline at end of file