logs.go 2.8 KB

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