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 diff --git a/core/os/dir_js.odin b/core/os/dir_js.odin index 8c45cf63b..cd6227507 100644 --- a/core/os/dir_js.odin +++ b/core/os/dir_js.odin @@ -2,6 +2,10 @@ #+private package os +// None of this does anything on js/wasm. +// It's only here so importing `core:os` on wasm panics cleanly, +// without spamming about all sorts of missing procs and types. + import "base:intrinsics" Read_Directory_Iterator_Impl :: struct { @@ -21,4 +25,4 @@ _read_directory_iterator_init :: proc(it: ^Read_Directory_Iterator, f: ^File) { _read_directory_iterator_destroy :: proc(it: ^Read_Directory_Iterator) { -} +} \ No newline at end of file diff --git a/core/os/env_js.odin b/core/os/env_js.odin index 644af61bd..55ffce593 100644 --- a/core/os/env_js.odin +++ b/core/os/env_js.odin @@ -2,16 +2,16 @@ #+private package os +// None of this does anything on js/wasm. +// It's only here so importing `core:os` on wasm panics cleanly, +// without spamming about all sorts of missing procs and types. + import "base:runtime" build_env :: proc() -> (err: Error) { return } -// delete_string_if_not_original :: proc(str: string) { - -// } - @(require_results) _lookup_env_alloc :: proc(key: string, allocator: runtime.Allocator) -> (value: string, found: bool) { return @@ -39,4 +39,4 @@ _clear_env :: proc() { @(require_results) _environ :: proc(allocator: runtime.Allocator) -> (environ: []string, err: Error) { return {}, .Unsupported -} +} \ No newline at end of file diff --git a/core/os/errors_js.odin b/core/os/errors_js.odin index 34779807d..368b94d68 100644 --- a/core/os/errors_js.odin +++ b/core/os/errors_js.odin @@ -2,6 +2,10 @@ #+private package os +// None of this does anything on js/wasm. +// It's only here so importing `core:os` on wasm panics cleanly, +// without spamming about all sorts of missing procs and types. + _Platform_Error :: enum i32 {} _error_string :: proc(errno: i32) -> string { @@ -10,4 +14,4 @@ _error_string :: proc(errno: i32) -> string { _get_platform_error :: proc(errno: _Platform_Error) -> Error { return Platform_Error(errno) -} +} \ No newline at end of file diff --git a/core/os/file_js.odin b/core/os/file_js.odin index 5d921c9b0..aef811c11 100644 --- a/core/os/file_js.odin +++ b/core/os/file_js.odin @@ -2,12 +2,17 @@ #+private package os -import "base:runtime" +// None of this does anything on js/wasm. +// It's only here so importing `core:os` on wasm panics cleanly, +// without spamming about all sorts of missing procs and types. +import "base:runtime" import "core:io" import "core:time" -File_Impl :: distinct rawptr +File_Impl :: struct { + file: File, +} _open :: proc(name: string, flags: File_Flags, perm: Permissions) -> (f: ^File, err: Error) { return nil, .Unsupported diff --git a/core/os/heap_js.odin b/core/os/heap_js.odin index 5f8d9bd35..ebc8f902d 100644 --- a/core/os/heap_js.odin +++ b/core/os/heap_js.odin @@ -2,6 +2,10 @@ #+private package os +// None of this does anything on js/wasm. +// It's only here so importing `core:os` on wasm panics cleanly, +// without spamming about all sorts of missing procs and types. + import "base:runtime" -_heap_allocator_proc :: runtime.wasm_allocator_proc +_heap_allocator_proc :: runtime.wasm_allocator_proc \ No newline at end of file diff --git a/core/os/path_js.odin b/core/os/path_js.odin index 6ac845560..874b45c89 100644 --- a/core/os/path_js.odin +++ b/core/os/path_js.odin @@ -2,6 +2,10 @@ #+private package os +// None of this does anything on js/wasm. +// It's only here so importing `core:os` on wasm panics cleanly, +// without spamming about all sorts of missing procs and types. + import "base:runtime" _Path_Separator :: '/' diff --git a/core/os/pipe_js.odin b/core/os/pipe_js.odin index aea5e9a83..5e724ea00 100644 --- a/core/os/pipe_js.odin +++ b/core/os/pipe_js.odin @@ -2,6 +2,10 @@ #+private package os +// None of this does anything on js/wasm. +// It's only here so importing `core:os` on wasm panics cleanly, +// without spamming about all sorts of missing procs and types. + _pipe :: proc() -> (r, w: ^File, err: Error) { err = .Unsupported return @@ -11,4 +15,4 @@ _pipe :: proc() -> (r, w: ^File, err: Error) { _pipe_has_data :: proc(r: ^File) -> (ok: bool, err: Error) { err = .Unsupported return -} +} \ No newline at end of file diff --git a/core/os/process_js.odin b/core/os/process_js.odin index 6283d270c..9b682da0c 100644 --- a/core/os/process_js.odin +++ b/core/os/process_js.odin @@ -2,6 +2,10 @@ #+private package os +// None of this does anything on js/wasm. +// It's only here so importing `core:os` on wasm panics cleanly, +// without spamming about all sorts of missing procs and types. + import "base:runtime" import "core:time" diff --git a/core/os/stat_js.odin b/core/os/stat_js.odin index bad75486b..a304435d9 100644 --- a/core/os/stat_js.odin +++ b/core/os/stat_js.odin @@ -2,6 +2,10 @@ #+private package os +// None of this does anything on js/wasm. +// It's only here so importing `core:os` on wasm panics cleanly, +// without spamming about all sorts of missing procs and types. + import "base:runtime" _fstat :: proc(f: ^File, allocator: runtime.Allocator) -> (fi: File_Info, err: Error) { diff --git a/core/os/temp_file_js.odin b/core/os/temp_file_js.odin index 32ce1c484..53158d843 100644 --- a/core/os/temp_file_js.odin +++ b/core/os/temp_file_js.odin @@ -2,6 +2,10 @@ #+private package os +// None of this does anything on js/wasm. +// It's only here so importing `core:os` on wasm panics cleanly, +// without spamming about all sorts of missing procs and types. + import "base:runtime" _temp_dir :: proc(allocator: runtime.Allocator) -> (string, runtime.Allocator_Error) { diff --git a/core/os/wasm.odin b/core/os/wasm.odin new file mode 100644 index 000000000..79c00f694 --- /dev/null +++ b/core/os/wasm.odin @@ -0,0 +1,4 @@ +#+build js wasm32, js wasm64p32 +package os + +#panic("core:os is unsupported on js/wasm") \ No newline at end of file