logs.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 formatter
  14. import (
  15. "context"
  16. "fmt"
  17. "io"
  18. "strconv"
  19. "strings"
  20. "sync"
  21. "github.com/docker/compose/v2/pkg/api"
  22. )
  23. // LogConsumer consume logs from services and format them
  24. type logConsumer struct {
  25. ctx context.Context
  26. presenters sync.Map // map[string]*presenter
  27. width int
  28. stdout io.Writer
  29. stderr io.Writer
  30. color bool
  31. prefix bool
  32. }
  33. // NewLogConsumer creates a new LogConsumer
  34. func NewLogConsumer(ctx context.Context, stdout, stderr io.Writer, color bool, prefix bool) api.LogConsumer {
  35. return &logConsumer{
  36. ctx: ctx,
  37. presenters: sync.Map{},
  38. width: 0,
  39. stdout: stdout,
  40. stderr: stderr,
  41. color: color,
  42. prefix: prefix,
  43. }
  44. }
  45. func (l *logConsumer) Register(name string) {
  46. l.register(name)
  47. }
  48. func (l *logConsumer) register(name string) *presenter {
  49. cf := monochrome
  50. if l.color {
  51. cf = nextColor()
  52. }
  53. p := &presenter{
  54. colors: cf,
  55. name: name,
  56. }
  57. l.presenters.Store(name, p)
  58. if l.prefix {
  59. l.computeWidth()
  60. l.presenters.Range(func(key, value interface{}) bool {
  61. p := value.(*presenter)
  62. p.setPrefix(l.width)
  63. return true
  64. })
  65. }
  66. return p
  67. }
  68. func (l *logConsumer) getPresenter(container string) *presenter {
  69. p, ok := l.presenters.Load(container)
  70. if !ok { // should have been registered, but ¯\_(ツ)_/¯
  71. return l.register(container)
  72. }
  73. return p.(*presenter)
  74. }
  75. // Log formats a log message as received from name/container
  76. func (l *logConsumer) Log(container, message string) {
  77. l.write(l.stdout, container, message)
  78. }
  79. // Log formats a log message as received from name/container
  80. func (l *logConsumer) Err(container, message string) {
  81. l.write(l.stderr, container, message)
  82. }
  83. func (l *logConsumer) write(w io.Writer, container, message string) {
  84. if l.ctx.Err() != nil {
  85. return
  86. }
  87. p := l.getPresenter(container)
  88. for _, line := range strings.Split(message, "\n") {
  89. fmt.Fprintf(w, "%s%s\n", p.prefix, line)
  90. }
  91. }
  92. func (l *logConsumer) Status(container, msg string) {
  93. p := l.getPresenter(container)
  94. s := p.colors(fmt.Sprintf("%s %s\n", container, msg))
  95. l.stdout.Write([]byte(s)) //nolint:errcheck
  96. }
  97. func (l *logConsumer) computeWidth() {
  98. width := 0
  99. l.presenters.Range(func(key, value interface{}) bool {
  100. p := value.(*presenter)
  101. if len(p.name) > width {
  102. width = len(p.name)
  103. }
  104. return true
  105. })
  106. l.width = width + 1
  107. }
  108. type presenter struct {
  109. colors colorFunc
  110. name string
  111. prefix string
  112. }
  113. func (p *presenter) setPrefix(width int) {
  114. p.prefix = p.colors(fmt.Sprintf("%-"+strconv.Itoa(width)+"s | ", p.name))
  115. }