github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/core/commands/log.go (about)

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  
     7  	cmds "github.com/ipfs/go-ipfs/commands"
     8  	"github.com/ipfs/go-ipfs/thirdparty/eventlog"
     9  	u "github.com/ipfs/go-ipfs/util"
    10  )
    11  
    12  // Golang os.Args overrides * and replaces the character argument with
    13  // an array which includes every file in the user's CWD. As a
    14  // workaround, we use 'all' instead. The util library still uses * so
    15  // we convert it at this step.
    16  var logAllKeyword = "all"
    17  
    18  var LogCmd = &cmds.Command{
    19  	Helptext: cmds.HelpText{
    20  		Tagline: "Interact with the daemon log output",
    21  		ShortDescription: `
    22  'ipfs log' contains utility commands to affect or read the logging
    23  output of a running daemon.
    24  `,
    25  	},
    26  
    27  	Subcommands: map[string]*cmds.Command{
    28  		"level": logLevelCmd,
    29  		"tail":  logTailCmd,
    30  	},
    31  }
    32  
    33  var logLevelCmd = &cmds.Command{
    34  	Helptext: cmds.HelpText{
    35  		Tagline: "Change the logging level",
    36  		ShortDescription: `
    37  'ipfs log level' is a utility command used to change the logging
    38  output of a running daemon.
    39  `,
    40  	},
    41  
    42  	Arguments: []cmds.Argument{
    43  		// TODO use a different keyword for 'all' because all can theoretically
    44  		// 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, "one of: debug, info, warning, error, fatal, panic"),
    47  	},
    48  	Run: func(req cmds.Request, res cmds.Response) {
    49  
    50  		args := req.Arguments()
    51  		subsystem, level := args[0], args[1]
    52  
    53  		if subsystem == logAllKeyword {
    54  			subsystem = "*"
    55  		}
    56  
    57  		if err := u.SetLogLevel(subsystem, level); err != nil {
    58  			res.SetError(err, cmds.ErrNormal)
    59  			return
    60  		}
    61  
    62  		s := fmt.Sprintf("Changed log level of '%s' to '%s'", subsystem, level)
    63  		log.Info(s)
    64  		res.SetOutput(&MessageOutput{s})
    65  	},
    66  	Marshalers: cmds.MarshalerMap{
    67  		cmds.Text: MessageTextMarshaler,
    68  	},
    69  	Type: MessageOutput{},
    70  }
    71  
    72  var logTailCmd = &cmds.Command{
    73  	Helptext: cmds.HelpText{
    74  		Tagline: "Read the logs",
    75  		ShortDescription: `
    76  'ipfs log tail' is a utility command used to read log output as it is written.
    77  `,
    78  	},
    79  
    80  	Run: func(req cmds.Request, res cmds.Response) {
    81  		r, w := io.Pipe()
    82  		eventlog.WriterGroup.AddWriter(w)
    83  		go func() {
    84  			<-req.Context().Done()
    85  			w.Close()
    86  		}()
    87  		res.SetOutput(r)
    88  	},
    89  }