Add support for debuglevel RPC command.

Also include the supported subsystems in the error message if an invalid
subsystem is specified.

Closes #15.
This commit is contained in:
Dave Collins 2013-11-22 10:46:56 -06:00
parent e930dc55f0
commit daa5310e2f
3 changed files with 44 additions and 12 deletions

View file

@ -121,15 +121,10 @@ func supportedSubsystems() []string {
return subsystems return subsystems
} }
// parseDebugLevel attempt to parse the specified debug level and set the levels // parseAndSetDebugLevels attempts to parse the specified debug level and set
// accordingly. An appropriate error is returned if anything is invalid. // the levels accordingly. An appropriate error is returned if anything is
func parseDebugLevel(debugLevel string) error { // invalid.
// Special show command to list supported subsystems. func parseAndSetDebugLevels(debugLevel string) error {
if debugLevel == "show" {
fmt.Println("Supported subsystems", supportedSubsystems())
os.Exit(0)
}
// When the specified string doesn't have any delimters, treat it as // When the specified string doesn't have any delimters, treat it as
// the log level for all subsystems. // the log level for all subsystems.
if !strings.Contains(debugLevel, ",") && !strings.Contains(debugLevel, "=") { if !strings.Contains(debugLevel, ",") && !strings.Contains(debugLevel, "=") {
@ -162,8 +157,9 @@ func parseDebugLevel(debugLevel string) error {
// Validate subsystem. // Validate subsystem.
if _, exists := subsystemLoggers[subsysID]; !exists { if _, exists := subsystemLoggers[subsysID]; !exists {
str := "The specified subsystem [%v] is invalid" str := "The specified subsystem [%v] is invalid -- " +
return fmt.Errorf(str, subsysID) "supported subsytems %v"
return fmt.Errorf(str, subsysID, supportedSubsystems())
} }
// Validate log level. // Validate log level.
@ -326,8 +322,14 @@ func loadConfig() (*config, []string, error) {
activeNetParams = netParams(btcwire.TestNet) 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). // 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()) err := fmt.Errorf("%s: %v", "loadConfig", err.Error())
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
parser.WriteHelp(os.Stderr) parser.WriteHelp(os.Stderr)

View file

@ -547,6 +547,7 @@ var handlers = map[string]commandHandler{
"backupwallet": handleAskWallet, "backupwallet": handleAskWallet,
"createmultisig": handleAskWallet, "createmultisig": handleAskWallet,
"createrawtransaction": handleUnimplemented, "createrawtransaction": handleUnimplemented,
"debuglevel": handleDebugLevel,
"decoderawtransaction": handleDecodeRawTransaction, "decoderawtransaction": handleDecodeRawTransaction,
"decodescript": handleUnimplemented, "decodescript": handleUnimplemented,
"dumpprivkey": handleAskWallet, "dumpprivkey": handleAskWallet,
@ -662,6 +663,29 @@ func handleAddNode(s *rpcServer, cmd btcjson.Cmd,
return nil, err 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. // handleDecodeRawTransaction handles decoderawtransaction commands.
func handleDecodeRawTransaction(s *rpcServer, cmd btcjson.Cmd, func handleDecodeRawTransaction(s *rpcServer, cmd btcjson.Cmd,
walletNotification chan []byte) (interface{}, error) { walletNotification chan []byte) (interface{}, error) {

View file

@ -44,6 +44,7 @@ var (
// to validate correctness and perform the command. // to validate correctness and perform the command.
var commandHandlers = map[string]*handlerData{ var commandHandlers = map[string]*handlerData{
"addnode": &handlerData{2, 0, displaySpewDump, nil, makeAddNode, "<ip> <add/remove/onetry>"}, "addnode": &handlerData{2, 0, displaySpewDump, nil, makeAddNode, "<ip> <add/remove/onetry>"},
"debuglevel": &handlerData{1, 0, displayGeneric, nil, makeDebugLevel, "<levelspec>"},
"decoderawtransaction": &handlerData{1, 0, displaySpewDump, nil, makeDecodeRawTransaction, "<txhash>"}, "decoderawtransaction": &handlerData{1, 0, displaySpewDump, nil, makeDecodeRawTransaction, "<txhash>"},
"dumpprivkey": &handlerData{1, 0, displayGeneric, nil, makeDumpPrivKey, "<bitcoinaddress>"}, "dumpprivkey": &handlerData{1, 0, displayGeneric, nil, makeDumpPrivKey, "<bitcoinaddress>"},
"getbestblockhash": &handlerData{0, 0, displayGeneric, nil, makeGetBestBlockHash, ""}, "getbestblockhash": &handlerData{0, 0, displayGeneric, nil, makeGetBestBlockHash, ""},
@ -127,6 +128,11 @@ func makeAddNode(args []interface{}) (btcjson.Cmd, error) {
args[1].(string)) 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 // makeDecodeRawTransaction generates the cmd structure for
// decoderawtransaction comands. // decoderawtransaction comands.
func makeDecodeRawTransaction(args []interface{}) (btcjson.Cmd, error) { func makeDecodeRawTransaction(args []interface{}) (btcjson.Cmd, error) {