logs.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. "bytes"
  16. "context"
  17. "io"
  18. "github.com/docker/compose-cli/api/compose"
  19. "github.com/docker/docker/api/types"
  20. "github.com/docker/docker/api/types/filters"
  21. "github.com/docker/docker/pkg/stdcopy"
  22. "golang.org/x/sync/errgroup"
  23. )
  24. func (s *composeService) Logs(ctx context.Context, projectName string, consumer compose.LogConsumer, options compose.LogOptions) error {
  25. list, err := s.apiClient.ContainerList(ctx, types.ContainerListOptions{
  26. Filters: filters.NewArgs(
  27. projectFilter(projectName),
  28. ),
  29. All: true,
  30. })
  31. ignore := func(string) bool {
  32. return false
  33. }
  34. if len(options.Services) > 0 {
  35. ignore = func(s string) bool {
  36. return !contains(options.Services, s)
  37. }
  38. }
  39. if err != nil {
  40. return err
  41. }
  42. eg, ctx := errgroup.WithContext(ctx)
  43. for _, c := range list {
  44. service := c.Labels[serviceLabel]
  45. if ignore(service) {
  46. continue
  47. }
  48. container, err := s.apiClient.ContainerInspect(ctx, c.ID)
  49. if err != nil {
  50. return err
  51. }
  52. eg.Go(func() error {
  53. r, err := s.apiClient.ContainerLogs(ctx, container.ID, types.ContainerLogsOptions{
  54. ShowStdout: true,
  55. ShowStderr: true,
  56. Follow: true,
  57. })
  58. defer r.Close() // nolint errcheck
  59. if err != nil {
  60. return err
  61. }
  62. w := getWriter(service, container.ID, consumer)
  63. if container.Config.Tty {
  64. _, err = io.Copy(w, r)
  65. } else {
  66. _, err = stdcopy.StdCopy(w, w, r)
  67. }
  68. return err
  69. })
  70. }
  71. return eg.Wait()
  72. }
  73. type splitBuffer struct {
  74. service string
  75. container string
  76. consumer compose.LogConsumer
  77. }
  78. // getWriter creates a io.Writer that will actually split by line and format by LogConsumer
  79. func getWriter(service, container string, l compose.LogConsumer) io.Writer {
  80. return splitBuffer{
  81. service: service,
  82. container: container,
  83. consumer: l,
  84. }
  85. }
  86. func (s splitBuffer) Write(b []byte) (n int, err error) {
  87. split := bytes.Split(b, []byte{'\n'})
  88. for _, line := range split {
  89. if len(line) != 0 {
  90. s.consumer.Log(s.service, s.container, string(line))
  91. }
  92. }
  93. return len(b), nil
  94. }