diff --git a/core/log/log.odin b/core/log/log.odin index d34a135cc..a699247b8 100644 --- a/core/log/log.odin +++ b/core/log/log.odin @@ -6,7 +6,6 @@ import "core:fmt" // NOTE(bill, 2019-12-31): These are defined in `package runtime` as they are used in the `context`. This is to prevent an import definition cycle. -Level :: runtime.Logger_Level /* Logger_Level :: enum { Debug = 0, @@ -16,8 +15,8 @@ Logger_Level :: enum { Fatal = 40, } */ +Level :: runtime.Logger_Level -Option :: runtime.Logger_Option /* Option :: enum { Level, @@ -30,11 +29,12 @@ Option :: enum { Terminal_Color } */ +Option :: runtime.Logger_Option -Options :: runtime.Logger_Options /* Options :: bit_set[Option]; */ +Options :: runtime.Logger_Options Full_Timestamp_Opts :: Options{ .Date, @@ -52,12 +52,11 @@ Location_File_Opts :: Options{ } -Logger_Proc :: runtime.Logger_Proc /* Logger_Proc :: #type proc(data: rawptr, level: Level, text: string, options: Options, location := #caller_location); */ +Logger_Proc :: runtime.Logger_Proc -Logger :: runtime.Logger /* Logger :: struct { procedure: Logger_Proc, @@ -66,6 +65,7 @@ Logger :: struct { options: Logger_Options, } */ +Logger :: runtime.Logger nil_logger_proc :: proc(data: rawptr, level: Level, text: string, options: Options, location := #caller_location) { // Do nothing @@ -75,7 +75,6 @@ nil_logger :: proc() -> Logger { return Logger{nil_logger_proc, nil, Level.Debug, nil} } -// TODO(bill): Should these be redesigned so that they are do not rely upon `package fmt`? debugf :: proc(fmt_str: string, args: ..any, location := #caller_location) { logf(level=.Debug, fmt_str=fmt_str, args=args, location=location) } diff --git a/core/log/log_allocator.odin b/core/log/log_allocator.odin new file mode 100644 index 000000000..e703ee1a9 --- /dev/null +++ b/core/log/log_allocator.odin @@ -0,0 +1,76 @@ +package log + +import "core:runtime" + +Log_Allocator :: struct { + allocator: runtime.Allocator, + level: Level, + prefix: string, +} + +log_allocator_init :: proc(la: ^Log_Allocator, level: Level, allocator := context.allocator, prefix := "") { + la.allocator = allocator + la.level = level + la.prefix = prefix +} + + +log_allocator :: proc(la: ^Log_Allocator) -> runtime.Allocator { + return runtime.Allocator{ + procedure = log_allocator_proc, + data = la, + } +} + +log_allocator_proc :: proc(allocator_data: rawptr, mode: runtime.Allocator_Mode, + size, alignment: int, + old_memory: rawptr, old_size: int, location := #caller_location) -> ([]byte, runtime.Allocator_Error) { + la := (^Log_Allocator)(allocator_data) + + switch mode { + case .Alloc: + logf( + level=la.level, + fmt_str = "%s%s>>> ALLOCATOR(mode=.Alloc, size=%d, alignment=%d)", + args = {la.prefix, " " if la.prefix != "" else "", size, alignment}, + location = location, + ) + case .Free: + logf( + level=la.level, + fmt_str = "%s%s<<< ALLOCATOR(mode=.Free, ptr=%p, size=%d)", + args = {la.prefix, " " if la.prefix != "" else "", old_memory, old_size}, + location = location, + ) + case .Free_All: + logf( + level=la.level, + fmt_str = "%s%s<<< ALLOCATOR(mode=.Free_All)", + args = {la.prefix, " " if la.prefix != "" else ""}, + location = location, + ) + case .Resize: + logf( + level=la.level, + fmt_str = "%s%s>>> ALLOCATOR(mode=.Resize, ptr=%p, old_size=%d, size=%d, alignment=%d)", + args = {la.prefix, " " if la.prefix != "" else "", old_memory, old_size, size, alignment}, + location = location, + ) + case .Query_Features: + logf( + level=la.level, + fmt_str = "%s%ALLOCATOR(mode=.Query_Features)", + args = {la.prefix, " " if la.prefix != "" else ""}, + location = location, + ) + case .Query_Info: + logf( + level=la.level, + fmt_str = "%s%ALLOCATOR(mode=.Query_Info)", + args = {la.prefix, " " if la.prefix != "" else ""}, + location = location, + ) + } + + return la.allocator.procedure(la.allocator.data, mode, size, alignment, old_memory, old_size, location) +} \ No newline at end of file