logs.go 2.7 KB

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