Files
lneto/internal/debug_heaplog.go
Pat Whittingslow 157a8537a2 testing.B.Loop() standardization and StackAsync.Debug improvement (#156)
* testing.B.Loop() standardization and StackAsync.Debug heap debugging improvement

* apply @MDr164 fixes

* @MDr164 great suggestions to prevent panic and print correct mallocs
2026-07-17 11:59:27 -03:00

55 lines
1.1 KiB
Go

//go:build debugheaplog
package internal
import (
"log/slog"
"time"
"unsafe"
)
const (
HeapAllocDebugging = true
timefmt = "[01-02 15:04:05.000]"
)
var (
timebuf [len(timefmt) * 2]byte
)
func LogEnabled(l *slog.Logger, lvl slog.Level) bool {
return true
}
func logAttrsAndAllocs(allocmsg string, l *slog.Logger, level slog.Level, msg string, attrs ...slog.Attr) {
LogAttrs(nil, level, msg, attrs...) // already logs attributes
}
func LogAttrs(_ *slog.Logger, level slog.Level, msg string, attrs ...slog.Attr) {
now := time.Now()
n := len(now.AppendFormat(timebuf[:0], timefmt))
print("time=", unsafe.String(&timebuf[0], n), " ")
if level == LevelTrace {
print("TRACE ")
} else if level < slog.LevelDebug {
print("SEQS ")
} else {
print(level.String(), " ")
}
print(msg)
for _, a := range attrs {
switch a.Value.Kind() {
case slog.KindString:
print(" ", a.Key, "=", a.Value.String())
case slog.KindInt64:
print(" ", a.Key, "=", a.Value.Int64())
case slog.KindUint64:
print(" ", a.Key, "=", a.Value.Uint64())
case slog.KindBool:
print(" ", a.Key, "=", a.Value.Bool())
}
}
println()
LogAllocs(msg)
}