Implement facility based logger, debugging via REST API
This implements a new debug/trace infrastructure based on a slightly
hacked up logger. Instead of the traditional "if debug { ... }" I've
rewritten the logger to have no-op Debugln and Debugf, unless debugging
has been enabled for a given "facility". The "facility" is just a
string, typically a package name.
This will be slightly slower than before; but not that much as it's
mostly a function call that returns immediately. For the cases where it
matters (the Debugln takes a hex.Dump() of something for example, and
it's not in a very occasional "if err != nil" branch) there is an
l.ShouldDebug(facility) that is fast enough to be used like the old "if
debug".
The point of all this is that we can now toggle debugging for the
various packages on and off at runtime. There's a new method
/rest/system/debug that can be POSTed a set of facilities to enable and
disable debug for, or GET from to get a list of facilities with
descriptions and their current debug status.
Similarly a /rest/system/log?since=... can grab the latest log entries,
up to 250 of them (hardcoded constant in main.go) plus the initial few.
Not implemented in this commit (but planned) is a simple debug GUI
available on /debug that shows the current log in an easily pasteable
format and has checkboxes to enable the various debug facilities.
The debug instructions to a user then becomes "visit this URL, check
these boxes, reproduce your problem, copy and paste the log". The actual
log viewer on the hypothetical /debug URL can poll regularly for new log
entries and this bypass the 250 line limit.
The existing STTRACE=foo variable is still obeyed and just sets the
start state of the system.
This commit is contained in:
@@ -11,8 +11,12 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// This package uses stdlib sync as it may be used to debug syncthing/lib/sync
|
||||
// and that would cause an implosion of the universe.
|
||||
|
||||
type LogLevel int
|
||||
|
||||
const (
|
||||
@@ -46,15 +50,17 @@ type Logger interface {
|
||||
Fatalf(format string, vals ...interface{})
|
||||
ShouldDebug(facility string) bool
|
||||
SetDebug(facility string, enabled bool)
|
||||
Facilities() (enabled, disabled []string)
|
||||
NewFacility(facility string) Logger
|
||||
Facilities() map[string]string
|
||||
FacilityDebugging() []string
|
||||
NewFacility(facility, description string) Logger
|
||||
}
|
||||
|
||||
type logger struct {
|
||||
logger *log.Logger
|
||||
handlers [NumLevels][]MessageHandler
|
||||
debug map[string]bool
|
||||
mut sync.Mutex
|
||||
logger *log.Logger
|
||||
handlers [NumLevels][]MessageHandler
|
||||
facilities map[string]string // facility name => description
|
||||
debug map[string]bool // facility name => debugging enabled
|
||||
mut sync.Mutex
|
||||
}
|
||||
|
||||
// The default logger logs to standard output with a time prefix.
|
||||
@@ -226,24 +232,42 @@ func (l *logger) SetDebug(facility string, enabled bool) {
|
||||
l.mut.Unlock()
|
||||
}
|
||||
|
||||
// Facilities returns the currently known set of facilities, both those for
|
||||
// which debug is enabled and those for which it is disabled.
|
||||
func (l *logger) Facilities() (enabled, disabled []string) {
|
||||
// FacilityDebugging returns the set of facilities that have debugging
|
||||
// enabled.
|
||||
func (l *logger) FacilityDebugging() []string {
|
||||
var enabled []string
|
||||
l.mut.Lock()
|
||||
for facility, isEnabled := range l.debug {
|
||||
if isEnabled {
|
||||
enabled = append(enabled, facility)
|
||||
} else {
|
||||
disabled = append(disabled, facility)
|
||||
}
|
||||
}
|
||||
l.mut.Unlock()
|
||||
return
|
||||
return enabled
|
||||
}
|
||||
|
||||
// Facilities returns the currently known set of facilities and their
|
||||
// descriptions.
|
||||
func (l *logger) Facilities() map[string]string {
|
||||
l.mut.Lock()
|
||||
res := make(map[string]string, len(l.facilities))
|
||||
for facility, descr := range l.facilities {
|
||||
res[facility] = descr
|
||||
}
|
||||
l.mut.Unlock()
|
||||
return res
|
||||
}
|
||||
|
||||
// NewFacility returns a new logger bound to the named facility.
|
||||
func (l *logger) NewFacility(facility string) Logger {
|
||||
func (l *logger) NewFacility(facility, description string) Logger {
|
||||
l.mut.Lock()
|
||||
if l.facilities == nil {
|
||||
l.facilities = make(map[string]string)
|
||||
}
|
||||
if description != "" {
|
||||
l.facilities[facility] = description
|
||||
}
|
||||
|
||||
if l.debug == nil {
|
||||
l.debug = make(map[string]bool)
|
||||
}
|
||||
@@ -279,3 +303,77 @@ func (l *facilityLogger) Debugf(format string, vals ...interface{}) {
|
||||
}
|
||||
l.logger.Debugf(format, vals...)
|
||||
}
|
||||
|
||||
// A Recorder keeps a size limited record of log events.
|
||||
type Recorder struct {
|
||||
lines []Line
|
||||
initial int
|
||||
mut sync.Mutex
|
||||
}
|
||||
|
||||
// A Line represents a single log entry.
|
||||
type Line struct {
|
||||
When time.Time
|
||||
Message string
|
||||
}
|
||||
|
||||
func NewRecorder(l Logger, level LogLevel, size, initial int) *Recorder {
|
||||
r := &Recorder{
|
||||
lines: make([]Line, 0, size),
|
||||
initial: initial,
|
||||
}
|
||||
l.AddHandler(level, r.append)
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *Recorder) Since(t time.Time) []Line {
|
||||
r.mut.Lock()
|
||||
defer r.mut.Unlock()
|
||||
|
||||
res := r.lines
|
||||
for i := 0; i < len(res) && res[i].When.Before(t); i++ {
|
||||
// nothing, just incrementing i
|
||||
}
|
||||
if len(res) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// We must copy the result as r.lines can be mutated as soon as the lock
|
||||
// is released.
|
||||
cp := make([]Line, len(res))
|
||||
copy(cp, res)
|
||||
return cp
|
||||
}
|
||||
|
||||
func (r *Recorder) Clear() {
|
||||
r.mut.Lock()
|
||||
r.lines = r.lines[:0]
|
||||
r.mut.Unlock()
|
||||
}
|
||||
|
||||
func (r *Recorder) append(l LogLevel, msg string) {
|
||||
line := Line{
|
||||
When: time.Now(),
|
||||
Message: msg,
|
||||
}
|
||||
|
||||
r.mut.Lock()
|
||||
defer r.mut.Unlock()
|
||||
|
||||
if len(r.lines) == cap(r.lines) {
|
||||
if r.initial > 0 {
|
||||
// Shift all lines one step to the left, keeping the "initial" first intact.
|
||||
copy(r.lines[r.initial+1:], r.lines[r.initial+2:])
|
||||
} else {
|
||||
copy(r.lines, r.lines[1:])
|
||||
}
|
||||
// Add the new one at the end
|
||||
r.lines[len(r.lines)-1] = line
|
||||
return
|
||||
}
|
||||
|
||||
r.lines = append(r.lines, line)
|
||||
if len(r.lines) == r.initial {
|
||||
r.lines = append(r.lines, Line{time.Now(), "..."})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user