mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 15:18:49 +00:00
log allocator: add option to switch between bytes and human format
This commit is contained in:
+34
-25
@@ -2,18 +2,26 @@ package log
|
|||||||
|
|
||||||
import "core:runtime"
|
import "core:runtime"
|
||||||
|
|
||||||
|
Log_Allocator_Format :: enum {
|
||||||
|
Bytes, // Actual number of bytes.
|
||||||
|
Human, // Bytes in human units like bytes, kibibytes, etc. as appropriate.
|
||||||
|
}
|
||||||
|
|
||||||
Log_Allocator :: struct {
|
Log_Allocator :: struct {
|
||||||
allocator: runtime.Allocator,
|
allocator: runtime.Allocator,
|
||||||
level: Level,
|
level: Level,
|
||||||
prefix: string,
|
prefix: string,
|
||||||
locked: bool,
|
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.allocator = allocator
|
||||||
la.level = level
|
la.level = level
|
||||||
la.prefix = prefix
|
la.prefix = prefix
|
||||||
la.locked = false
|
la.locked = false
|
||||||
|
la.size_fmt = size_fmt
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -37,27 +45,27 @@ log_allocator_proc :: proc(allocator_data: rawptr, mode: runtime.Allocator_Mode,
|
|||||||
|
|
||||||
switch mode {
|
switch mode {
|
||||||
case .Alloc:
|
case .Alloc:
|
||||||
logf(
|
fmt: string
|
||||||
la.level,
|
switch la.size_fmt {
|
||||||
"%s%s>>> ALLOCATOR(mode=.Alloc, size=%m, alignment=%d)",
|
case .Bytes: fmt = "%s%s>>> ALLOCATOR(mode=.Alloc, size=%d, alignment=%d)"
|
||||||
la.prefix, padding, size, alignment,
|
case .Human: fmt = "%s%s>>> ALLOCATOR(mode=.Alloc, size=%m, alignment=%d)"
|
||||||
location = location,
|
}
|
||||||
)
|
logf(la.level, fmt, la.prefix, padding, size, alignment, location = location)
|
||||||
case .Alloc_Non_Zeroed:
|
case .Alloc_Non_Zeroed:
|
||||||
logf(
|
fmt: string
|
||||||
la.level,
|
switch la.size_fmt {
|
||||||
"%s%s>>> ALLOCATOR(mode=.Alloc_Non_Zeroed, size=%m, alignment=%d)",
|
case .Bytes: fmt = "%s%s>>> ALLOCATOR(mode=.Alloc_Non_Zeroed, size=%d, alignment=%d)"
|
||||||
la.prefix, padding, size, alignment,
|
case .Human: fmt = "%s%s>>> ALLOCATOR(mode=.Alloc_Non_Zeroed, size=%m, alignment=%d)"
|
||||||
location = location,
|
}
|
||||||
)
|
logf(la.level, fmt, la.prefix, padding, size, alignment, location = location)
|
||||||
case .Free:
|
case .Free:
|
||||||
if old_size != 0 {
|
if old_size != 0 {
|
||||||
logf(
|
fmt: string
|
||||||
la.level,
|
switch la.size_fmt {
|
||||||
"%s%s<<< ALLOCATOR(mode=.Free, ptr=%p, size=%m)",
|
case .Bytes: fmt = "%s%s<<< ALLOCATOR(mode=.Free, ptr=%p, size=%d)"
|
||||||
la.prefix, padding, old_memory, old_size,
|
case .Human: fmt = "%s%s<<< ALLOCATOR(mode=.Free, ptr=%p, size=%m)"
|
||||||
location = location,
|
}
|
||||||
)
|
logf(la.level, fmt, la.prefix, padding, old_memory, old_size, location = location)
|
||||||
} else {
|
} else {
|
||||||
logf(
|
logf(
|
||||||
la.level,
|
la.level,
|
||||||
@@ -74,12 +82,13 @@ log_allocator_proc :: proc(allocator_data: rawptr, mode: runtime.Allocator_Mode,
|
|||||||
location = location,
|
location = location,
|
||||||
)
|
)
|
||||||
case .Resize:
|
case .Resize:
|
||||||
logf(
|
fmt: string
|
||||||
la.level,
|
switch la.size_fmt {
|
||||||
"%s%s>>> ALLOCATOR(mode=.Resize, ptr=%p, old_size=%m, size=%m, alignment=%d)",
|
case .Bytes: fmt = "%s%s>>> ALLOCATOR(mode=.Resize, ptr=%p, old_size=%d, size=%d, alignment=%d)"
|
||||||
la.prefix, padding, old_memory, old_size, size, alignment,
|
case .Human: fmt = "%s%s>>> ALLOCATOR(mode=.Resize, ptr=%p, old_size=%m, size=%m, alignment=%d)"
|
||||||
location = location,
|
}
|
||||||
)
|
logf(la.level, fmt, la.prefix, padding, old_memory, old_size, size, alignment, location = location)
|
||||||
|
|
||||||
case .Query_Features:
|
case .Query_Features:
|
||||||
logf(
|
logf(
|
||||||
la.level,
|
la.level,
|
||||||
|
|||||||
Reference in New Issue
Block a user