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
- func Argv(args []string) string
- func Crash(where string, args ...any)
- func Crashes(dir string) []string
- func Cut(s string, n int) string
- func Dump(dir, why string) (string, error)
- func Dumps(dir string) []string
- func Folder(home string) string
- func L() *slog.Logger
- func Lock(mu *sync.Mutex, name string)
- func New(w io.Writer, level slog.Leveler) *slog.Logger
- func Panicked(where string, p any, args ...any)
- func ParseLevel(s string) (slog.Level, error)
- func Ran(ctx context.Context, cmd *exec.Cmd, start time.Time, err error)
- func Shift(path string, keep int) error
- func Start(home string) (stop func() error, err error)
- func Took(level slog.Level, msg string, start time.Time, args ...any)
- func Use(l *slog.Logger) (restore func())
- type Held
- type Mutex
- type Rotating
- type Shutdown
Constants ¶
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" )
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 )
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 )
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 )
const HungExit = 3
HungExit is the exit code of a djinn up whose stop outlived ExitAfter.
Variables ¶
This section is empty.
Functions ¶
func Argv ¶
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
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 Dump ¶
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 Lock ¶
Lock locks mu, and logs at warn a wait past Slow for the lock name: who holds it held up the others.
func Panicked ¶ added in v0.1.2
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 ¶
ParseLevel reads debug, info, warn or error; empty is info.
func Ran ¶
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 ¶
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 ¶
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.
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
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.
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 ¶
OpenRotating opens path to append to it, rolled over at size bytes with keep older files.
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 ¶
NewShutdown is a Shutdown that dumps to dir after 10 s and exits after 30 s.
func (*Shutdown) Begin ¶
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 ¶
Running names the steps still running, the oldest first, with how long each has run.