logs.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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(container, service, message string) {
  38. if a.allowList[service] {
  39. a.delegate.Log(container, service, message)
  40. }
  41. }
  42. func (a *allowListLogConsumer) Status(container, message string) {
  43. if a.allowList[container] {
  44. a.delegate.Status(container, message)
  45. }
  46. }
  47. func (a *allowListLogConsumer) Register(name string) {
  48. if a.allowList[name] {
  49. a.delegate.Register(name)
  50. }
  51. }
  52. // GetWriter creates a io.Writer that will actually split by line and format by LogConsumer
  53. func GetWriter(container, service string, events compose.ContainerEventListener) io.Writer {
  54. return &splitBuffer{
  55. buffer: bytes.Buffer{},
  56. service: service,
  57. container: container,
  58. consumer: events,
  59. }
  60. }
  61. type splitBuffer struct {
  62. buffer bytes.Buffer
  63. service string
  64. container string
  65. consumer compose.ContainerEventListener
  66. }
  67. // Write implements io.Writer. joins all input, splits on the separator and yields each chunk
  68. func (s *splitBuffer) Write(b []byte) (int, error) {
  69. n, err := s.buffer.Write(b)
  70. if err != nil {
  71. return n, err
  72. }
  73. for {
  74. b = s.buffer.Bytes()
  75. index := bytes.Index(b, []byte{'\n'})
  76. if index < 0 {
  77. break
  78. }
  79. line := s.buffer.Next(index + 1)
  80. s.consumer(compose.ContainerEvent{
  81. Type: compose.ContainerEventLog,
  82. Service: s.service,
  83. Container: s.container,
  84. Line: string(line[:len(line)-1]),
  85. })
  86. }
  87. return n, nil
  88. }