logs.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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: options.Follow,
  57. Tail: options.Tail,
  58. })
  59. defer r.Close() // nolint errcheck
  60. if err != nil {
  61. return err
  62. }
  63. w := getWriter(service, container.ID, consumer)
  64. if container.Config.Tty {
  65. _, err = io.Copy(w, r)
  66. } else {
  67. _, err = stdcopy.StdCopy(w, w, r)
  68. }
  69. return err
  70. })
  71. }
  72. return eg.Wait()
  73. }
  74. type splitBuffer struct {
  75. service string
  76. container string
  77. consumer compose.LogConsumer
  78. }
  79. // getWriter creates a io.Writer that will actually split by line and format by LogConsumer
  80. func getWriter(service, container string, l compose.LogConsumer) io.Writer {
  81. return splitBuffer{
  82. service: service,
  83. container: container,
  84. consumer: l,
  85. }
  86. }
  87. func (s splitBuffer) Write(b []byte) (n int, err error) {
  88. split := bytes.Split(b, []byte{'\n'})
  89. for _, line := range split {
  90. if len(line) != 0 {
  91. s.consumer.Log(s.service, s.container, string(line))
  92. }
  93. }
  94. return len(b), nil
  95. }