log

package
v0.0.0-...-e165ebb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 15, 2026 License: MPL-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func C

func C(msg string, scratch []byte)

C logs the stack trace of the current goroutine to Console.

func ConsoleReady

func ConsoleReady(ctx context.Context)

func D

func D(msg string, args ...any)

D logs a debug message.

func D2

func D2(at int, msg string, args ...any)

func E

func E(msg string, args ...any)

E logs an error message.

func E2

func E2(at int, msg string, args ...any)

func EE

func EE(msg string, args ...any) (err error)

func I

func I(msg string, args ...any)

I logs an info message.

func I2

func I2(at int, msg string, args ...any)

func N

func N(string, ...any)

N is a no-op logger.

func N2

func N2(int, string, ...any)

N2 is a no-op logger.

func NewLogger

func NewLogger(tag string) *simpleLogger

NewLogger creates a new Glogger with the given tag.

func P

func P(msg string, args ...any)

P logs a private message.

func R

func R(c bool, msg string, args ...any)

R logs msg to as error to log if c is false, or to console otherwise.

func SetConsole

func SetConsole(consoleCtx context.Context, c Console)

SetConsole sets external console to redirect log output to.

func SetConsoleLevel

func SetConsoleLevel(level LogLevel)

func SetLevel

func SetLevel(level LogLevel)

func T

func T(msg string, args ...any)

T logs the stack trace of the current goroutine.

func TALL

func TALL(msg string, atleast64k []byte)

TALL logs the stack trace of all active goroutines.

func U

func U(msg string)

U logs a user message (notifies the user).

func V

func V(msg string, args ...any)

V logs a verbose message.

func V2

func V2(at int, msg string, args ...any)

func VV

func VV(msg string, args ...any)

VV logs a very verbose message.

func VV2

func VV2(at int, msg string, args ...any)

func W

func W(msg string, args ...any)

W logs a warning message.

func W2

func W2(at int, msg string, args ...any)

func WE

func WE(msg string, args ...any) (err error)

func Wtf

func Wtf(msg string, args ...any)

Wtf logs a fatal message.

Types

type Console

type Console interface {
	// Log logs a multi-line msg.
	Log(level LogLevel, msg Logmsg)
}

Console is an external logger.

type FilebasedConsole

type FilebasedConsole interface {
	Console
	io.Closer
	File() *os.File
}

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 LogFn

type LogFn func(string, ...any)

func Of

func Of(tag string, l LogFn2) LogFn

type LogFn2

type LogFn2 func(int, string, ...any)

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.
)

func LevelOf

func LevelOf(level int32) LogLevel

type Logger

type Logger interface {
	SetLevel(level LogLevel)
	SetConsoleLevel(level LogLevel)
	SetConsole(c Console)
	ConsoleReady(ctx context.Context)
	Usr(msg string)
	Printf(msg string, args ...any)
	VeryVerbosef(at int, msg string, args ...any)
	Verbosef(at int, msg string, args ...any)
	Debugf(at int, msg string, args ...any)
	Piif(at int, msg string, args ...any)
	Infof(at int, msg string, args ...any)
	Warnf(at int, msg string, args ...any)
	Errorf(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)
}
var Glogger Logger = defaultLogger()

based on: github.com/eycorsican/go-tun2socks/blob/301549c43/common/log/log.go#L5

type Logmsg

type Logmsg = string

type MemReader

type MemReader interface {
	Drain(fd, start, end int) int
	OnClose() bool
}

MemReader is the consumer side of a Memconsole double-buffer.

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 buffer-full events where the active buffer could not be flipped (other buffer still draining) and the write either overwrote the oldest slot (overwriteOnFull=true) or was discarded (overwriteOnFull=false).

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) Waited

func (mc *Memconsole) Waited() (uint64, uint64)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL