diff options
| author | Colin Davidson <colrdavidson@gmail.com> | 2024-01-17 13:11:10 -0800 |
|---|---|---|
| committer | Colin Davidson <colrdavidson@gmail.com> | 2024-01-17 13:11:10 -0800 |
| commit | d0bb1fb84419670526ada2aba21b14677dbd1566 (patch) | |
| tree | b35b5a2181cd498d9a753eaf3ba308a94eb7f476 /core/log | |
| parent | bfbeb23f5479af375ceea6d97d2add64a6bc6519 (diff) | |
| parent | ea43c030aa0f984679e78e624dab0ceedc56db26 (diff) | |
bring log allocator up to date
Diffstat (limited to 'core/log')
| -rw-r--r-- | core/log/log_allocator.odin | 129 |
1 files changed, 68 insertions, 61 deletions
diff --git a/core/log/log_allocator.odin b/core/log/log_allocator.odin index 846a84a62..322c2e717 100644 --- a/core/log/log_allocator.odin +++ b/core/log/log_allocator.odin @@ -1,19 +1,28 @@ package log import "core:runtime" +import "core:fmt" + +Log_Allocator_Format :: enum { + Bytes, // Actual number of bytes. + Human, // Bytes in human units like bytes, kibibytes, etc. as appropriate. +} Log_Allocator :: struct { allocator: runtime.Allocator, level: Level, prefix: string, locked: bool, + size_fmt: Log_Allocator_Format, } -log_allocator_init :: proc(la: ^Log_Allocator, level: Level, allocator := context.allocator, prefix := "") { +log_allocator_init :: proc(la: ^Log_Allocator, level: Level, size_fmt := Log_Allocator_Format.Bytes, + allocator := context.allocator, prefix := "") { la.allocator = allocator la.level = level la.prefix = prefix la.locked = false + la.size_fmt = size_fmt } @@ -29,78 +38,80 @@ log_allocator_proc :: proc(allocator_data: rawptr, mode: runtime.Allocator_Mode, old_memory: rawptr, old_size: int, location := #caller_location) -> ([]byte, runtime.Allocator_Error) { la := (^Log_Allocator)(allocator_data) + if context.logger.procedure == nil || la.level < context.logger.lowest_level { + return la.allocator.procedure(la.allocator.data, mode, size, alignment, old_memory, old_size, location) + } + padding := " " if la.prefix != "" else "" + buf: [256]byte = --- + if !la.locked { la.locked = true defer la.locked = false switch mode { case .Alloc: - logf( - la.level, - "%s%s>>> ALLOCATOR(mode=.Alloc, size=%d, alignment=%d)", - la.prefix, padding, size, alignment, - location = location, - ) + format: string + switch la.size_fmt { + case .Bytes: format = "%s%s>>> ALLOCATOR(mode=.Alloc, size=%d, alignment=%d)" + case .Human: format = "%s%s>>> ALLOCATOR(mode=.Alloc, size=%m, alignment=%d)" + } + str := fmt.bprintf(buf[:], format, la.prefix, padding, size, alignment) + context.logger.procedure(context.logger.data, la.level, str, context.logger.options, location) + case .Alloc_Non_Zeroed: - logf( - la.level, - "%s%s>>> ALLOCATOR(mode=.Alloc_Non_Zeroed, size=%d, alignment=%d)", - la.prefix, padding, size, alignment, - location = location, - ) + format: string + switch la.size_fmt { + case .Bytes: format = "%s%s>>> ALLOCATOR(mode=.Alloc_Non_Zeroed, size=%d, alignment=%d)" + case .Human: format = "%s%s>>> ALLOCATOR(mode=.Alloc_Non_Zeroed, size=%m, alignment=%d)" + } + str := fmt.bprintf(buf[:], format, la.prefix, padding, size, alignment) + context.logger.procedure(context.logger.data, la.level, str, context.logger.options, location) + case .Free: if old_size != 0 { - logf( - la.level, - "%s%s<<< ALLOCATOR(mode=.Free, ptr=%p, size=%d)", - la.prefix, padding, old_memory, old_size, - location = location, - ) + format: string + switch la.size_fmt { + case .Bytes: format = "%s%s<<< ALLOCATOR(mode=.Free, ptr=%p, size=%d)" + case .Human: format = "%s%s<<< ALLOCATOR(mode=.Free, ptr=%p, size=%m)" + } + str := fmt.bprintf(buf[:], format, la.prefix, padding, old_memory, old_size) + context.logger.procedure(context.logger.data, la.level, str, context.logger.options, location) } else { - logf( - la.level, - "%s%s<<< ALLOCATOR(mode=.Free, ptr=%p)", - la.prefix, padding, old_memory, - location = location, - ) + str := fmt.bprintf(buf[:], "%s%s<<< ALLOCATOR(mode=.Free, ptr=%p)", la.prefix, padding, old_memory) + context.logger.procedure(context.logger.data, la.level, str, context.logger.options, location) } + case .Free_All: - logf( - la.level, - "%s%s<<< ALLOCATOR(mode=.Free_All)", - la.prefix, padding, - location = location, - ) + str := fmt.bprintf(buf[:], "%s%s<<< ALLOCATOR(mode=.Free_All)", la.prefix, padding) + context.logger.procedure(context.logger.data, la.level, str, context.logger.options, location) + case .Resize: - logf( - la.level, - "%s%s>>> ALLOCATOR(mode=.Resize, ptr=%p, old_size=%d, size=%d, alignment=%d)", - la.prefix, padding, old_memory, old_size, size, alignment, - location = location, - ) + format: string + switch la.size_fmt { + case .Bytes: format = "%s%s>>> ALLOCATOR(mode=.Resize, ptr=%p, old_size=%d, size=%d, alignment=%d)" + case .Human: format = "%s%s>>> ALLOCATOR(mode=.Resize, ptr=%p, old_size=%m, size=%m, alignment=%d)" + } + str := fmt.bprintf(buf[:], format, la.prefix, padding, old_memory, old_size, size, alignment) + context.logger.procedure(context.logger.data, la.level, str, context.logger.options, location) + case .Resize_Non_Zeroed: - logf( - la.level, - "%s%s>>> ALLOCATOR(mode=.Resize_Non_Zeroed, ptr=%p, old_size=%d, size=%d, alignment=%d)", - la.prefix, padding, old_memory, old_size, size, alignment, - location = location, - ) + format: string + switch la.size_fmt { + case .Bytes: format = "%s%s>>> ALLOCATOR(mode=.Resize_Non_Zeroed, ptr=%p, old_size=%d, size=%d, alignment=%d)" + case .Human: format = "%s%s>>> ALLOCATOR(mode=.Resize_Non_Zeroed, ptr=%p, old_size=%m, size=%m, alignment=%d)" + } + str := fmt.bprintf(buf[:], format, la.prefix, padding, old_memory, old_size, size, alignment) + context.logger.procedure(context.logger.data, la.level, str, context.logger.options, location) + case .Query_Features: - logf( - la.level, - "%s%ALLOCATOR(mode=.Query_Features)", - la.prefix, padding, - location = location, - ) + str := fmt.bprintf(buf[:], "%s%sALLOCATOR(mode=.Query_Features)", la.prefix, padding) + context.logger.procedure(context.logger.data, la.level, str, context.logger.options, location) + case .Query_Info: - logf( - la.level, - "%s%ALLOCATOR(mode=.Query_Info)", - la.prefix, padding, - location = location, - ) + str := fmt.bprintf(buf[:], "%s%sALLOCATOR(mode=.Query_Info)", la.prefix, padding) + context.logger.procedure(context.logger.data, la.level, str, context.logger.options, location) } } @@ -109,12 +120,8 @@ log_allocator_proc :: proc(allocator_data: rawptr, mode: runtime.Allocator_Mode, la.locked = true defer la.locked = false if err != nil { - logf( - la.level, - "%s%ALLOCATOR ERROR=%v", - la.prefix, padding, error, - location = location, - ) + str := fmt.bprintf(buf[:], "%s%sALLOCATOR ERROR=%v", la.prefix, padding, err) + context.logger.procedure(context.logger.data, la.level, str, context.logger.options, location) } } return data, err |