Documentation
¶
Index ¶
- Constants
- Variables
- func C(msg string, scratch []byte)
- func ConsoleReady(ctx context.Context)
- func D(msg string, args ...any)
- func D2(at int, msg string, args ...any)
- func D3(key uint64, msg string, args ...any)
- func D4(key string, msg string, args ...any)
- func E(msg string, args ...any)
- func E2(at int, msg string, args ...any)
- func E3(key uint64, msg string, args ...any)
- func E4(key string, msg string, args ...any)
- func EE(msg string, args ...any) (err error)
- func EE3(key uint64, msg string, args ...any) (err error)
- func EE4(key string, msg string, args ...any) (err error)
- func Hist(w io.Writer) int
- func I(msg string, args ...any)
- func I2(at int, msg string, args ...any)
- func I3(key uint64, msg string, args ...any)
- func I4(key string, msg string, args ...any)
- func Metrics() string
- func N(string, ...any)
- func N2(int, string, ...any)
- func N3(uint64, string, ...any)
- func NewLogger(tag string) *simpleLogger
- func P(msg string, args ...any)
- func P3(key uint64, msg string, args ...any)
- func P4(key string, msg string, args ...any)
- func PeekAllTraces() []byte
- func R(c bool, msg string, args ...any)
- func Release(key uint64) bool
- func ReleaseStr(key string)
- func S(alleast64k []byte) []byte
- func SetCallerDepth(d uint8)
- func SetConsole(consoleCtx context.Context, c Console)
- func SetConsoleLevel(level LogLevel)
- func SetLevel(level LogLevel)
- func SetTraceCap(n int)
- func StackOutput(w io.Writer) bool
- func T(msg string, args ...any)
- func TALL(msg string, atleast64k []byte)
- func TraceCap() int
- func TraceCount() int
- func U(msg string)
- func U3(key uint64, msg string)
- func U4(key, msg string)
- func V(msg string, args ...any)
- func V2(at int, msg string, args ...any)
- func V3(key uint64, msg string, args ...any)
- func V4(key string, msg string, args ...any)
- func VV(msg string, args ...any)
- func VV2(at int, msg string, args ...any)
- func VV3(key uint64, msg string, args ...any)
- func VV4(key string, msg string, args ...any)
- func W(msg string, args ...any)
- func W2(at int, msg string, args ...any)
- func W3(key uint64, msg string, args ...any)
- func W4(key string, msg string, args ...any)
- func WE(msg string, args ...any) (err error)
- func WE3(key uint64, msg string, args ...any) (err error)
- func WE4(key string, msg string, args ...any) (err error)
- func Wtf(msg string, args ...any)
- type Console
- type FilebasedConsole
- type LogFn
- type LogFn2
- type LogLevel
- type LogStat
- type LogStore
- type Logger
- type Logmsg
- type MemReader
- type Memconsole
- func (mc *Memconsole) BufSize() int
- func (mc *Memconsole) Close() error
- func (mc *Memconsole) Consumed() uint64
- func (mc *Memconsole) Drops() uint64
- func (mc *Memconsole) FDs() (mfd1, mfd2 int)
- func (mc *Memconsole) Flush()
- func (mc *Memconsole) Log(lvl LogLevel, msg Logmsg)
- func (mc *Memconsole) SetReader(r MemReader)
- func (mc *Memconsole) SlotSize() int
- func (mc *Memconsole) Waited() (uint64, uint64)
Constants ¶
const ( // LB512 is the smallest slab, 512 bytes LB512 = 1 << 9 // LB1024 is a 1k slab; sufficient for any single log line // since charsPerLine=800 bounds each formatted line. LB1024 = 1 << 10 // LBMAX is the largest log slab size LBMAX = LB1024 )
Variables ¶
var Debug = false
var Verbose = false
Functions ¶
func ConsoleReady ¶
func EE3 ¶
EE3 logs msg as error and returns it as an error, traced to the LogStore for key (if any).
func NewLogger ¶
func NewLogger(tag string) *simpleLogger
NewLogger creates a new Logger with the given tag.
func PeekAllTraces ¶
func PeekAllTraces() []byte
PeekAllTraces returns the lines from all live trace stores, with stores in creation order and lines oldest first. Each line is followed by a newline. The returned bytes are a snapshot and do not alias the pooled slabs.
func Release ¶
Release removes the LogStore for key, if any, from the global map and recycles its pooled byte slices.
func ReleaseStr ¶
func ReleaseStr(key string)
func SetCallerDepth ¶
func SetCallerDepth(d uint8)
func SetConsole ¶
SetConsole sets external console to redirect log output to.
func SetConsoleLevel ¶
func SetConsoleLevel(level LogLevel)
func SetTraceCap ¶
func SetTraceCap(n int)
SetTraceCap sets the max number of lines each LogStore accumulates, dropping the oldest beyond that. Applies to live stores too. Values <= 0 restore [defaultTraceCap].
func StackOutput ¶
func TraceCap ¶
func TraceCap() int
TraceCap returns the max number of lines each LogStore accumulates.
func U4 ¶
func U4(key, msg string)
U4 logs a user message (notifies the user), tracing it with the hexadecimal key.
func WE3 ¶
WE3 logs msg as warning and returns it as an error, traced to the LogStore for key (if any).
Types ¶
type FilebasedConsole ¶
func NewFilebased ¶
func NewFilebased() (reader *os.File, writer FilebasedConsole, err error)
NewFilebased sets a pipe-backed console and returns reader and writer. Caller is expected to hand off the read fd and read until EOF. Caller owns the reader and write and must close both.
type LogLevel ¶
type LogLevel uint32
const ( VVERBOSE LogLevel = iota // VVERBOSE is the most verbose log level. VERBOSE // VERBOSE is the verbose log level. DEBUG // DEBUG is the debug log level. INFO // INFO is the informational log level. WARN // WARN is the warning log level. ERROR // ERROR is the error log level. STACKTRACE // STACKTRACE is the stack trace log level. USR // USR is interactive log (e.g. as user prompt). NONE // NONE no-ops the logger. )
type LogStat ¶
type LogStat struct {
// Logger identity
Tag string
// Current levels
Level LogLevel
ConsoleLevel LogLevel
// Caller depth
CallerDepth uint8
// Per-level message counts (indexed by LogLevel)
Count [NONE + 1]uint64
// Per-level bytes written to stdout/stderr
Bytes [NONE + 1]uint64
// Per-level dropped (spammy) message counts
Skipped [NONE + 1]uint32
// Total console drops (backpressure)
ConsoleDrops uint32
// Pool statistics
PoolGets uint64
PoolNews uint64
PoolPuts uint64
PoolDrops uint64
// Ring buffer size
RingSize int
// Per-key trace-store indexes and capacity
TrMapSize int
TrMapCap int
TrOrdSize int
TrIdxSize int
// Spam clock: per-level ticks
ClockL1 [NONE + 1]uint8
}
LogStat holds a snapshot of logger, pool, and trace-store statistics.
type LogStore ¶
type LogStore interface {
// Get returns all accumulated lines, oldest first, or nil if this
// store has been consumed or recycled. Get is a read-only peek: it
// may be called repeatedly. The returned bytes are valid until the
// store is released or evicted from the bounded global map.
Get() *[][]byte
// Put adds a single formatted log line to this store. The slab
// (*[]byte from obtain, see logpool.go) is owned by the store once
// put; the oldest line may be evicted to make room.
// Put is a no-op if store is consumed or recycled.
Put(*[]byte)
}
LogStore accumulates formatted log lines for a single trace key, backed by pooled []byte slices from logpool.go.
type Logger ¶
type Logger interface {
SetLevel(level LogLevel)
SetConsoleLevel(level LogLevel)
SetConsole(c Console)
SetCallerDepth(d uint8)
ConsoleReady(ctx context.Context)
Usr(key uint64, msg string)
Printf(msg string, args ...any)
VeryVerbosef(key uint64, at int, msg string, args ...any)
Verbosef(key uint64, at int, msg string, args ...any)
Debugf(key uint64, at int, msg string, args ...any)
Piif(key uint64, at int, msg string, args ...any)
Infof(key uint64, at int, msg string, args ...any)
Warnf(key uint64, at int, msg string, args ...any)
Errorf(key uint64, at int, msg string, args ...any)
Fatalf(at int, msg string, args ...any)
Trace(c bool, t string)
Stack(at int, msg string, scratch []byte)
StackOutput(w io.Writer) bool
Hist(w io.Writer) int
Metrics() *LogStat
}
var Glogger Logger = defaultLogger()
based on: github.com/eycorsican/go-tun2socks/blob/301549c43/common/log/log.go#L5
type Memconsole ¶
type Memconsole struct {
// contains filtered or unexported fields
}
Memconsole is a double-buffered, zero-copy log console. Log lines are written into one of two page-aligned shared-memory regions. When a buffer fills up the reader is signalled via Drain() and the writer flips to the other buffer.
The Go side is always the writer. The reader may live in another runtime.
When the active buffer fills up and draining[other]=true: the write either wraps slotIdx=0 (overwriteOnFull=true, ring-within-buffer) and increments Drops, or is discarded (overwriteOnFull=false) and Drops is incremented. Once consume returns on the other buffer it is available for the next flip.
periodicFlush: if the other buffer is draining when the ticker fires, the ticker re-arms itself for another memFlushInterval rather than stacking concurrent consume calls.
func NewMemoryBased ¶
func NewMemoryBased() (mc *Memconsole, err error)
NewMemoryBased creates a Memconsole backed by page-aligned memfd regions. Returns an error if the kernel does not support memfd_create (Linux < 3.17) or if any syscall fails.
The caller must close mc when done. FDs passed via LogMem should be dup(2)'d by the receiver before use.
func (*Memconsole) BufSize ¶
func (mc *Memconsole) BufSize() int
BufSize returns the byte size of each shared-memory region. Pass this value to the external reader (e.g. via Console.LogMemFD) so it knows how large an mmap to perform.
func (*Memconsole) Close ¶
func (mc *Memconsole) Close() error
Close drains any pending data, waits for any concurrent Drain call to return, calls Close(), then unmaps the shared-memory regions and closes the memfds.
func (*Memconsole) Consumed ¶
func (mc *Memconsole) Consumed() uint64
func (*Memconsole) Drops ¶
func (mc *Memconsole) Drops() uint64
Drops returns the cumulative number of payload bytes dropped when the active buffer could not be flipped because the other buffer was still draining. When overwriteOnFull is true the whole active buffer (slotIdx*memSlotSize bytes of already-written payload) is counted; when false only the still-unwritten tail (yank - i) is counted. Consumed similarly counts bytes returned by MemReader.Drain, so written - Drops ~= consumed + buffered.
func (*Memconsole) FDs ¶
func (mc *Memconsole) FDs() (mfd1, mfd2 int)
FDs returns the raw file descriptor integers for the two shared buffers backed by memfd. The receiver must use dup(2) fds to own these fds.
func (*Memconsole) Flush ¶
func (mc *Memconsole) Flush()
Flush drains the currently active buffer and flips to the other buffer. It is a no-op when console has closed, or when there is no buffer to flush, or when the other buffer is still being drained concurrently.
func (*Memconsole) Log ¶
func (mc *Memconsole) Log(lvl LogLevel, msg Logmsg)
Log implements log.Console. It writes lvl-prefixed msg + '\n' into the active memfd buffer, flushing and flipping when the buffer is full.
func (*Memconsole) SetReader ¶
func (mc *Memconsole) SetReader(r MemReader)
SetReader installs r as the consumer of the Memconsole's shared-memory buffers. Drain(fd, start, end) is called synchronously (but without mc.mu held) each time a buffer fills up or the periodic ticker fires; its return signals that the buffer is free for reuse.
func (*Memconsole) SlotSize ¶
func (mc *Memconsole) SlotSize() int
func (*Memconsole) Waited ¶
func (mc *Memconsole) Waited() (uint64, uint64)