logs.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. "time"
  22. "github.com/buger/goterm"
  23. "github.com/docker/compose/v2/pkg/api"
  24. "github.com/docker/docker/pkg/jsonmessage"
  25. )
  26. // LogConsumer consume logs from services and format them
  27. type logConsumer struct {
  28. ctx context.Context
  29. presenters sync.Map // map[string]*presenter
  30. width int
  31. stdout io.Writer
  32. stderr io.Writer
  33. color bool
  34. prefix bool
  35. timestamp bool
  36. }
  37. // NewLogConsumer creates a new LogConsumer
  38. func NewLogConsumer(ctx context.Context, stdout, stderr io.Writer, color, prefix, timestamp bool) api.LogConsumer {
  39. return &logConsumer{
  40. ctx: ctx,
  41. presenters: sync.Map{},
  42. width: 0,
  43. stdout: stdout,
  44. stderr: stderr,
  45. color: color,
  46. prefix: prefix,
  47. timestamp: timestamp,
  48. }
  49. }
  50. func (l *logConsumer) Register(name string) {
  51. l.register(name)
  52. }
  53. func (l *logConsumer) register(name string) *presenter {
  54. var p *presenter
  55. root, _, found := strings.Cut(name, " ")
  56. if found {
  57. parent := l.getPresenter(root)
  58. p = &presenter{
  59. colors: parent.colors,
  60. name: name,
  61. prefix: parent.prefix,
  62. }
  63. } else {
  64. cf := monochrome
  65. if l.color {
  66. if name == api.WatchLogger {
  67. cf = makeColorFunc("92")
  68. } else {
  69. cf = nextColor()
  70. }
  71. }
  72. p = &presenter{
  73. colors: cf,
  74. name: name,
  75. }
  76. }
  77. l.presenters.Store(name, p)
  78. l.computeWidth()
  79. if l.prefix {
  80. l.presenters.Range(func(key, value interface{}) bool {
  81. p := value.(*presenter)
  82. p.setPrefix(l.width)
  83. return true
  84. })
  85. }
  86. return p
  87. }
  88. func (l *logConsumer) getPresenter(container string) *presenter {
  89. p, ok := l.presenters.Load(container)
  90. if !ok { // should have been registered, but ¯\_(ツ)_/¯
  91. return l.register(container)
  92. }
  93. return p.(*presenter)
  94. }
  95. // Log formats a log message as received from name/container
  96. func (l *logConsumer) Log(container, message string) {
  97. l.write(l.stdout, container, message)
  98. }
  99. // Err formats a log message as received from name/container
  100. func (l *logConsumer) Err(container, message string) {
  101. l.write(l.stderr, container, message)
  102. }
  103. func (l *logConsumer) write(w io.Writer, container, message string) {
  104. if l.ctx.Err() != nil {
  105. return
  106. }
  107. if KeyboardManager != nil {
  108. KeyboardManager.ClearKeyboardInfo()
  109. }
  110. p := l.getPresenter(container)
  111. timestamp := time.Now().Format(jsonmessage.RFC3339NanoFixed)
  112. for _, line := range strings.Split(message, "\n") {
  113. if l.timestamp {
  114. _, _ = fmt.Fprintf(w, "%s%s%s\n", p.prefix, timestamp, line)
  115. } else {
  116. _, _ = fmt.Fprintf(w, "%s%s\n", p.prefix, line)
  117. }
  118. }
  119. if KeyboardManager != nil {
  120. KeyboardManager.PrintKeyboardInfo()
  121. }
  122. }
  123. func (l *logConsumer) Status(container, msg string) {
  124. p := l.getPresenter(container)
  125. s := p.colors(fmt.Sprintf("%s%s %s\n", goterm.RESET_LINE, container, msg))
  126. l.stdout.Write([]byte(s)) //nolint:errcheck
  127. }
  128. func (l *logConsumer) computeWidth() {
  129. width := 0
  130. l.presenters.Range(func(key, value interface{}) bool {
  131. p := value.(*presenter)
  132. if len(p.name) > width {
  133. width = len(p.name)
  134. }
  135. return true
  136. })
  137. l.width = width + 1
  138. }
  139. type presenter struct {
  140. colors colorFunc
  141. name string
  142. prefix string
  143. }
  144. func (p *presenter) setPrefix(width int) {
  145. if p.name == api.WatchLogger {
  146. p.prefix = p.colors(strings.Repeat(" ", width) + " ⦿ ")
  147. return
  148. }
  149. p.prefix = p.colors(fmt.Sprintf("%-"+strconv.Itoa(width)+"s | ", p.name))
  150. }