log.go 2.72 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
Change the verbosity of one or all subsystems log output. Does NOT affect the event log.
39
`,
40
	},
41

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

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

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

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

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

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

91
var logTailCmd = &cmds.Command{
92
	Helptext: cmds.HelpText{
93
		Tagline: "Read the event log.",
94
		ShortDescription: `
95
Outputs event log messages (NOT other log messages) as they are generated.
96 97 98
`,
	},

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