write_log_stream.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package httputils
  2. import (
  3. "fmt"
  4. "io"
  5. "sort"
  6. "strings"
  7. "golang.org/x/net/context"
  8. "github.com/docker/docker/api/types"
  9. "github.com/docker/docker/api/types/backend"
  10. "github.com/docker/docker/pkg/ioutils"
  11. "github.com/docker/docker/pkg/jsonlog"
  12. "github.com/docker/docker/pkg/stdcopy"
  13. )
  14. // WriteLogStream writes an encoded byte stream of log messages from the
  15. // messages channel, multiplexing them with a stdcopy.Writer if mux is true
  16. func WriteLogStream(ctx context.Context, w io.Writer, msgs <-chan *backend.LogMessage, config *types.ContainerLogsOptions, mux bool) {
  17. wf := ioutils.NewWriteFlusher(w)
  18. defer wf.Close()
  19. wf.Flush()
  20. // this might seem like doing below is clear:
  21. // var outStream io.Writer = wf
  22. // however, this GREATLY DISPLEASES golint, and if you do that, it will
  23. // fail CI. we need outstream to be type writer because if we mux streams,
  24. // we will need to reassign all of the streams to be stdwriters, which only
  25. // conforms to the io.Writer interface.
  26. var outStream io.Writer
  27. outStream = wf
  28. errStream := outStream
  29. sysErrStream := errStream
  30. if mux {
  31. sysErrStream = stdcopy.NewStdWriter(outStream, stdcopy.Systemerr)
  32. errStream = stdcopy.NewStdWriter(outStream, stdcopy.Stderr)
  33. outStream = stdcopy.NewStdWriter(outStream, stdcopy.Stdout)
  34. }
  35. for {
  36. msg, ok := <-msgs
  37. if !ok {
  38. return
  39. }
  40. // check if the message contains an error. if so, write that error
  41. // and exit
  42. if msg.Err != nil {
  43. fmt.Fprintf(sysErrStream, "Error grabbing logs: %v\n", msg.Err)
  44. continue
  45. }
  46. logLine := msg.Line
  47. if config.Details {
  48. logLine = append([]byte(stringAttrs(msg.Attrs)+" "), logLine...)
  49. }
  50. if config.Timestamps {
  51. // TODO(dperny) the format is defined in
  52. // daemon/logger/logger.go as logger.TimeFormat. importing
  53. // logger is verboten (not part of backend) so idk if just
  54. // importing the same thing from jsonlog is good enough
  55. logLine = append([]byte(msg.Timestamp.Format(jsonlog.RFC3339NanoFixed)+" "), logLine...)
  56. }
  57. if msg.Source == "stdout" && config.ShowStdout {
  58. outStream.Write(logLine)
  59. }
  60. if msg.Source == "stderr" && config.ShowStderr {
  61. errStream.Write(logLine)
  62. }
  63. }
  64. }
  65. type byKey []string
  66. func (s byKey) Len() int { return len(s) }
  67. func (s byKey) Less(i, j int) bool {
  68. keyI := strings.Split(s[i], "=")
  69. keyJ := strings.Split(s[j], "=")
  70. return keyI[0] < keyJ[0]
  71. }
  72. func (s byKey) Swap(i, j int) {
  73. s[i], s[j] = s[j], s[i]
  74. }
  75. func stringAttrs(a backend.LogAttributes) string {
  76. var ss byKey
  77. for k, v := range a {
  78. ss = append(ss, k+"="+v)
  79. }
  80. sort.Sort(ss)
  81. return strings.Join(ss, ",")
  82. }