From a50baa7c309f7eb55fe87c71f4c688ace325b6ac Mon Sep 17 00:00:00 2001 From: Sebastien Douheret Date: Wed, 17 May 2017 17:10:45 +0200 Subject: Add logsDir setting and more - add logsDir setting in config.json - redirect Syncthing and Syncthing-inotify into log files - Use autogenerated Syncthing apikey if gui-apikey not set in config.json --- lib/common/filepath.go | 15 +++++++++++++++ lib/common/httpclient.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 lib/common/filepath.go (limited to 'lib/common') diff --git a/lib/common/filepath.go b/lib/common/filepath.go new file mode 100644 index 0000000..603c2a2 --- /dev/null +++ b/lib/common/filepath.go @@ -0,0 +1,15 @@ +package common + +import "os" + +// Exists returns whether the given file or directory exists or not +func Exists(path string) bool { + _, err := os.Stat(path) + if err == nil { + return true + } + if os.IsNotExist(err) { + return false + } + return true +} diff --git a/lib/common/httpclient.go b/lib/common/httpclient.go index 40d7bc2..72132bf 100644 --- a/lib/common/httpclient.go +++ b/lib/common/httpclient.go @@ -9,6 +9,8 @@ import ( "io/ioutil" "net/http" "strings" + + "github.com/Sirupsen/logrus" ) type HTTPClient struct { @@ -20,6 +22,7 @@ type HTTPClient struct { id string csrf string conf HTTPClientConfig + logger *logrus.Logger } type HTTPClientConfig struct { @@ -30,6 +33,13 @@ type HTTPClientConfig struct { CsrfDisable bool } +const ( + logError = 1 + logWarning = 2 + logInfo = 3 + logDebug = 4 +) + // Inspired by syncthing/cmd/cli const insecure = false @@ -64,6 +74,30 @@ func HTTPNewClient(baseURL string, cfg HTTPClientConfig) (*HTTPClient, error) { return &client, nil } +// SetLogger Define the logger to use +func (c *HTTPClient) SetLogger(log *logrus.Logger) { + c.logger = log +} + +func (c *HTTPClient) log(level int, format string, args ...interface{}) { + if c.logger != nil { + switch level { + case logError: + c.logger.Errorf(format, args...) + break + case logWarning: + c.logger.Warningf(format, args...) + break + case logInfo: + c.logger.Infof(format, args...) + break + default: + c.logger.Debugf(format, args...) + break + } + } +} + // Send request to retrieve Client id and/or CSRF token func (c *HTTPClient) getCidAndCsrf() error { request, err := http.NewRequest("GET", c.endpoint, nil) @@ -171,6 +205,8 @@ func (c *HTTPClient) handleRequest(request *http.Request) (*http.Response, error request.Header.Set("X-CSRF-Token-"+c.id[:5], c.csrf) } + c.log(logDebug, "HTTP %s %v", request.Method, request.URL) + response, err := c.httpClient.Do(request) if err != nil { return nil, err -- cgit 1.2.3-korg