logs.go 2.6 KB

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