copier.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package logger
  2. import (
  3. "bytes"
  4. "io"
  5. "sync"
  6. "time"
  7. "github.com/Sirupsen/logrus"
  8. )
  9. const (
  10. bufSize = 16 * 1024
  11. readSize = 2 * 1024
  12. )
  13. // Copier can copy logs from specified sources to Logger and attach Timestamp.
  14. // Writes are concurrent, so you need implement some sync in your logger.
  15. type Copier struct {
  16. // srcs is map of name -> reader pairs, for example "stdout", "stderr"
  17. srcs map[string]io.Reader
  18. dst Logger
  19. copyJobs sync.WaitGroup
  20. closeOnce sync.Once
  21. closed chan struct{}
  22. }
  23. // NewCopier creates a new Copier
  24. func NewCopier(srcs map[string]io.Reader, dst Logger) *Copier {
  25. return &Copier{
  26. srcs: srcs,
  27. dst: dst,
  28. closed: make(chan struct{}),
  29. }
  30. }
  31. // Run starts logs copying
  32. func (c *Copier) Run() {
  33. for src, w := range c.srcs {
  34. c.copyJobs.Add(1)
  35. go c.copySrc(src, w)
  36. }
  37. }
  38. func (c *Copier) copySrc(name string, src io.Reader) {
  39. defer c.copyJobs.Done()
  40. buf := make([]byte, bufSize)
  41. n := 0
  42. eof := false
  43. msg := &Message{Source: name}
  44. for {
  45. select {
  46. case <-c.closed:
  47. return
  48. default:
  49. // Work out how much more data we are okay with reading this time.
  50. upto := n + readSize
  51. if upto > cap(buf) {
  52. upto = cap(buf)
  53. }
  54. // Try to read that data.
  55. if upto > n {
  56. read, err := src.Read(buf[n:upto])
  57. if err != nil {
  58. if err != io.EOF {
  59. logrus.Errorf("Error scanning log stream: %s", err)
  60. return
  61. }
  62. eof = true
  63. }
  64. n += read
  65. }
  66. // If we have no data to log, and there's no more coming, we're done.
  67. if n == 0 && eof {
  68. return
  69. }
  70. // Break up the data that we've buffered up into lines, and log each in turn.
  71. p := 0
  72. for q := bytes.Index(buf[p:n], []byte{'\n'}); q >= 0; q = bytes.Index(buf[p:n], []byte{'\n'}) {
  73. msg.Line = buf[p : p+q]
  74. msg.Timestamp = time.Now().UTC()
  75. msg.Partial = false
  76. select {
  77. case <-c.closed:
  78. return
  79. default:
  80. if logErr := c.dst.Log(msg); logErr != nil {
  81. logrus.Errorf("Failed to log msg %q for logger %s: %s", msg.Line, c.dst.Name(), logErr)
  82. }
  83. }
  84. p += q + 1
  85. }
  86. // If there's no more coming, or the buffer is full but
  87. // has no newlines, log whatever we haven't logged yet,
  88. // noting that it's a partial log line.
  89. if eof || (p == 0 && n == len(buf)) {
  90. if p < n {
  91. msg.Line = buf[p:n]
  92. msg.Timestamp = time.Now().UTC()
  93. msg.Partial = true
  94. if logErr := c.dst.Log(msg); logErr != nil {
  95. logrus.Errorf("Failed to log msg %q for logger %s: %s", msg.Line, c.dst.Name(), logErr)
  96. }
  97. p = 0
  98. n = 0
  99. }
  100. if eof {
  101. return
  102. }
  103. }
  104. // Move any unlogged data to the front of the buffer in preparation for another read.
  105. if p > 0 {
  106. copy(buf[0:], buf[p:n])
  107. n -= p
  108. }
  109. }
  110. }
  111. }
  112. // Wait waits until all copying is done
  113. func (c *Copier) Wait() {
  114. c.copyJobs.Wait()
  115. }
  116. // Close closes the copier
  117. func (c *Copier) Close() {
  118. c.closeOnce.Do(func() {
  119. close(c.closed)
  120. })
  121. }