package benchmarks
import (
"context"
"fmt"
"io"
"log/slog"
"log/slog/internal/buffer"
"strconv"
"time"
)
type fastTextHandler struct {
w io .Writer
}
func newFastTextHandler(w io .Writer ) slog .Handler {
return &fastTextHandler {w : w }
}
func (h *fastTextHandler ) Enabled (context .Context , slog .Level ) bool { return true }
func (h *fastTextHandler ) Handle (_ context .Context , r slog .Record ) error {
buf := buffer .New ()
defer buf .Free ()
if !r .Time .IsZero () {
buf .WriteString ("time=" )
h .appendTime (buf , r .Time )
buf .WriteByte (' ' )
}
buf .WriteString ("level=" )
*buf = strconv .AppendInt (*buf , int64 (r .Level ), 10 )
buf .WriteByte (' ' )
buf .WriteString ("msg=" )
buf .WriteString (r .Message )
r .Attrs (func (a slog .Attr ) bool {
buf .WriteByte (' ' )
buf .WriteString (a .Key )
buf .WriteByte ('=' )
h .appendValue (buf , a .Value )
return true
})
buf .WriteByte ('\n' )
_ , err := h .w .Write (*buf )
return err
}
func (h *fastTextHandler ) appendValue (buf *buffer .Buffer , v slog .Value ) {
switch v .Kind () {
case slog .KindString :
buf .WriteString (v .String ())
case slog .KindInt64 :
*buf = strconv .AppendInt (*buf , v .Int64 (), 10 )
case slog .KindUint64 :
*buf = strconv .AppendUint (*buf , v .Uint64 (), 10 )
case slog .KindFloat64 :
*buf = strconv .AppendFloat (*buf , v .Float64 (), 'g' , -1 , 64 )
case slog .KindBool :
*buf = strconv .AppendBool (*buf , v .Bool ())
case slog .KindDuration :
*buf = strconv .AppendInt (*buf , v .Duration ().Nanoseconds (), 10 )
case slog .KindTime :
h .appendTime (buf , v .Time ())
case slog .KindAny :
a := v .Any ()
switch a := a .(type ) {
case error :
buf .WriteString (a .Error())
default :
fmt .Fprint (buf , a )
}
default :
panic (fmt .Sprintf ("bad kind: %s" , v .Kind ()))
}
}
func (h *fastTextHandler ) appendTime (buf *buffer .Buffer , t time .Time ) {
*buf = strconv .AppendInt (*buf , t .Unix (), 10 )
}
func (h *fastTextHandler ) WithAttrs ([]slog .Attr ) slog .Handler {
panic ("fastTextHandler: With unimplemented" )
}
func (*fastTextHandler ) WithGroup (string ) slog .Handler {
panic ("fastTextHandler: WithGroup unimplemented" )
}
type asyncHandler struct {
ringBuffer [100 ]slog .Record
next int
}
func newAsyncHandler() *asyncHandler {
return &asyncHandler {}
}
func (*asyncHandler ) Enabled (context .Context , slog .Level ) bool { return true }
func (h *asyncHandler ) Handle (_ context .Context , r slog .Record ) error {
h .ringBuffer [h .next ] = r .Clone ()
h .next = (h .next + 1 ) % len (h .ringBuffer )
return nil
}
func (*asyncHandler ) WithAttrs ([]slog .Attr ) slog .Handler {
panic ("asyncHandler: With unimplemented" )
}
func (*asyncHandler ) WithGroup (string ) slog .Handler {
panic ("asyncHandler: WithGroup unimplemented" )
}
type disabledHandler struct {}
func (disabledHandler ) Enabled (context .Context , slog .Level ) bool { return false }
func (disabledHandler ) Handle (context .Context , slog .Record ) error { panic ("should not be called" ) }
func (disabledHandler ) WithAttrs ([]slog .Attr ) slog .Handler {
panic ("disabledHandler: With unimplemented" )
}
func (disabledHandler ) WithGroup (string ) slog .Handler {
panic ("disabledHandler: WithGroup unimplemented" )
}
The pages are generated with Golds v0.7.0-preview . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .