From: Urban Wallasch Date: Mon, 8 Apr 2019 20:51:43 +0000 (+0200) Subject: * Improved tracing and logging, so check and checkFatal show the correct source location. X-Git-Url: https://git.packet-gain.de/?a=commitdiff_plain;h=9f2b1371cd378073bd12a9dd674777f3cc5bf889;p=gogopherd.git * Improved tracing and logging, so check and checkFatal show the correct source location. --- diff --git a/config.go b/config.go index 1ce1ab9..969dfaf 100644 --- a/config.go +++ b/config.go @@ -145,7 +145,7 @@ func initialize() { cfg.idxpage = "" } - tracer.Print("Version: ", version) + tracer.Print("final config after evaluating command line:") tracer.Print("fqdn: ", cfg.fqdn) tracer.Print("iface: ", cfg.iface) tracer.Print("port: ", cfg.port) @@ -158,6 +158,7 @@ func initialize() { tracer.Print("indexes: ", cfg.indexes) tracer.Print("timeout: ", cfg.timeout) tracer.Print("verbose: ", cfg.verbose) + tracer.Print("gogopherd version ", version) } /* EOF */ diff --git a/util.go b/util.go index 84be4f8..b31686f 100644 --- a/util.go +++ b/util.go @@ -17,34 +17,42 @@ import ( "net/http" "os" "path/filepath" + "runtime" "strings" "time" ) var ( logger *log.Logger + loggex *log.Logger tracer *log.Logger + tracex *log.Logger ) func initUtil(verbose bool) { logger = log.New(os.Stderr, "", log.Ldate|log.Ltime|log.Lshortfile) + loggex = log.New(os.Stderr, "", log.Ldate|log.Ltime) if verbose { tracer = log.New(os.Stderr, "", log.Ldate|log.Ltime|log.Lshortfile) + tracex = log.New(os.Stderr, "", log.Ldate|log.Ltime) } else { tracer = log.New(ioutil.Discard, "", 0) + tracex = log.New(ioutil.Discard, "", 0) } } func checkFatal(err error, msg string) { if err != nil { - logger.Print(msg, ": ", err.Error()) + _, file, line, _ := runtime.Caller(1) + loggex.Printf("%s:%d: %s %v\n", filepath.Base(file), line, msg, err) os.Exit(1) } } func check(err error, msg string) error { - if err != nil { - tracer.Print(msg, ": ", err.Error()) + if err != nil && cfg.verbose { + _, file, line, _ := runtime.Caller(1) + tracex.Printf("%s:%d: %s %v\n", filepath.Base(file), line, msg, err) } return err }