logx

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: Apache-2.0 Imports: 21 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package logx is the log of djinn up, to debug a freeze or a quit that hangs (W511): levelled lines (log/slog) in logs/djinn.log of the data folder, rolled over in the process on every system, and goroutine dumps beside them. It never logs a secret, a prompt or what a person wrote: names, durations, exit codes and errors.

Index

Constants

View Source
const (
	// DumpPrefix starts the name of a goroutine dump in the log folder; its time follows, then ".txt".
	DumpPrefix = "goroutines-"
	// DumpsKept is how many goroutine dumps the log folder keeps: the oldest go first.
	DumpsKept = 5
	// DumpRequest is the file whose presence in the log folder asks a djinn up for a dump, on any system: it removes
	// it, then dumps (djinn debug dump, when the server does not answer).
	DumpRequest = "dump.request"
)
View Source
const (
	// HeldLong is how long a watched lock may be held before Watch logs its holder at warn (W549).
	HeldLong = 10 * time.Second
	// HeldFrozen is how long a watched lock may be held before Watch logs at error and dumps the goroutines: no work
	// holds a lock that long, the process froze.
	HeldFrozen = 3 * HeldLong
)
View Source
const (
	// Dir is the folder of the logs and the dumps, in the data folder.
	Dir = "logs"
	// File is the log of djinn up, in Dir.
	File = "djinn.log"
	// Console is what a djinn up started in the background writes on its output (the window's toolkit, panics), in Dir.
	Console = "console.log"
	// MaxSize is the size at which File rolls over.
	MaxSize = 10 << 20
	// Keep is how many rolled-over files File keeps: File and they hold at most (Keep+1) × MaxSize, 50 MB.
	Keep = 4
	// Level is the variable that sets the lowest level logged: debug, info (the default), warn or error.
	Level = "DJINN_LOG"
	// Slow is the time past which a call, a pass or a wait logs at warn.
	Slow = time.Second
)
View Source
const (
	// CrashFile receives what the runtime writes when djinn up dies of a panic or a fatal error, in Dir: the runtime
	// writes it on the standard error too, which only a background djinn up keeps (Console). It is empty while djinn
	// up runs, and removed when it stops.
	CrashFile = "crash.txt"
	// CrashPrefix starts the name a start gives the CrashFile the last djinn up left: crash-<time>.txt, its time the
	// crash's.
	CrashPrefix = "crash-"
	// CrashesKept is how many crash files the log folder keeps: the oldest go first.
	CrashesKept = 5
)
View Source
const HungExit = 3

HungExit is the exit code of a djinn up whose stop outlived ExitAfter.

Variables

This section is empty.

Functions

func Argv

func Argv(args []string) string

Argv is args as logged: the program by its base name, a message, title or body hidden, a password or token of a URL hidden, each argument cut at argMax, and past argsMax only their count.

func Crash added in v0.1.2

func Crash(where string, args ...any)

Crash, deferred first in a goroutine of djinn up that cannot go on after a panic (a loop of the harness, a reader), logs the panic at error, with where it happened (the loop, then key-value pairs: task, wish), its stack, and that djinn up stops; then it panics again: the runtime ends the process, and writes the panic to CrashFile and the standard error. Without a panic it does nothing.

func Crashes added in v0.1.2

func Crashes(dir string) []string

Crashes lists the crash files of dir, oldest first.

func Cut

func Cut(s string, n int) string

Cut is s on one line, cut at n bytes.

func Dump

func Dump(dir, why string) (string, error)

Dump writes the stacks of every goroutine of the process, with how long each waits, to a new file of dir, logs it with why, and removes the oldest dumps past DumpsKept. It returns the file's path.

func Dumps

func Dumps(dir string) []string

Dumps lists the goroutine dumps of dir, oldest first.

func Folder

func Folder(home string) string

Folder is the folder of the logs of the data folder home.

func L

func L() *slog.Logger

L is the log: it discards everything until Start, as in tests.

func Lock

func Lock(mu *sync.Mutex, name string)

Lock locks mu, and logs at warn a wait past Slow for the lock name: who holds it held up the others.

func New

func New(w io.Writer, level slog.Leveler) *slog.Logger

New is a log written to w as text lines (key=value), from level up.

func Panicked added in v0.1.2

func Panicked(where string, p any, args ...any)

Panicked logs at error p, a panic recovered in where where djinn up goes on (a call to the server, a callback of the window), with its stack.

func ParseLevel

func ParseLevel(s string) (slog.Level, error)

ParseLevel reads debug, info, warn or error; empty is info.

func Ran

func Ran(ctx context.Context, cmd *exec.Cmd, start time.Time, err error)

Ran logs cmd once it has run since start, with the error it returned: its program and arguments (Args), its folder, how long it took, its exit code, and whether its context killed it at a deadline (W511). A command that succeeds in less than Slow logs at debug; one that exits non-zero logs at info; one past Slow or killed at its deadline, at warn; one that could not run or died of something else than its exit code or its context, at error (Q243). ctx is the context cmd ran under, or nil; err is what running it returned, or nil when only its exit code is known.

func Shift

func Shift(path string, keep int) error

Shift moves path to path.1 after moving each path.i to path.i+1, removing path.keep first; with keep 0 it removes path. A file missing on the way is no error.

func Start

func Start(home string) (stop func() error, err error)

Start opens the log of the data folder home, rolled over at MaxSize, at the level DJINN_LOG names, and makes it the log, and the log package's, until stop. A panic or a fatal error that ends the process lands in CrashFile beside it; the one the last djinn up left is kept under its time, and logged.

func Took

func Took(level slog.Level, msg string, start time.Time, args ...any)

Took logs msg with how long since start, at warn past Slow, at level otherwise.

func Use

func Use(l *slog.Logger) (restore func())

Use makes l the log until the returned function restores the one before; tests capture the log with it.

Types

type Held added in v0.1.2

type Held struct {
	// contains filtered or unexported fields
}

Held knows since when, and from where, a lock is held, so that Watch names a holder that keeps it too long: the next freeze names itself. Mutex is one; a lock it does not own (the database's one connection) calls Take and Give.

func (*Held) Give added in v0.1.2

func (h *Held) Give()

Give records that the lock is no longer held.

func (*Held) Take added in v0.1.2

func (h *Held) Take()

Take records that the lock is held from now, by its caller.

func (*Held) Watch added in v0.1.2

func (h *Held) Watch(ctx context.Context, name, dir string)

Watch looks at the lock every second until ctx ends: held past HeldLong, it logs at warn where its holder took it; past HeldFrozen, once more at error, with a dump of every goroutine to dir (Dump), which shows what the holder waits for. Each hold is logged once at each level. dir "": no dump.

type Mutex added in v0.1.2

type Mutex struct {
	// contains filtered or unexported fields
}

Mutex is a sync.Mutex that knows its holder: Watch logs one that keeps it past HeldLong.

func (*Mutex) Lock added in v0.1.2

func (m *Mutex) Lock()

Lock locks m and records the caller as its holder.

func (*Mutex) TryLock added in v0.1.2

func (m *Mutex) TryLock() bool

TryLock locks m if it is free, and says whether it did.

func (*Mutex) Unlock added in v0.1.2

func (m *Mutex) Unlock()

Unlock unlocks m.

func (*Mutex) Watch added in v0.1.2

func (m *Mutex) Watch(ctx context.Context, name, dir string)

Watch watches m until ctx ends (Held.Watch).

type Rotating

type Rotating struct {
	// contains filtered or unexported fields
}

Rotating is a file that rolls over once it holds max bytes: path becomes path.1, path.1 becomes path.2, and so on; past keep older files, the oldest is removed. All of them hold at most (keep+1) × max bytes, a line longer than max aside. It closes the file before it renames it, as Windows requires, and needs no logrotate.

func OpenRotating

func OpenRotating(path string, size int64, keep int) (*Rotating, error)

OpenRotating opens path to append to it, rolled over at size bytes with keep older files.

func (*Rotating) Close

func (r *Rotating) Close() error

Close closes the current file.

func (*Rotating) Write

func (r *Rotating) Write(p []byte) (int, error)

Write writes p whole to the current file, after rolling it over when p would take it past max.

type Shutdown

type Shutdown struct {
	// Dir receives the dumps.
	Dir string
	// DumpAfter and ExitAfter count from Begin.
	DumpAfter, ExitAfter time.Duration
	// BeforeExit runs before a hung stop exits: a restart starts the newer djinn there. Nil does nothing.
	BeforeExit func()
	// Exit ends the process: os.Exit, unless a test replaces it.
	Exit func(code int)
	// contains filtered or unexported fields
}

Shutdown follows the stop of djinn up step by step in the log, and keeps it from hanging: past DumpAfter it dumps the goroutines and logs the steps still running; past ExitAfter it logs them again, dumps again, runs BeforeExit and exits with HungExit.

func NewShutdown

func NewShutdown(dir string) *Shutdown

NewShutdown is a Shutdown that dumps to dir after 10 s and exits after 30 s.

func (*Shutdown) Begin

func (s *Shutdown) Begin(why string)

Begin starts the stop, for why: the first call logs it and arms the watchdog; the others do nothing.

func (*Shutdown) End

func (s *Shutdown) End()

End logs that the stop ended, and disarms the watchdog.

func (*Shutdown) Running

func (s *Shutdown) Running() []string

Running names the steps still running, the oldest first, with how long each has run.

func (*Shutdown) Start

func (s *Shutdown) Start(name string) (end func())

Start logs that the step name begins, and begins the stop if it has not; the returned function logs that it ended.

func (*Shutdown) Step

func (s *Shutdown) Step(name string, f func())

Step runs f as the step name.

Jump to

Keyboard shortcuts

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