github.com/cloudfoundry-attic/ltc@v0.0.0-20151123212628-098adc7919fc/logs/logs.go (about) 1 package logs 2 3 import "github.com/cloudfoundry/sonde-go/events" 4 5 type LogReader interface { 6 TailLogs(appGuid string, logCallback func(*events.LogMessage), errorCallback func(error)) 7 StopTailing() 8 } 9 10 type logConsumer interface { 11 TailingLogs(appGuid string, authToken string, outputChan chan<- *events.LogMessage, errorChan chan<- error) 12 Close() error 13 } 14 15 type logReader struct { 16 consumer logConsumer 17 stopChan chan struct{} 18 } 19 20 func NewLogReader(consumer logConsumer) LogReader { 21 return &logReader{ 22 consumer: consumer, 23 stopChan: make(chan struct{}), 24 } 25 } 26 27 func (l *logReader) TailLogs(appGuid string, logCallback func(*events.LogMessage), errorCallback func(error)) { 28 outputChan := make(chan *events.LogMessage, 10) 29 errorChan := make(chan error, 10) 30 31 go l.consumer.TailingLogs(appGuid, "", outputChan, errorChan) 32 33 l.readChannels(outputChan, errorChan, logCallback, errorCallback) 34 } 35 36 func (l *logReader) StopTailing() { 37 l.stopChan <- struct{}{} 38 l.consumer.Close() 39 } 40 41 func (l *logReader) readChannels(outputChan <-chan *events.LogMessage, errorChan <-chan error, logCallback func(*events.LogMessage), errorCallback func(error)) { 42 for { 43 select { 44 case <-l.stopChan: 45 return 46 case err := <-errorChan: 47 errorCallback(err) 48 case logMessage, ok := (<-outputChan): 49 if !ok { 50 return 51 } 52 logCallback(logMessage) 53 } 54 } 55 }