github.com/bdwilliams/libcompose@v0.3.1-0.20160826154243-d81a9bdacff0/project/listener.go (about) 1 package project 2 3 import ( 4 "bytes" 5 6 "github.com/Sirupsen/logrus" 7 "github.com/docker/libcompose/project/events" 8 ) 9 10 var ( 11 infoEvents = map[events.EventType]bool{ 12 events.ServiceDeleteStart: true, 13 events.ServiceDelete: true, 14 events.ServiceDownStart: true, 15 events.ServiceDown: true, 16 events.ServiceStopStart: true, 17 events.ServiceStop: true, 18 events.ServiceKillStart: true, 19 events.ServiceKill: true, 20 events.ServiceCreateStart: true, 21 events.ServiceCreate: true, 22 events.ServiceStartStart: true, 23 events.ServiceStart: true, 24 events.ServiceRestartStart: true, 25 events.ServiceRestart: true, 26 events.ServiceUpStart: true, 27 events.ServiceUp: true, 28 events.ServicePauseStart: true, 29 events.ServicePause: true, 30 events.ServiceUnpauseStart: true, 31 events.ServiceUnpause: true, 32 } 33 ) 34 35 type defaultListener struct { 36 project *Project 37 listenChan chan events.Event 38 upCount int 39 } 40 41 // NewDefaultListener create a default listener for the specified project. 42 func NewDefaultListener(p *Project) chan<- events.Event { 43 l := defaultListener{ 44 listenChan: make(chan events.Event), 45 project: p, 46 } 47 go l.start() 48 return l.listenChan 49 } 50 51 func (d *defaultListener) start() { 52 for event := range d.listenChan { 53 buffer := bytes.NewBuffer(nil) 54 if event.Data != nil { 55 for k, v := range event.Data { 56 if buffer.Len() > 0 { 57 buffer.WriteString(", ") 58 } 59 buffer.WriteString(k) 60 buffer.WriteString("=") 61 buffer.WriteString(v) 62 } 63 } 64 65 if event.EventType == events.ServiceUp { 66 d.upCount++ 67 } 68 69 logf := logrus.Debugf 70 71 if infoEvents[event.EventType] { 72 logf = logrus.Infof 73 } 74 75 if event.ServiceName == "" { 76 logf("Project [%s]: %s %s", d.project.Name, event.EventType, buffer.Bytes()) 77 } else { 78 logf("[%d/%d] [%s]: %s %s", d.upCount, d.project.ServiceConfigs.Len(), event.ServiceName, event.EventType, buffer.Bytes()) 79 } 80 } 81 }