OLD | NEW |
(Empty) | |
| 1 package cmd |
| 2 |
| 3 import ( |
| 4 "io" |
| 5 "launchpad.net/gnuflag" |
| 6 "launchpad.net/juju/go/log" |
| 7 stdlog "log" |
| 8 "os" |
| 9 ) |
| 10 |
| 11 // Log supplies the necessary functionality for Commands that wish to set up |
| 12 // logging. |
| 13 type Log struct { |
| 14 Path string |
| 15 Verbose bool |
| 16 Debug bool |
| 17 } |
| 18 |
| 19 // AddFlags adds appropriate flags to f. |
| 20 func (c *Log) AddFlags(f *gnuflag.FlagSet) { |
| 21 f.StringVar(&c.Path, "log-file", "", "path to write log to") |
| 22 f.BoolVar(&c.Verbose, "v", false, "if set, log additional messages") |
| 23 f.BoolVar(&c.Verbose, "verbose", false, "if set, log additional messages
") |
| 24 f.BoolVar(&c.Debug, "debug", false, "if set, log debugging messages") |
| 25 } |
| 26 |
| 27 // Start starts logging using the given Context. |
| 28 func (c *Log) Start(ctx *Context) (err error) { |
| 29 log.Debug = c.Debug |
| 30 var target io.Writer |
| 31 if c.Path != "" { |
| 32 path := ctx.AbsPath(c.Path) |
| 33 target, err = os.OpenFile(path, os.O_WRONLY|os.O_APPEND|os.O_CRE
ATE, 0644) |
| 34 if err != nil { |
| 35 return |
| 36 } |
| 37 } else if c.Verbose || c.Debug { |
| 38 target = ctx.Stderr |
| 39 } |
| 40 if target != nil { |
| 41 log.Target = stdlog.New(target, "", stdlog.LstdFlags) |
| 42 } else { |
| 43 log.Target = nil |
| 44 } |
| 45 return |
| 46 } |
OLD | NEW |