mirror of
https://github.com/octoleo/syncthing.git
synced 2024-12-23 11:28:59 +00:00
76af9ba53d
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.
84 lines
2.0 KiB
Go
84 lines
2.0 KiB
Go
// Copyright (C) 2015 The Syncthing Authors.
|
|
//
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
package versioner
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/syncthing/syncthing/lib/osutil"
|
|
)
|
|
|
|
func init() {
|
|
// Register the constructor for this type of versioner with the name "external"
|
|
Factories["external"] = NewExternal
|
|
}
|
|
|
|
type External struct {
|
|
command string
|
|
folderPath string
|
|
}
|
|
|
|
func NewExternal(folderID, folderPath string, params map[string]string) Versioner {
|
|
command := params["command"]
|
|
|
|
s := External{
|
|
command: command,
|
|
folderPath: folderPath,
|
|
}
|
|
|
|
l.Debugf("instantiated %#v", s)
|
|
return s
|
|
}
|
|
|
|
// Archive moves the named file away to a version archive. If this function
|
|
// returns nil, the named file does not exist any more (has been archived).
|
|
func (v External) Archive(filePath string) error {
|
|
_, err := osutil.Lstat(filePath)
|
|
if os.IsNotExist(err) {
|
|
l.Debugln("not archiving nonexistent file", filePath)
|
|
return nil
|
|
} else if err != nil {
|
|
return err
|
|
}
|
|
|
|
l.Debugln("archiving", filePath)
|
|
|
|
inFolderPath, err := filepath.Rel(v.folderPath, filePath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if v.command == "" {
|
|
return errors.New("Versioner: command is empty, please enter a valid command")
|
|
}
|
|
|
|
cmd := exec.Command(v.command, v.folderPath, inFolderPath)
|
|
env := os.Environ()
|
|
// filter STGUIAUTH and STGUIAPIKEY from environment variables
|
|
filteredEnv := []string{}
|
|
for _, x := range env {
|
|
if !strings.HasPrefix(x, "STGUIAUTH=") && !strings.HasPrefix(x, "STGUIAPIKEY=") {
|
|
filteredEnv = append(filteredEnv, x)
|
|
}
|
|
}
|
|
cmd.Env = filteredEnv
|
|
err = cmd.Run()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// return error if the file was not removed
|
|
if _, err = osutil.Lstat(filePath); os.IsNotExist(err) {
|
|
return nil
|
|
}
|
|
return errors.New("Versioner: file was not removed by external script")
|
|
}
|