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

import (
	"fmt"
5
	"io"
6

7
	cmds "github.com/ipfs/go-ipfs/commands"
Jeromy's avatar
Jeromy committed
8
	logging "gx/ipfs/Qmazh5oNUVsDZTs2g59rq8aYQqwpss8tcUWQzor5sCCEuH/go-log"
9 10
)

11 12 13 14 15 16
// 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
17
var LogCmd = &cmds.Command{
18
	Helptext: cmds.HelpText{
rht's avatar
rht committed
19
		Tagline: "Interact with the daemon log output.",
20 21 22 23 24 25 26 27
		ShortDescription: `
'ipfs log' contains utility commands to affect or read the logging
output of a running daemon.
`,
	},

	Subcommands: map[string]*cmds.Command{
		"level": logLevelCmd,
28
		"tail":  logTailCmd,
29 30 31 32
	},
}

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

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

51
		args := req.Arguments()
52
		subsystem, level := args[0], args[1]
53 54 55 56 57

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

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

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

73
var logTailCmd = &cmds.Command{
74
	Helptext: cmds.HelpText{
rht's avatar
rht committed
75
		Tagline: "Read the logs.",
76
		ShortDescription: `
77
'ipfs log tail' is a utility command used to read log output as it is written.
78 79 80
`,
	},

81
	Run: func(req cmds.Request, res cmds.Response) {
82
		ctx := req.Context()
Jeromy's avatar
Jeromy committed
83
		r, w := io.Pipe()
84
		go func() {
85 86
			defer w.Close()
			<-ctx.Done()
87
		}()
88
		logging.WriterGroup.AddWriter(w)
Jeromy's avatar
Jeromy committed
89
		res.SetOutput(r)
90 91
	},
}