github.com/kcmerrill/alfred@v0.0.0-20180727171036-06445dcb5e3d/pkg/alfred/log.go (about) 1 package alfred 2 3 import ( 4 "os" 5 "strings" 6 ) 7 8 // Log will set an external logger 9 func Log(filename string, context *Context) { 10 tasks := make(map[string]Task) 11 log(Task{ 12 Log: filename, 13 }, context, tasks) 14 } 15 func log(task Task, context *Context, tasks map[string]Task) { 16 if task.Log != "" { 17 l := translate(strings.TrimSpace(task.Log), context) 18 19 f, err := os.OpenFile(l, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0755) 20 if err == nil { 21 context.Lock.Lock() 22 context.Log[l] = f 23 context.Lock.Unlock() 24 } else { 25 outFail("log", err.Error(), context) 26 task.Exit(context, tasks) 27 } 28 } 29 } 30 31 func logger(text string, context *Context) { 32 c := *context 33 // strip away all the color 34 c.Text = TextConfig{} 35 for _, f := range context.Log { 36 f.WriteString(translate(text, context)) 37 } 38 }