logs.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 utils
  14. import (
  15. "bytes"
  16. "io"
  17. "github.com/docker/compose-cli/api/compose"
  18. )
  19. // FilteredLogConsumer filters logs for given services
  20. func FilteredLogConsumer(consumer compose.LogConsumer, services []string) compose.LogConsumer {
  21. if len(services) == 0 {
  22. return consumer
  23. }
  24. allowed := map[string]bool{}
  25. for _, s := range services {
  26. allowed[s] = true
  27. }
  28. return &allowListLogConsumer{
  29. allowList: allowed,
  30. delegate: consumer,
  31. }
  32. }
  33. type allowListLogConsumer struct {
  34. allowList map[string]bool
  35. delegate compose.LogConsumer
  36. }
  37. func (a *allowListLogConsumer) Log(name, container, message string) {
  38. if a.allowList[name] {
  39. a.delegate.Log(name, container, message)
  40. }
  41. }
  42. func (a *allowListLogConsumer) Status(name, container, message string) {
  43. if a.allowList[name] {
  44. a.delegate.Status(name, container, message)
  45. }
  46. }
  47. func (a *allowListLogConsumer) Register(name string, source string) {
  48. if a.allowList[name] {
  49. a.delegate.Register(name, source)
  50. }
  51. }
  52. // GetWriter creates a io.Writer that will actually split by line and format by LogConsumer
  53. func GetWriter(name, service, container string, events compose.ContainerEventListener) io.Writer {
  54. return &splitBuffer{
  55. buffer: bytes.Buffer{},
  56. name: name,
  57. service: service,
  58. container: container,
  59. consumer: events,
  60. }
  61. }
  62. type splitBuffer struct {
  63. buffer bytes.Buffer
  64. name string
  65. service string
  66. container string
  67. consumer compose.ContainerEventListener
  68. }
  69. // Write implements io.Writer. joins all input, splits on the separator and yields each chunk
  70. func (s *splitBuffer) Write(b []byte) (int, error) {
  71. n, err := s.buffer.Write(b)
  72. if err != nil {
  73. return n, err
  74. }
  75. for {
  76. b = s.buffer.Bytes()
  77. index := bytes.Index(b, []byte{'\n'})
  78. if index < 0 {
  79. break
  80. }
  81. line := s.buffer.Next(index + 1)
  82. s.consumer(compose.ContainerEvent{
  83. Type: compose.ContainerEventLog,
  84. Name: s.name,
  85. Service: s.service,
  86. Source: s.container,
  87. Line: string(line[:len(line)-1]),
  88. })
  89. }
  90. return n, nil
  91. }