cmd: add set-log-categories

example:

	$ monero daemon set-log-categories \
		--categories=net.http:4,net.dns:2

	Status: OK

	CATEGORY        LEVEL
	net.http        4
	net.dns         2

Signed-off-by: Ciro S. Costa <utxobr@protonmail.com>
This commit is contained in:
Ciro S. Costa 2021-09-19 14:11:16 +00:00
parent 96063a2959
commit 48852069f5
4 changed files with 210 additions and 0 deletions

View file

@ -0,0 +1,85 @@
package daemon
import (
"fmt"
"strings"
"github.com/spf13/cobra"
"github.com/cirocosta/go-monero/cmd/monero/display"
"github.com/cirocosta/go-monero/cmd/monero/options"
"github.com/cirocosta/go-monero/pkg/rpc/daemon"
)
type setLogCategoriesCommand struct {
Categories string
JSON bool
}
func (c *setLogCategoriesCommand) Cmd() *cobra.Command {
cmd := &cobra.Command{
Use: "set-log-categories",
Short: "set the categories for which logs should be emitted",
RunE: c.RunE,
}
cmd.Flags().BoolVar(&c.JSON, "json",
false, "whether or not to output the result as json")
cmd.Flags().StringVar(&c.Categories, "categories",
"", "comma-separated list of <category>:<level> pairs")
_ = cmd.MarkFlagRequired("categories")
return cmd
}
func (c *setLogCategoriesCommand) RunE(_ *cobra.Command, _ []string) error {
ctx, cancel := options.RootOpts.Context()
defer cancel()
client, err := options.RootOpts.Client()
if err != nil {
return fmt.Errorf("client: %w", err)
}
params := daemon.SetLogCategoriesRequestParameters{
Categories: c.Categories,
}
resp, err := client.SetLogCategories(ctx, params)
if err != nil {
return fmt.Errorf("set log level: %w", err)
}
if c.JSON {
return display.JSON(resp)
}
return c.pretty(resp)
}
// nolint:forbidigo
func (c *setLogCategoriesCommand) pretty(v *daemon.SetLogCategoriesResult) error {
table := display.NewTable()
table.AddRow("Status:", v.Status)
table.AddRow("")
fmt.Println(table)
table = display.NewTable()
table.AddRow("CATEGORY", "LEVEL")
for _, pair := range strings.Split(v.Categories, ",") {
parts := strings.SplitN(pair, ":", 2)
if len(parts) != 2 {
return fmt.Errorf("expected 2 parts, got %d", len(parts))
}
table.AddRow(parts[0], parts[1])
}
fmt.Println(table)
return nil
}
func init() {
RootCommand.AddCommand((&setLogCategoriesCommand{}).Cmd())
}

View file

@ -0,0 +1,69 @@
package daemon
import (
"fmt"
"github.com/spf13/cobra"
"github.com/cirocosta/go-monero/cmd/monero/display"
"github.com/cirocosta/go-monero/cmd/monero/options"
"github.com/cirocosta/go-monero/pkg/rpc/daemon"
)
type setLogLevelCommand struct {
Level int8
JSON bool
}
func (c *setLogLevelCommand) Cmd() *cobra.Command {
cmd := &cobra.Command{
Use: "set-log-level",
Short: "configure the daemon log level",
RunE: c.RunE,
}
cmd.Flags().BoolVar(&c.JSON, "json",
false, "whether or not to output the result as json")
cmd.Flags().Int8Var(&c.Level, "level",
0, "daemon log level (0-4, from less to more verbose")
_ = cmd.MarkFlagRequired("level")
return cmd
}
func (c *setLogLevelCommand) RunE(_ *cobra.Command, _ []string) error {
ctx, cancel := options.RootOpts.Context()
defer cancel()
client, err := options.RootOpts.Client()
if err != nil {
return fmt.Errorf("client: %w", err)
}
params := daemon.SetLogLevelRequestParameters{
Level: c.Level,
}
resp, err := client.SetLogLevel(ctx, params)
if err != nil {
return fmt.Errorf("set log level: %w", err)
}
if c.JSON {
return display.JSON(resp)
}
c.pretty(resp)
return nil
}
// nolint:forbidigo
func (c *setLogLevelCommand) pretty(v *daemon.SetLogLevelResult) {
table := display.NewTable()
table.AddRow("Status:", v.Status)
fmt.Println(table)
}
func init() {
RootCommand.AddCommand((&setLogLevelCommand{}).Cmd())
}

View file

@ -18,6 +18,8 @@ const (
endpointGetTransactions = "/get_transactions"
endpointMiningStatus = "/mining_status"
endpointSetLimit = "/set_limit"
endpointSetLogLevel = "/set_log_level"
endpointSetLogCategories = "/set_log_categories"
endpointStartMining = "/start_mining"
endpointStopMining = "/stop_mining"
)
@ -46,6 +48,32 @@ func (c *Client) GetLimit(ctx context.Context) (*GetLimitResult, error) {
return resp, nil
}
func (c *Client) SetLogCategories(
ctx context.Context, params SetLogCategoriesRequestParameters,
) (*SetLogCategoriesResult, error) {
resp := &SetLogCategoriesResult{}
err := c.RawRequest(ctx, endpointSetLogCategories, params, resp)
if err != nil {
return nil, fmt.Errorf("raw request: %w", err)
}
return resp, nil
}
func (c *Client) SetLogLevel(
ctx context.Context, params SetLogLevelRequestParameters,
) (*SetLogLevelResult, error) {
resp := &SetLogLevelResult{}
err := c.RawRequest(ctx, endpointSetLogLevel, params, resp)
if err != nil {
return nil, fmt.Errorf("raw request: %w", err)
}
return resp, nil
}
func (c *Client) SetLimit(
ctx context.Context, params SetLimitRequestParameters,
) (*SetLimitResult, error) {

View file

@ -852,6 +852,34 @@ type GetTransactionPoolResult struct {
Untrusted bool `json:"untrusted"`
}
type SetLogCategoriesRequestParameters struct {
// Categories to log with their corresponding levels formatted as a
// comma-separated list of <category>:<level> pairs.
//
// For instance, to activate verbosity 1 for the `net.http` category
// and verbosity 4 for `net.dns`:
//
// net.htpp:1,net.dns:4
//
Categories string `json:"categories"`
}
type SetLogCategoriesResult struct {
Categories string `json:"categories"`
RPCResultFooter `json:",inline"`
}
type SetLogLevelRequestParameters struct {
// Level is the log level that the daemon should use. From 0 to 4 (less
// verbose to more verbose).
//
Level int8 `json:"level"`
}
type SetLogLevelResult struct {
RPCResultFooter `json:",inline"`
}
type SetLimitRequestParameters struct {
// LimitUp is the upload limit in kB/s
//