mirror of
https://github.com/PeernetOfficial/Cmd.git
synced 2026-07-17 02:47:52 +01:00
Allow to change log error output via command and setting.
This commit is contained in:
@@ -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...)
|
||||
}
|
||||
}
|
||||
|
||||
12
Main.go
12
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() {
|
||||
|
||||
2
go.mod
2
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
|
||||
|
||||
4
go.sum
4
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=
|
||||
|
||||
Reference in New Issue
Block a user