diff options
author | Sebastien Douheret <sebastien.douheret@iot.bzh> | 2017-08-28 16:00:22 +0200 |
---|---|---|
committer | Sebastien Douheret <sebastien.douheret@iot.bzh> | 2017-08-28 16:00:22 +0200 |
commit | 813a78d13a4773b6a74a4f0090bb3621a355bf1d (patch) | |
tree | 96be5c6867fa7a5a21e67efdd7eb33078abe4b83 /main.go | |
parent | ebb04d162f47f4324672813b3eb0ce3137c3e80f (diff) |
Fixed early log prints
Signed-off-by: Sebastien Douheret <sebastien.douheret@iot.bzh>
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 55 |
1 files changed, 32 insertions, 23 deletions
@@ -39,7 +39,7 @@ var AppSubVersion = "unknown-dev" // Create logger var log = logrus.New() -var earlyLog = []string{} +var logFileInitial = "/tmp/xds-gdb.log" // Application details const ( @@ -64,6 +64,8 @@ type EnvVar struct { func exitError(code syscall.Errno, f string, a ...interface{}) { err := fmt.Sprintf(f, a...) fmt.Fprintf(os.Stderr, err+"\n") + log.Debugf("Exit: code=%v, err=%s", code, err) + os.Exit(int(code)) } @@ -73,6 +75,18 @@ func main() { var listProject bool var err error + // Init Logger and set temporary file and level for the 1st part + // IOW while XDS_LOGLEVEL and XDS_LOGFILE options are not parsed + logFile = logFileInitial + fdL, err := os.OpenFile(logFileInitial, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666) + if err != nil { + msgErr := fmt.Sprintf("Cannot create log file %s", logFileInitial) + exitError(syscall.EPERM, msgErr) + } + log.Formatter = &logrus.TextFormatter{} + log.Out = fdL + log.Level = logrus.DebugLevel + uri = "localhost:8000" logLevel = defaultLogLevel @@ -140,6 +154,7 @@ func main() { } // Process gdb arguments + log.Debugf("xds-gdb started with args: %v", os.Args) args := make([]string, len(os.Args)) args[0] = os.Args[0] gdbArgs := make([]string, len(os.Args)) @@ -229,23 +244,24 @@ endloop: var err error curDir, _ := os.Getwd() - // Set logger level, formatter and log file + // Now set logger level and log file to correct/env var settings if log.Level, err = logrus.ParseLevel(logLevel); err != nil { msg := fmt.Sprintf("Invalid log level : \"%v\"\n", logLevel) return cli.NewExitError(msg, int(syscall.EINVAL)) } - log.Formatter = &logrus.TextFormatter{} + log.Infof("Switch log level to %s", logLevel) - // Always log into a file - if logFile == "" { - logFile = "/tmp/xds-gdb.log" - } - 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)) + if logFile != logFileInitial { + log.Infof("Switch logging to log file %s", 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)) + } + defer fdL.Close() + log.Out = fdL } - log.Out = fdL // Build env variables env := []string{} @@ -266,11 +282,6 @@ endloop: gdb.SetConfig("listProject", listProject) } - // Log early print - for _, msg := range earlyLog { - log.Debugf(msg) - } - // Log useful info log.Infof("Original arguments: %v", os.Args) log.Infof("Current directory : %v", curDir) @@ -471,7 +482,7 @@ func loadConfigEnvFile(confFile, gdbCmdFile string) (map[string]string, string, // 1- if no confFile set, use setting from gdb command file is option // --command/-x is set if confFile == "" && gdbCmdFile != "" { - logEarly("Try extract config from gdbCmdFile: %s", gdbCmdFile) + log.Infof("Try extract config from gdbCmdFile: %s", gdbCmdFile) confFile, err = extractEnvFromCmdFile(gdbCmdFile) if confFile != "" { defer os.Remove(confFile) @@ -492,7 +503,7 @@ func loadConfigEnvFile(confFile, gdbCmdFile string) (map[string]string, string, path.Join(u.HomeDir, ".xds"), } { confFile = path.Join(d, xdsEnvFile) - logEarly("Search config in %s", confFile) + log.Infof("Search config in %s", confFile) if common.Exists(confFile) { break } @@ -501,9 +512,11 @@ func loadConfigEnvFile(confFile, gdbCmdFile string) (map[string]string, string, } if confFile == "" { + log.Infof("NO valid conf file found!") return envMap, "", nil } + log.Infof("Use conf file: %s", confFile) if !common.Exists(confFile) { return envMap, confFile, fmt.Errorf("Error no env config file not found") } @@ -576,7 +589,3 @@ func extractEnvFromCmdFile(cmdFile string) (string, error) { return envFileName, nil } - -func logEarly(format string, a ...interface{}) { - earlyLog = append(earlyLog, fmt.Sprintf(format, a...)) -} |