X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?p=src%2Fxds%2Fxds-cli.git;a=blobdiff_plain;f=main.go;h=f9b32f38c8fabeba0a177de64e28dfc1da725c37;hp=668738c34cb58f79873b0207b0e746369a0c5d7f;hb=84273d6128b7c3c9d31c2f3a1453107a78e949a9;hpb=ad7380c0eeaa8165067a43734d925a1ccc9187e1 diff --git a/main.go b/main.go index 668738c..f9b32f3 100644 --- a/main.go +++ b/main.go @@ -177,6 +177,12 @@ func main() { Usage: "logging level (supported levels: panic, fatal, error, warn, info, debug)", Value: defaultLogLevel, }, + cli.StringFlag{ + Name: "logfile", + Value: "stderr", + Usage: "filename where logs will be redirected (default stderr)\n\t", + EnvVar: "XDS_LOGFILENAME", + }, cli.StringFlag{ Name: "url, u", EnvVar: "XDS_AGENT_URL", @@ -206,8 +212,8 @@ func main() { // Add --config option to all commands to support --config option either before or after command verb // IOW support following both syntaxes: - // xds-cli exec --config myprj.conf ... - // xds-cli --config myprj.conf exec ... + // xds-cli exec --config myPrj.conf ... + // xds-cli --config myPrj.conf exec ... for i, cmd := range app.Commands { if len(cmd.Flags) > 0 { app.Commands[i].Flags = append(cmd.Flags, cli.StringFlag{Hidden: true, Name: "config, c"}) @@ -277,6 +283,17 @@ func main() { } Log.Formatter = &logrus.TextFormatter{} + if ctx.String("logfile") != "stderr" { + logFile, _ := common.ResolveEnvVar(ctx.String("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, 1) + } + Log.Infof("Logging to file: %s", logFile) + Log.Out = fdL + } + Log.Infof("%s version: %s", AppName, app.Version) earlyDisplay() Log.Debugf("\nEnvironment: %v\n", os.Environ())