aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2022-08-08 11:57:51 +0100
committergingerBill <bill@gingerbill.org>2022-08-08 11:57:51 +0100
commit4912ecc3ea29c719931b04b38ee4ee4374b42a64 (patch)
tree944f902063d60ff1d6168858634db3e8206a7f57
parentedba99d6360802da152a850056e6754cfed25d27 (diff)
Add `log.Log_Allocator`
-rw-r--r--core/log/log.odin11
-rw-r--r--core/log/log_allocator.odin76
2 files changed, 81 insertions, 6 deletions
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