From 79dd6918f2e2defd0a4cd8576fec2f3cd88e879f Mon Sep 17 00:00:00 2001 From: Kudalufi Date: Tue, 3 Jan 2017 08:54:28 +0000 Subject: [PATCH] cmd/syncthing: Add support for -auditfile= (fixes #3859) Adds support for -auditfile= where is "-" for stdout, "--" for stderr, or a filename. It can be left blank (or left out entirely) for the original behaviour of creating a timestamped filename. GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/3860 --- cmd/syncthing/main.go | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/cmd/syncthing/main.go b/cmd/syncthing/main.go index d3d378814..f92329a1e 100644 --- a/cmd/syncthing/main.go +++ b/cmd/syncthing/main.go @@ -12,6 +12,7 @@ import ( "errors" "flag" "fmt" + "io" "io/ioutil" "log" "net" @@ -211,6 +212,7 @@ type RuntimeOptions struct { hideConsole bool logFile string auditEnabled bool + auditFile string verbose bool paused bool unpaused bool @@ -273,6 +275,7 @@ func parseCommandLineOptions() RuntimeOptions { flag.BoolVar(&options.paused, "paused", false, "Start with all devices and folders paused") flag.BoolVar(&options.unpaused, "unpaused", false, "Start with all devices and folders unpaused") flag.StringVar(&options.logFile, "logfile", options.logFile, "Log file name (use \"-\" for stdout)") + flag.StringVar(&options.auditFile, "auditfile", options.auditFile, "Specify audit file (use \"-\" for stdout, \"--\" for stderr)") if runtime.GOOS == "windows" { // Allow user to hide the console window flag.BoolVar(&options.hideConsole, "no-console", false, "Hide console window") @@ -545,7 +548,7 @@ func syncthingMain(runtimeOptions RuntimeOptions) { l.SetPrefix("[start] ") if runtimeOptions.auditEnabled { - startAuditing(mainService) + startAuditing(mainService, runtimeOptions.auditFile) } if runtimeOptions.verbose { @@ -928,11 +931,31 @@ func copyFile(src, dst string) error { return nil } -func startAuditing(mainService *suture.Supervisor) { - auditFile := timestampedLoc(locAuditLog) - fd, err := os.OpenFile(auditFile, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600) - if err != nil { - l.Fatalln("Audit:", err) +func startAuditing(mainService *suture.Supervisor, auditFile string) { + + var fd io.Writer + var err error + var auditDest string + var auditFlags int + + if auditFile == "-" { + fd = os.Stdout + auditDest = "stdout" + } else if auditFile == "--" { + fd = os.Stderr + auditDest = "stderr" + } else { + if auditFile == "" { + auditFile = timestampedLoc(locAuditLog) + auditFlags = os.O_WRONLY | os.O_CREATE | os.O_EXCL + } else { + auditFlags = os.O_WRONLY | os.O_CREATE | os.O_APPEND + } + fd, err = os.OpenFile(auditFile, auditFlags, 0600) + if err != nil { + l.Fatalln("Audit:", err) + } + auditDest = auditFile } auditService := newAuditService(fd) @@ -942,7 +965,7 @@ func startAuditing(mainService *suture.Supervisor) { // ensure we capture all events from the start. auditService.WaitForStart() - l.Infoln("Audit log in", auditFile) + l.Infoln("Audit log in", auditDest) } func setupGUI(mainService *suture.Supervisor, cfg *config.Wrapper, m *model.Model, apiSub events.BufferedSubscription, diskSub events.BufferedSubscription, discoverer discover.CachingMux, connectionsService *connections.Service, errors, systemLog logger.Recorder, runtimeOptions RuntimeOptions) {