1
0

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