Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config update #56

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 19 additions & 35 deletions core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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")
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion core/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 11 additions & 6 deletions core/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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...)
}
}
Expand All @@ -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...)
}
}
12 changes: 4 additions & 8 deletions core/openp2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 [email protected]")

Expand Down Expand Up @@ -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 [email protected]")
gLog.Println(LvINFO, &gConf)
Expand Down