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

import (
	"fmt"
5
	"io"
6

Jeromy's avatar
Jeromy committed
7
	logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
Hector Sanjuan's avatar
Hector Sanjuan committed
8 9

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

12 13 14 15 16 17
// 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
18
var LogCmd = &cmds.Command{
19
	Helptext: cmds.HelpText{
rht's avatar
rht committed
20
		Tagline: "Interact with the daemon log output.",
21 22 23 24 25 26 27 28
		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
29
		"ls":    logLsCmd,
30
		"tail":  logTailCmd,
31 32 33 34
	},
}

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

43
	Arguments: []cmds.Argument{
44 45
		// TODO use a different keyword for 'all' because all can theoretically
		// clash with a subsystem name
46
		cmds.StringArg("subsystem", true, false, fmt.Sprintf("The subsystem logging identifier. Use '%s' for all subsystems.", logAllKeyword)),
47 48
		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.
49
		`),
50
	},
51
	Run: func(req cmds.Request, res cmds.Response) {
52

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

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

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

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

Hector Sanjuan's avatar
Hector Sanjuan committed
75 76 77 78 79 80 81 82 83
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) {
Richard Littauer's avatar
Richard Littauer committed
84
		res.SetOutput(&stringList{logging.GetSubsystems()})
Hector Sanjuan's avatar
Hector Sanjuan committed
85 86
	},
	Marshalers: cmds.MarshalerMap{
Richard Littauer's avatar
Richard Littauer committed
87
		cmds.Text: stringListMarshaler,
Hector Sanjuan's avatar
Hector Sanjuan committed
88
	},
Richard Littauer's avatar
Richard Littauer committed
89
	Type: stringList{},
Hector Sanjuan's avatar
Hector Sanjuan committed
90 91
}

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

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