logs.go 2.6 KB

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