diff --git a/config.go b/config.go index 215aba3ab..dd0a9212f 100644 --- a/config.go +++ b/config.go @@ -121,15 +121,10 @@ func supportedSubsystems() []string { return subsystems } -// parseDebugLevel attempt to parse the specified debug level and set the levels -// accordingly. An appropriate error is returned if anything is invalid. -func parseDebugLevel(debugLevel string) error { - // Special show command to list supported subsystems. - if debugLevel == "show" { - fmt.Println("Supported subsystems", supportedSubsystems()) - os.Exit(0) - } - +// parseAndSetDebugLevels attempts to parse the specified debug level and set +// the levels accordingly. An appropriate error is returned if anything is +// invalid. +func parseAndSetDebugLevels(debugLevel string) error { // When the specified string doesn't have any delimters, treat it as // the log level for all subsystems. if !strings.Contains(debugLevel, ",") && !strings.Contains(debugLevel, "=") { @@ -162,8 +157,9 @@ func parseDebugLevel(debugLevel string) error { // Validate subsystem. if _, exists := subsystemLoggers[subsysID]; !exists { - str := "The specified subsystem [%v] is invalid" - return fmt.Errorf(str, subsysID) + str := "The specified subsystem [%v] is invalid -- " + + "supported subsytems %v" + return fmt.Errorf(str, subsysID, supportedSubsystems()) } // Validate log level. @@ -326,8 +322,14 @@ func loadConfig() (*config, []string, error) { activeNetParams = netParams(btcwire.TestNet) } + // Special show command to list supported subsystems and exit. + if cfg.DebugLevel == "show" { + fmt.Println("Supported subsystems", supportedSubsystems()) + os.Exit(0) + } + // Parse, validate, and set debug log level(s). - if err := parseDebugLevel(cfg.DebugLevel); err != nil { + if err := parseAndSetDebugLevels(cfg.DebugLevel); err != nil { err := fmt.Errorf("%s: %v", "loadConfig", err.Error()) fmt.Fprintln(os.Stderr, err) parser.WriteHelp(os.Stderr) diff --git a/rpcserver.go b/rpcserver.go index bf6f4a19e..a6c4b8516 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -547,6 +547,7 @@ var handlers = map[string]commandHandler{ "backupwallet": handleAskWallet, "createmultisig": handleAskWallet, "createrawtransaction": handleUnimplemented, + "debuglevel": handleDebugLevel, "decoderawtransaction": handleDecodeRawTransaction, "decodescript": handleUnimplemented, "dumpprivkey": handleAskWallet, @@ -662,6 +663,29 @@ func handleAddNode(s *rpcServer, cmd btcjson.Cmd, return nil, err } +// handleDebugLevel handles debuglevel commands. +func handleDebugLevel(s *rpcServer, cmd btcjson.Cmd, + walletNotification chan []byte) (interface{}, error) { + c := cmd.(*btcjson.DebugLevelCmd) + + // Special show command to list supported subsystems. + if c.LevelSpec == "show" { + return fmt.Sprintf("Supported subsystems %v", + supportedSubsystems()), nil + } + + err := parseAndSetDebugLevels(c.LevelSpec) + if err != nil { + jsonErr := btcjson.Error{ + Code: btcjson.ErrInvalidParams.Code, + Message: err.Error(), + } + return nil, jsonErr + } + + return "Done.", nil +} + // handleDecodeRawTransaction handles decoderawtransaction commands. func handleDecodeRawTransaction(s *rpcServer, cmd btcjson.Cmd, walletNotification chan []byte) (interface{}, error) { diff --git a/util/btcctl/btcctl.go b/util/btcctl/btcctl.go index 1438d13cf..e4b8c94dd 100644 --- a/util/btcctl/btcctl.go +++ b/util/btcctl/btcctl.go @@ -44,6 +44,7 @@ var ( // to validate correctness and perform the command. var commandHandlers = map[string]*handlerData{ "addnode": &handlerData{2, 0, displaySpewDump, nil, makeAddNode, " "}, + "debuglevel": &handlerData{1, 0, displayGeneric, nil, makeDebugLevel, ""}, "decoderawtransaction": &handlerData{1, 0, displaySpewDump, nil, makeDecodeRawTransaction, ""}, "dumpprivkey": &handlerData{1, 0, displayGeneric, nil, makeDumpPrivKey, ""}, "getbestblockhash": &handlerData{0, 0, displayGeneric, nil, makeGetBestBlockHash, ""}, @@ -127,6 +128,11 @@ func makeAddNode(args []interface{}) (btcjson.Cmd, error) { args[1].(string)) } +// makeDebugLevel generates the cmd structure for debuglevel commands. +func makeDebugLevel(args []interface{}) (btcjson.Cmd, error) { + return btcjson.NewDebugLevelCmd("btcctl", args[0].(string)) +} + // makeDecodeRawTransaction generates the cmd structure for // decoderawtransaction comands. func makeDecodeRawTransaction(args []interface{}) (btcjson.Cmd, error) {