Added -logfile option.
[src/xds/xds-server.git] / main.go
diff --git a/main.go b/main.go
index 36586cf..060a927 100644 (file)
--- a/main.go
+++ b/main.go
@@ -7,6 +7,7 @@ import (
        "os"
        "os/exec"
        "os/signal"
+       "path/filepath"
        "strings"
        "syscall"
        "time"
@@ -115,8 +116,25 @@ func xdsApp(cliCtx *cli.Context) error {
        }
        ctx.Config = cfg
 
-       // TODO allow to redirect stdout/sterr into logs file
-       //logFilename := filepath.Join(ctx.Config.FileConf.LogsDir + "xds-server.log")
+       // Logs redirected into a file when logsDir is set
+       logfilename := cliCtx.GlobalString("logfile")
+       if ctx.Config.FileConf.LogsDir != "" && logfilename != "stdout" {
+               if logfilename == "" {
+                       logfilename = "xds-server.log"
+               }
+               // is it an absolute path ?
+               logFile := logfilename
+               if logfilename[0] == '.' || logfilename[0] != '/' {
+                       logFile = filepath.Join(ctx.Config.FileConf.LogsDir, logfilename)
+               }
+               fmt.Printf("Logging file: %s\n", logFile)
+               fdL, err := os.OpenFile(logFile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
+               if err != nil {
+                       msgErr := fmt.Sprintf("Cannot create log file %s", logFile)
+                       return cli.NewExitError(msgErr, int(syscall.EPERM))
+               }
+               ctx.Log.Out = fdL
+       }
 
        // FIXME - add a builder interface and support other builder type (eg. native)
        builderType := "syncthing"
@@ -132,24 +150,26 @@ func xdsApp(cliCtx *cli.Context) error {
                if err != nil {
                        return cli.NewExitError(err, 2)
                }
-               ctx.Log.Infof("Syncthing started (PID %d)", ctx.SThgCmd.Process.Pid)
+               fmt.Printf("Syncthing started (PID %d)\n", ctx.SThgCmd.Process.Pid)
 
                ctx.Log.Infof("Starting Syncthing-inotify...")
                ctx.SThgInotCmd, err = ctx.SThg.StartInotify()
                if err != nil {
                        return cli.NewExitError(err, 2)
                }
-               ctx.Log.Infof("Syncthing-inotify started (PID %d)", ctx.SThgInotCmd.Process.Pid)
+               fmt.Printf("Syncthing-inotify started (PID %d)\n", ctx.SThgInotCmd.Process.Pid)
 
                // Establish connection with local Syncthing (retry if connection fail)
+               fmt.Printf("Establishing connection with Syncthing...\n")
                time.Sleep(2 * time.Second)
-               retry := 10
+               maxRetry := 30
+               retry := maxRetry
                err = nil
                for retry > 0 {
                        if err = ctx.SThg.Connect(); err == nil {
                                break
                        }
-                       ctx.Log.Warningf("Establishing connection to Syncthing (retry %d/10)", retry)
+                       ctx.Log.Warningf("Establishing connection to Syncthing (retry %d/%d)", retry, maxRetry)
                        time.Sleep(time.Second)
                        retry--
                }
@@ -168,16 +188,25 @@ func xdsApp(cliCtx *cli.Context) error {
                }
 
                // Retrieve initial Syncthing config
+
+               // FIXME: cannot retrieve default SDK, need to save on disk or somewhere
+               // else all config to be able to restore it.
+               defaultSdk := ""
                stCfg, err := ctx.SThg.ConfigGet()
                if err != nil {
                        return cli.NewExitError(err, 2)
                }
                for _, stFld := range stCfg.Folders {
-                       relativePath := strings.TrimPrefix(stFld.RawPath, ctx.Config.ShareRootDir)
+                       relativePath := strings.TrimPrefix(stFld.RawPath, ctx.Config.FileConf.ShareRootDir)
                        if relativePath == "" {
                                relativePath = stFld.RawPath
                        }
-                       newFld := xdsconfig.NewFolderConfig(stFld.ID, stFld.Label, ctx.Config.ShareRootDir, strings.Trim(relativePath, "/"))
+
+                       newFld := xdsconfig.NewFolderConfig(stFld.ID,
+                               stFld.Label,
+                               ctx.Config.FileConf.ShareRootDir,
+                               strings.TrimRight(relativePath, "/"),
+                               defaultSdk)
                        ctx.Config.Folders = ctx.Config.Folders.Update(xdsconfig.FoldersConfig{newFld})
                }
 
@@ -236,6 +265,12 @@ func main() {
                        Usage:  "logging level (supported levels: panic, fatal, error, warn, info, debug)\n\t",
                        EnvVar: "LOG_LEVEL",
                },
+               cli.StringFlag{
+                       Name:   "logfile",
+                       Value:  "stdout",
+                       Usage:  "filename where logs will be redirected (default stdout)\n\t",
+                       EnvVar: "LOG_FILENAME",
+               },
        }
 
        // only one action: Web Server