From 42598a95921eaa5eb47ee4e760149f4bf90317ce Mon Sep 17 00:00:00 2001 From: W192547975 <138874985+W192547975@users.noreply.github.com> Date: Sat, 18 Nov 2023 12:21:06 +0800 Subject: [PATCH 1/7] Update config.go --- core/config.go | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/core/config.go b/core/config.go index 87f46b0..54cafb0 100644 --- a/core/config.go +++ b/core/config.go @@ -229,7 +229,7 @@ func parseParams(subCommand string) { daemonMode := fset.Bool("d", false, "daemonMode") notVerbose := fset.Bool("nv", false, "not log console") newconfig := fset.Bool("newconfig", false, "not load existing config.json") - logLevel := fset.Int("loglevel", 1, "0:debug 1:info 2:warn 3:error") + logLevel := fset.Int("loglevel", int(LvINFO), "0:debug 1:info 2:warn 3:error") if subCommand == "" { // no subcommand fset.Parse(os.Args[1:]) } else { @@ -255,22 +255,16 @@ func parseParams(subCommand string) { gConf.daemonMode = *daemonMode // spec paramters in commandline will always be used fset.Visit(func(f *flag.Flag) { - if f.Name == "sharebandwidth" { + select f.Name { + case "sharebandwidth": gConf.Network.ShareBandwidth = *shareBandwidth - } - if f.Name == "node" { - gConf.Network.Node = *node - } - if f.Name == "serverhost" { - gConf.Network.ServerHost = *serverHost - } - if f.Name == "loglevel" { + case "serverhost": + gConf.Network.ServerHost = "" + case "loglevel": gConf.LogLevel = *logLevel - } - if f.Name == "tcpport" { - gConf.Network.TCPPort = *tcpPort - } - if f.Name == "token" { + case "tcpport": + gConf.Network.TCPPort = 0 + case "token": gConf.setToken(*token) } }) @@ -309,7 +303,7 @@ func parseParams(subCommand string) { gConf.Network.UDPPort2 = UDPPort2 gLog.setLevel(LogLevel(gConf.LogLevel)) if *notVerbose { - gLog.setMode(LogFile) + gLog.setMode(gLog.Mode() &^ LogConsole) } // gConf.mtx.Unlock() gConf.save() From 82f3ecde05e53f9a1d6caa3489c464337bc325b2 Mon Sep 17 00:00:00 2001 From: W192547975 <138874985+W192547975@users.noreply.github.com> Date: Sat, 18 Nov 2023 12:22:59 +0800 Subject: [PATCH 2/7] Update log.go --- core/log.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/core/log.go b/core/log.go index cda5595..6ea735d 100644 --- a/core/log.go +++ b/core/log.go @@ -97,6 +97,11 @@ func (l *logger) setMode(mode int) { defer l.mtx.Unlock() l.mode = mode } +func (l *logger) Mode() int { + l.mtx.Lock() + defer l.mtx.Unlock() + return l.mode +} func (l *logger) checkFile() { if l.maxLogSize <= 0 { From 6ca324275b108a28f856b0615ff6309d32c7d500 Mon Sep 17 00:00:00 2001 From: W192547975 Date: Wed, 22 Nov 2023 21:20:17 +0800 Subject: [PATCH 3/7] arguments update --- core/config.go | 10 +++------- core/install.go | 2 +- core/openp2p.go | 12 ++++-------- 3 files changed, 8 insertions(+), 16 deletions(-) diff --git a/core/config.go b/core/config.go index 54cafb0..4be07e0 100644 --- a/core/config.go +++ b/core/config.go @@ -209,8 +209,8 @@ type NetworkConfig struct { TCPPort int } -func parseParams(subCommand string) { - fset := flag.NewFlagSet(subCommand, flag.ExitOnError) +func parseParams(name string, arguments []string) { + fset := flag.NewFlagSet(name, flag.ExitOnError) serverHost := fset.String("serverhost", "api.openp2p.cn", "server host ") serverPort := fset.Int("serverport", WsPort, "server port ") // serverHost := flag.String("serverhost", "127.0.0.1", "server host ") // for debug @@ -230,11 +230,7 @@ func parseParams(subCommand string) { notVerbose := fset.Bool("nv", false, "not log console") newconfig := fset.Bool("newconfig", false, "not load existing config.json") logLevel := fset.Int("loglevel", int(LvINFO), "0:debug 1:info 2:warn 3:error") - if subCommand == "" { // no subcommand - fset.Parse(os.Args[1:]) - } else { - fset.Parse(os.Args[2:]) - } + fset.Parse(arguments) config := AppConfig{Enabled: 1} config.PeerNode = *peerNode diff --git a/core/install.go b/core/install.go index 3c0dfb3..97a0565 100644 --- a/core/install.go +++ b/core/install.go @@ -35,7 +35,7 @@ func install() { uninstall() // save config file - parseParams("install") + parseParams("install", os.Args[2:]) targetPath := filepath.Join(defaultInstallPath, defaultBinName) d := daemon{} // copy files diff --git a/core/openp2p.go b/core/openp2p.go index a6b2a5d..a752b22 100644 --- a/core/openp2p.go +++ b/core/openp2p.go @@ -29,7 +29,7 @@ func Run() { } else { installByFilename() } - parseParams("") + parseParams("", os.Args[1:]) gLog.Println(LvINFO, "openp2p start. version: ", OpenP2PVersion) gLog.Println(LvINFO, "Contact: QQ group 16947733, Email openp2p.cn@gmail.com") @@ -64,14 +64,10 @@ func RunAsModule(baseDir string, token string, bw int, logLevel int) *P2PNetwork os.Chdir(baseDir) // for system service gLog = NewLogger(baseDir, ProductName, LvDEBUG, 1024*1024, LogFile|LogConsole) - parseParams("") + buf := make([]string, 0, len(os.Args)) + buf = append(buf, os.Args[1:], "-token", token, "-loglevel", strconv.FormatInt(logLevel, 10), "-sharebandwidth", strconv.FormatInt(bw, 10)) + parseParams("", buf) - n, err := strconv.ParseUint(token, 10, 64) - if err == nil { - gConf.setToken(n) - } - gLog.setLevel(LogLevel(logLevel)) - gConf.setShareBandwidth(bw) gLog.Println(LvINFO, "openp2p start. version: ", OpenP2PVersion) gLog.Println(LvINFO, "Contact: QQ group 16947733, Email openp2p.cn@gmail.com") gLog.Println(LvINFO, &gConf) From c3956c5182fbe57f99a874c85a035ce73305dcae Mon Sep 17 00:00:00 2001 From: W192547975 Date: Thu, 23 Nov 2023 13:03:47 +0800 Subject: [PATCH 4/7] fixbug --- core/openp2p.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/openp2p.go b/core/openp2p.go index a752b22..20aeb20 100644 --- a/core/openp2p.go +++ b/core/openp2p.go @@ -64,9 +64,9 @@ func RunAsModule(baseDir string, token string, bw int, logLevel int) *P2PNetwork os.Chdir(baseDir) // for system service gLog = NewLogger(baseDir, ProductName, LvDEBUG, 1024*1024, LogFile|LogConsole) - buf := make([]string, 0, len(os.Args)) - buf = append(buf, os.Args[1:], "-token", token, "-loglevel", strconv.FormatInt(logLevel, 10), "-sharebandwidth", strconv.FormatInt(bw, 10)) - parseParams("", buf) + args := append(os.Args[1:], "-token", token, "-loglevel", strconv.FormatInt(logLevel, 10), + "-sharebandwidth", strconv.FormatInt(bw, 10)) + parseParams("", args) gLog.Println(LvINFO, "openp2p start. version: ", OpenP2PVersion) gLog.Println(LvINFO, "Contact: QQ group 16947733, Email openp2p.cn@gmail.com") From 181c04971cf3d7d0b59bfe05e6df337dfadc256f Mon Sep 17 00:00:00 2001 From: W192547975 Date: Thu, 23 Nov 2023 13:12:14 +0800 Subject: [PATCH 5/7] rename --- core/config.go | 2 +- core/log.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/config.go b/core/config.go index 4be07e0..cebd554 100644 --- a/core/config.go +++ b/core/config.go @@ -299,7 +299,7 @@ func parseParams(name string, arguments []string) { gConf.Network.UDPPort2 = UDPPort2 gLog.setLevel(LogLevel(gConf.LogLevel)) if *notVerbose { - gLog.setMode(gLog.Mode() &^ LogConsole) + gLog.setMode(gLog.mode() &^ LogConsole) } // gConf.mtx.Unlock() gConf.save() diff --git a/core/log.go b/core/log.go index 6ea735d..7b38c4d 100644 --- a/core/log.go +++ b/core/log.go @@ -97,7 +97,7 @@ func (l *logger) setMode(mode int) { defer l.mtx.Unlock() l.mode = mode } -func (l *logger) Mode() int { +func (l *logger) mode() int { l.mtx.Lock() defer l.mtx.Unlock() return l.mode From 36d0ca52322a9f1e62c026b5e1eb2a344d3e95ad Mon Sep 17 00:00:00 2001 From: W192547975 <138874985+W192547975@users.noreply.github.com> Date: Sat, 30 Dec 2023 15:38:45 +0800 Subject: [PATCH 6/7] Update config.go --- core/config.go | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/core/config.go b/core/config.go index cebd554..b852efc 100644 --- a/core/config.go +++ b/core/config.go @@ -259,32 +259,26 @@ func parseParams(name string, arguments []string) { case "loglevel": gConf.LogLevel = *logLevel case "tcpport": - gConf.Network.TCPPort = 0 + gConf.Network.TCPPort = *tcpPort case "token": gConf.setToken(*token) + case "node": + gConf.Network.Node = *node } }) // set default value if gConf.Network.ServerHost == "" { gConf.Network.ServerHost = *serverHost } - if *node != "" { - gConf.Network.Node = *node - } else { + if gConf.Network.Node == "" { envNode := os.Getenv("OPENP2P_NODE") - if envNode != "" { - gConf.Network.Node = envNode - } - if gConf.Network.Node == "" { // if node name not set. use os.Hostname - gConf.Network.Node = defaultNodeName() + if envNode == "" { // if node name not set. use os.Hostname + envNode = defaultNodeName() } + gConf.Network.Node = envNode } if gConf.Network.TCPPort == 0 { - if *tcpPort == 0 { - p := int(nodeNameToID(gConf.Network.Node)%15000 + 50000) - tcpPort = &p - } - gConf.Network.TCPPort = *tcpPort + gConf.Network.TCPPort = int(nodeNameToID(gConf.Network.Node)%15000 + 50000) } if *token == 0 { envToken := os.Getenv("OPENP2P_TOKEN") From dcdb1543183703aee027dcad0b2384676acf1318 Mon Sep 17 00:00:00 2001 From: W192547975 Date: Thu, 8 Feb 2024 13:50:39 +0800 Subject: [PATCH 7/7] Update log.go Add spaces --- core/log.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/core/log.go b/core/log.go index 7b38c4d..ac7b79b 100644 --- a/core/log.go +++ b/core/log.go @@ -29,8 +29,8 @@ func init() { loglevel = make(map[LogLevel]string) logFileNames[0] = ".log" loglevel[LvDEBUG] = "DEBUG" - loglevel[LvINFO] = "INFO" - loglevel[LvWARN] = "WARN" + loglevel[LvINFO] = "INFO " + loglevel[LvWARN] = "WARN " loglevel[LvERROR] = "ERROR" } @@ -144,10 +144,10 @@ func (l *logger) Printf(level LogLevel, format string, params ...interface{}) { } pidAndLevel := []interface{}{l.pid, loglevel[level]} params = append(pidAndLevel, params...) - if l.mode & LogFile != 0 { + if l.mode&LogFile != 0 { l.loggers[0].Printf("%d %s "+format+l.lineEnding, params...) } - if l.mode & LogConsole != 0 { + if l.mode&LogConsole != 0 { l.stdLogger.Printf("%d %s "+format+l.lineEnding, params...) } } @@ -161,10 +161,10 @@ func (l *logger) Println(level LogLevel, params ...interface{}) { pidAndLevel := []interface{}{l.pid, " ", loglevel[level], " "} params = append(pidAndLevel, params...) params = append(params, l.lineEnding) - if l.mode & LogFile != 0 { + if l.mode&LogFile != 0 { l.loggers[0].Print(params...) } - if l.mode & LogConsole != 0 { + if l.mode&LogConsole != 0 { l.stdLogger.Print(params...) } }