log.go 2.79 KB
Newer Older
1 2 3 4
package commands

import (
	"fmt"
5
	"io"
Hector Sanjuan's avatar
Hector Sanjuan committed
6
	"strings"
7

8
	logging "gx/ipfs/QmaDNZ4QMdBdku1YZWBysufYyoQt1negQGNav6PLYarbY8/go-log"
Hector Sanjuan's avatar
Hector Sanjuan committed
9 10

	cmds "github.com/ipfs/go-ipfs/commands"
11 12
)

13 14 15 16 17 18
// Golang os.Args overrides * and replaces the character argument with
// an array which includes every file in the user's CWD. As a
// workaround, we use 'all' instead. The util library still uses * so
// we convert it at this step.
var logAllKeyword = "all"

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
19
var LogCmd = &cmds.Command{
20
	Helptext: cmds.HelpText{
rht's avatar
rht committed
21
		Tagline: "Interact with the daemon log output.",
22 23 24 25 26 27 28 29
		ShortDescription: `
'ipfs log' contains utility commands to affect or read the logging
output of a running daemon.
`,
	},

	Subcommands: map[string]*cmds.Command{
		"level": logLevelCmd,
Hector Sanjuan's avatar
Hector Sanjuan committed
30
		"ls":    logLsCmd,
31
		"tail":  logTailCmd,
32 33 34 35
	},
}

var logLevelCmd = &cmds.Command{
36
	Helptext: cmds.HelpText{
rht's avatar
rht committed
37
		Tagline: "Change the logging level.",
38
		ShortDescription: `
39
'ipfs log level' is a utility command used to change the logging
40 41
output of a running daemon.
`,
42
	},
43

44
	Arguments: []cmds.Argument{
45 46
		// TODO use a different keyword for 'all' because all can theoretically
		// clash with a subsystem name
47
		cmds.StringArg("subsystem", true, false, fmt.Sprintf("The subsystem logging identifier. Use '%s' for all subsystems.", logAllKeyword)),
48 49
		cmds.StringArg("level", true, false, `The log level, with 'debug' the most verbose and 'critical' the least verbose.
			One of: debug, info, notice, warning, error, critical.
50
		`),
51
	},
52
	Run: func(req cmds.Request, res cmds.Response) {
53

54
		args := req.Arguments()
55
		subsystem, level := args[0], args[1]
56 57 58 59 60

		if subsystem == logAllKeyword {
			subsystem = "*"
		}

Jeromy's avatar
Jeromy committed
61
		if err := logging.SetLogLevel(subsystem, level); err != nil {
62 63
			res.SetError(err, cmds.ErrNormal)
			return
64 65
		}

66
		s := fmt.Sprintf("Changed log level of '%s' to '%s'\n", subsystem, level)
67
		log.Info(s)
68
		res.SetOutput(&MessageOutput{s})
69
	},
70 71
	Marshalers: cmds.MarshalerMap{
		cmds.Text: MessageTextMarshaler,
72
	},
73
	Type: MessageOutput{},
74
}
75

Hector Sanjuan's avatar
Hector Sanjuan committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
var logLsCmd = &cmds.Command{
	Helptext: cmds.HelpText{
		Tagline: "List the logging subsystems.",
		ShortDescription: `
'ipfs log ls' is a utility command used to list the logging
subsystems of a running daemon.
`,
	},
	Run: func(req cmds.Request, res cmds.Response) {
		output := strings.Join(logging.GetSubsystems(), "\n")
		res.SetOutput(&MessageOutput{output + "\n"})
	},
	Marshalers: cmds.MarshalerMap{
		cmds.Text: MessageTextMarshaler,
	},
	Type: MessageOutput{},
}

94
var logTailCmd = &cmds.Command{
95
	Helptext: cmds.HelpText{
rht's avatar
rht committed
96
		Tagline: "Read the logs.",
97
		ShortDescription: `
98
'ipfs log tail' is a utility command used to read log output as it is written.
99 100 101
`,
	},

102
	Run: func(req cmds.Request, res cmds.Response) {
103
		ctx := req.Context()
Jeromy's avatar
Jeromy committed
104
		r, w := io.Pipe()
105
		go func() {
106 107
			defer w.Close()
			<-ctx.Done()
108
		}()
109
		logging.WriterGroup.AddWriter(w)
Jeromy's avatar
Jeromy committed
110
		res.SetOutput(r)
111 112
	},
}