Add -logfile flag, Windows only

This commit is contained in:
Jakob Borg 2014-10-19 14:57:03 +02:00
parent 99df4d660b
commit 5d0183a9ed
2 changed files with 32 additions and 9 deletions

View File

@ -178,6 +178,7 @@ var (
doUpgradeCheck bool doUpgradeCheck bool
noBrowser bool noBrowser bool
generateDir string generateDir string
logFile string
noRestart = os.Getenv("STNORESTART") != "" noRestart = os.Getenv("STNORESTART") != ""
guiAddress = os.Getenv("STGUIADDRESS") // legacy guiAddress = os.Getenv("STGUIADDRESS") // legacy
guiAuthentication = os.Getenv("STGUIAUTH") // legacy guiAuthentication = os.Getenv("STGUIAUTH") // legacy
@ -194,6 +195,15 @@ func main() {
if err != nil { if err != nil {
l.Fatalln("home:", err) l.Fatalln("home:", err)
} }
if runtime.GOOS == "windows" {
// On Windows, we use a log file by default. Setting the -logfile flag
// to the empty string disables this behavior.
logFile = filepath.Join(defConfDir, "syncthing.log")
flag.StringVar(&logFile, "logfile", logFile, "Log file name (blank for stdout)")
}
flag.StringVar(&generateDir, "generate", "", "Generate key and config in specified dir, then exit") flag.StringVar(&generateDir, "generate", "", "Generate key and config in specified dir, then exit")
flag.StringVar(&guiAddress, "gui-address", guiAddress, "Override GUI address") flag.StringVar(&guiAddress, "gui-address", guiAddress, "Override GUI address")
flag.StringVar(&guiAuthentication, "gui-authentication", guiAuthentication, "Override GUI authentication; username:password") flag.StringVar(&guiAuthentication, "gui-authentication", guiAuthentication, "Override GUI authentication; username:password")

View File

@ -44,6 +44,19 @@ func monitorMain() {
os.Setenv("STMONITORED", "yes") os.Setenv("STMONITORED", "yes")
l.SetPrefix("[monitor] ") l.SetPrefix("[monitor] ")
var err error
var dst io.Writer
if logFile == "" {
dst = os.Stdout
} else {
dst, err = os.Create(logFile)
if err != nil {
l.Fatalln("log file:", err)
}
l.Infof(`Log output directed to file "%s"`, logFile)
}
args := os.Args args := os.Args
var restarts [countRestarts]time.Time var restarts [countRestarts]time.Time
@ -64,12 +77,12 @@ func monitorMain() {
stderr, err := cmd.StderrPipe() stderr, err := cmd.StderrPipe()
if err != nil { if err != nil {
l.Fatalln(err) l.Fatalln("stderr:", err)
} }
stdout, err := cmd.StdoutPipe() stdout, err := cmd.StdoutPipe()
if err != nil { if err != nil {
l.Fatalln(err) l.Fatalln("stdout:", err)
} }
l.Infoln("Starting syncthing") l.Infoln("Starting syncthing")
@ -87,8 +100,8 @@ func monitorMain() {
stdoutLastLines = make([]string, 0, 50) stdoutLastLines = make([]string, 0, 50)
stdoutMut.Unlock() stdoutMut.Unlock()
go copyStderr(stderr) go copyStderr(stderr, dst)
go copyStdout(stdout) go copyStdout(stdout, dst)
exit := make(chan error) exit := make(chan error)
@ -130,7 +143,7 @@ func monitorMain() {
} }
} }
func copyStderr(stderr io.ReadCloser) { func copyStderr(stderr io.ReadCloser, dst io.Writer) {
br := bufio.NewReader(stderr) br := bufio.NewReader(stderr)
var panicFd *os.File var panicFd *os.File
@ -141,7 +154,7 @@ func copyStderr(stderr io.ReadCloser) {
} }
if panicFd == nil { if panicFd == nil {
os.Stderr.WriteString(line) dst.Write([]byte(line))
if strings.HasPrefix(line, "panic:") || strings.HasPrefix(line, "fatal error:") { if strings.HasPrefix(line, "panic:") || strings.HasPrefix(line, "fatal error:") {
panicFd, err = os.Create(filepath.Join(confDir, time.Now().Format("panic-20060102-150405.log"))) panicFd, err = os.Create(filepath.Join(confDir, time.Now().Format("panic-20060102-150405.log")))
@ -173,8 +186,8 @@ func copyStderr(stderr io.ReadCloser) {
} }
} }
func copyStdout(stderr io.ReadCloser) { func copyStdout(stdout io.ReadCloser, dst io.Writer) {
br := bufio.NewReader(stderr) br := bufio.NewReader(stdout)
for { for {
line, err := br.ReadString('\n') line, err := br.ReadString('\n')
if err != nil { if err != nil {
@ -192,6 +205,6 @@ func copyStdout(stderr io.ReadCloser) {
} }
stdoutMut.Unlock() stdoutMut.Unlock()
os.Stdout.WriteString(line) dst.Write([]byte(line))
} }
} }