From ccc33f61c32d87b4afe4b1741c6699ca602ce401 Mon Sep 17 00:00:00 2001 From: Kleissner Date: Tue, 6 Jul 2021 19:09:46 +0200 Subject: [PATCH] Allow to change log error output via command and setting. --- Command Line.go | 26 +++++++++++++++++++++++++- Main.go | 12 ++++++++++-- go.mod | 2 +- go.sum | 4 ++-- 4 files changed, 38 insertions(+), 6 deletions(-) diff --git a/Command Line.go b/Command Line.go index 760bcd9..9f6893f 100644 --- a/Command Line.go +++ b/Command Line.go @@ -93,13 +93,14 @@ func showHelp() { "warehouse store Store data into local warehouse\n" + "dht get Get data via DHT by hash\n" + "dht store Store data into DHT\n" + + "log error Set error log output\n" + "\n") } func userCommands() { reader := bufio.NewReader(os.Stdin) - fmt.Print("Peernet Cmd " + core.Version + "\n------------------------------\n") + fmt.Print(appName + " " + core.Version + "\n------------------------------\n") showHelp() for { @@ -284,6 +285,14 @@ func userCommands() { fmt.Printf("Invalid hash. Hex-encoded blake3 hash as input is required.\n") } + case "log error": + fmt.Printf("Please choose the target output of error messages:\n0 = Log file (default)\n1 = Command line\n2 = Log file + command line\n3 = None\n") + if number, valid := getUserOptionInt(reader); !valid || number < 0 || number > 3 { + fmt.Printf("Invalid option.") + } else { + config.ErrorOutput = number + } + } } } @@ -452,3 +461,18 @@ func GetPeerlistSorted() (peers []*core.PeerInfo) { return peers } + +// logError handles error messages from core +func logError(function, format string, v ...interface{}) { + switch config.ErrorOutput { + case 0: + core.DefaultLogError(function, format, v) + + case 1: + fmt.Printf("["+function+"] "+format, v...) + + case 2: + core.DefaultLogError(function, format, v) + fmt.Printf("["+function+"] "+format, v...) + } +} diff --git a/Main.go b/Main.go index 666e456..96bba14 100644 --- a/Main.go +++ b/Main.go @@ -14,9 +14,15 @@ import ( ) const configFile = "Config.yaml" +const appName = "Peernet Cmd" + +var config struct { + // Log settings + ErrorOutput int `yaml:"ErrorOutput"` // 0 = Log file (default), 1 = Command line, 2 = Log file + command line, 3 = None +} func init() { - if status, err := core.LoadConfig(configFile); err != nil { + if status, err := core.LoadConfigOut(configFile, &config); err != nil { switch status { case 0: fmt.Printf("Unknown error accessing config file '%s': %s", configFile, err.Error()) @@ -35,8 +41,10 @@ func init() { os.Exit(1) } + core.UserAgent = appName + "/" + core.Version + core.Filters.LogError = logError + core.Init() - core.UserAgent = "Peernet Cmd/" + core.Version } func main() { diff --git a/go.mod b/go.mod index 8f743fa..fcbc42b 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,4 @@ module github.com/PeernetOfficial/Cmd go 1.16 -require github.com/PeernetOfficial/core v0.0.0-20210521003332-1a44b9c5e7e8 +require github.com/PeernetOfficial/core v0.0.0-20210706113727-27fdafd23135 diff --git a/go.sum b/go.sum index 9f4b048..f720bec 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/PeernetOfficial/core v0.0.0-20210521003332-1a44b9c5e7e8 h1:yywiMb2CScG2kVYeURTlV8h1UCj8o2vwJuyZDU7TCAM= -github.com/PeernetOfficial/core v0.0.0-20210521003332-1a44b9c5e7e8/go.mod h1:NglFBSMGFtMqR9ugIR0H244XYPvE1eC5H0X6sergW5U= +github.com/PeernetOfficial/core v0.0.0-20210706113727-27fdafd23135 h1:sTSVUqT6zja5hwjpQIodNxxiDTwFiPRw4Fm1rDT3QVY= +github.com/PeernetOfficial/core v0.0.0-20210706113727-27fdafd23135/go.mod h1:NglFBSMGFtMqR9ugIR0H244XYPvE1eC5H0X6sergW5U= github.com/PeernetOfficial/core/dht v0.0.0-20210506124834-e929d6ba2f22 h1:FHBHtQR3ShxuxkG8tgE4ER86UmbNdXam/I1irdiusqg= github.com/PeernetOfficial/core/dht v0.0.0-20210506124834-e929d6ba2f22/go.mod h1:sb2H21VIVTohCXVrDFo/Skl2tN8Yzba+MXSS6NgCYXw= github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII=