printer.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "fmt"
  16. "github.com/docker/compose/v2/pkg/api"
  17. )
  18. // logPrinter watch application containers and collect their logs
  19. type logPrinter interface {
  20. HandleEvent(event api.ContainerEvent)
  21. }
  22. type printer struct {
  23. consumer api.LogConsumer
  24. }
  25. // newLogPrinter builds a LogPrinter passing containers logs to LogConsumer
  26. func newLogPrinter(consumer api.LogConsumer) logPrinter {
  27. printer := printer{
  28. consumer: consumer,
  29. }
  30. return &printer
  31. }
  32. func (p *printer) HandleEvent(event api.ContainerEvent) {
  33. switch event.Type {
  34. case api.ContainerEventExited:
  35. p.consumer.Status(event.Source, fmt.Sprintf("exited with code %d", event.ExitCode))
  36. case api.ContainerEventRecreated:
  37. p.consumer.Status(event.Container.Labels[api.ContainerReplaceLabel], "has been recreated")
  38. case api.ContainerEventLog, api.HookEventLog:
  39. p.consumer.Log(event.Source, event.Line)
  40. case api.ContainerEventErr:
  41. p.consumer.Err(event.Source, event.Line)
  42. }
  43. }