diff --git a/cmd/harmony/main.go b/cmd/harmony/main.go index 5739688f7..cc66f7988 100644 --- a/cmd/harmony/main.go +++ b/cmd/harmony/main.go @@ -117,8 +117,8 @@ func initSetup() { } // Logging setup - utils.SetPortAndIP(*port, *ip) - utils.SetVerbosity(log.Lvl(*verbosity)) + utils.SetLogContext(*port, *ip) + utils.SetLogVerbosity(log.Lvl(*verbosity)) // Set default keystore Dir hmykey.DefaultKeyStoreDir = *keystoreDir diff --git a/internal/hmyapi/backend.go b/internal/hmyapi/backend.go index 8a766c0f7..0df6594c4 100644 --- a/internal/hmyapi/backend.go +++ b/internal/hmyapi/backend.go @@ -30,6 +30,11 @@ func GetAPIs(b *core.HmyAPIBackend) []rpc.API { Version: "1.0", Service: NewPublicAccountAPI(b.AccountManager()), Public: true, + }, { + Namespace: "hmy", + Version: "1.0", + Service: NewDebugAPI(b), + Public: true, // FIXME: change to false once IPC implemented }, } } diff --git a/internal/hmyapi/debug.go b/internal/hmyapi/debug.go new file mode 100644 index 000000000..ed7e2c3e0 --- /dev/null +++ b/internal/hmyapi/debug.go @@ -0,0 +1,33 @@ +package hmyapi + +import ( + "context" + "errors" + + "github.com/ethereum/go-ethereum/log" + "github.com/harmony-one/harmony/core" + "github.com/harmony-one/harmony/internal/utils" +) + +// DebugAPI Internal JSON RPC for debugging purpose +type DebugAPI struct { + b *core.HmyAPIBackend +} + +// NewDebugAPI Creates a new DebugAPI instance +func NewDebugAPI(b *core.HmyAPIBackend) *DebugAPI { + return &DebugAPI{b} +} + +// SetLogVerbosity Sets log verbosity on runtime +// Example usage: +// curl -H "Content-Type: application/json" -d '{"method":"debug_setLogVerbosity","params":[0],"id":1}' http://localhost:9123 +func (*DebugAPI) SetLogVerbosity(ctx context.Context, level int) (map[string]interface{}, error) { + if level < int(log.LvlCrit) || level > int(log.LvlTrace) { + return nil, errors.New("invalid log level") + } + + verbosity := log.Lvl(level) + utils.SetLogVerbosity(verbosity) + return map[string]interface{}{"verbosity": verbosity.String()}, nil +}