Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request (with patch): redirect events audit file to stderr #3859

Closed
VA1DER opened this issue Jan 2, 2017 · 1 comment
Closed

Feature request (with patch): redirect events audit file to stderr #3859

VA1DER opened this issue Jan 2, 2017 · 1 comment
Labels
frozen-due-to-age Issues closed and untouched for a long time, together with being locked for discussion

Comments

@VA1DER
Copy link

VA1DER commented Jan 2, 2017

As per discussion in the forum, I am submitting a patch to add the ability to direct the events audit file to stderr.

I chose not to use "-audit=stderr" as discussed in the forum since -audit is used currently as a boolean and this would have broken any implementation that used -audit by itself rather than -audit=true. Instead, I added a new command line switch "-auditfile" and gave the user the ability to select stdout ("-"), stderr ("--"), an arbitrary file, or to have the original behaviour of a file with a timestamped filename.

Attached below the diff is a zip file with the diff in it.

diff -u5r /home/Kurt/projects/syncthing/src/github.com/syncthing/syncthing/cmd/syncthing/main.go syncthing/main.go
--- /home/Kurt/projects/syncthing/src/github.com/syncthing/syncthing/cmd/syncthing/main.go	2017-01-02 02:05:09.188656600 -0400
+++ syncthing/main.go	2017-01-02 02:25:59.330766400 -0400
@@ -27,10 +27,11 @@
 	"sort"
 	"strconv"
 	"strings"
 	"syscall"
 	"time"
+	"io"
 
 	"github.com/syncthing/syncthing/lib/config"
 	"github.com/syncthing/syncthing/lib/connections"
 	"github.com/syncthing/syncthing/lib/db"
 	"github.com/syncthing/syncthing/lib/dialer"
@@ -209,10 +210,11 @@
 	noBrowser      bool
 	browserOnly    bool
 	hideConsole    bool
 	logFile        string
 	auditEnabled   bool
+	auditFile      string
 	verbose        bool
 	paused         bool
 	unpaused       bool
 	guiAddress     string
 	guiAPIKey      string
@@ -271,10 +273,11 @@
 	flag.BoolVar(&options.auditEnabled, "audit", false, "Write events to audit file")
 	flag.BoolVar(&options.verbose, "verbose", false, "Print verbose log output")
 	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")
 	}
 
@@ -543,11 +546,11 @@
 	// Set a log prefix similar to the ID we will have later on, or early log
 	// lines look ugly.
 	l.SetPrefix("[start] ")
 
 	if runtimeOptions.auditEnabled {
-		startAuditing(mainService)
+		startAuditing(mainService, runtimeOptions.auditFile)
 	}
 
 	if runtimeOptions.verbose {
 		mainService.Add(newVerboseService())
 	}
@@ -929,25 +932,41 @@
 	}
 
 	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
+
+	if auditFile == "-" {
+		fd = os.Stdout
+		auditDest = "stdout"
+	} else if auditFile == "--" {
+		fd = os.Stderr
+		auditDest = "stderr"
+	} else {
+		if auditFile == "" {
+			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)
+		}
+		auditDest = auditFile
 	}
 
 	auditService := newAuditService(fd)
 	mainService.Add(auditService)
 
 	// We wait for the audit service to fully start before we return, to
 	// 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) {
 	guiCfg := cfg.GUI()
 

syncthing_audit_specify.zip

@calmh
Copy link
Member

calmh commented Jan 2, 2017

Hi! Please open a pull request with the changes.

@st-review st-review added the frozen-due-to-age Issues closed and untouched for a long time, together with being locked for discussion label Jan 4, 2018
@syncthing syncthing locked and limited conversation to collaborators Jan 4, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
frozen-due-to-age Issues closed and untouched for a long time, together with being locked for discussion
Projects
None yet
Development

No branches or pull requests

3 participants