diff --git a/core/config.go b/core/config.go index 87f46b0..b852efc 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 @@ -229,12 +229,8 @@ 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") - if subCommand == "" { // no subcommand - fset.Parse(os.Args[1:]) - } else { - fset.Parse(os.Args[2:]) - } + logLevel := fset.Int("loglevel", int(LvINFO), "0:debug 1:info 2:warn 3:error") + fset.Parse(arguments) config := AppConfig{Enabled: 1} config.PeerNode = *peerNode @@ -255,46 +251,34 @@ 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" { + case "tcpport": gConf.Network.TCPPort = *tcpPort - } - if f.Name == "token" { + 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") @@ -309,7 +293,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() 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/log.go b/core/log.go index cda5595..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" } @@ -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 { @@ -139,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...) } } @@ -156,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...) } } diff --git a/core/openp2p.go b/core/openp2p.go index a6b2a5d..20aeb20 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("") + args := append(os.Args[1:], "-token", token, "-loglevel", strconv.FormatInt(logLevel, 10), + "-sharebandwidth", strconv.FormatInt(bw, 10)) + parseParams("", args) - 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)