reporter.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package check
  2. import (
  3. "fmt"
  4. "io"
  5. "sync"
  6. )
  7. // -----------------------------------------------------------------------
  8. // Output writer manages atomic output writing according to settings.
  9. type outputWriter struct {
  10. m sync.Mutex
  11. writer io.Writer
  12. wroteCallProblemLast bool
  13. Stream bool
  14. Verbose bool
  15. }
  16. func newOutputWriter(writer io.Writer, stream, verbose bool) *outputWriter {
  17. return &outputWriter{writer: writer, Stream: stream, Verbose: verbose}
  18. }
  19. func (ow *outputWriter) Write(content []byte) (n int, err error) {
  20. ow.m.Lock()
  21. n, err = ow.writer.Write(content)
  22. ow.m.Unlock()
  23. return
  24. }
  25. func (ow *outputWriter) WriteCallStarted(label string, c *C) {
  26. if ow.Stream {
  27. header := renderCallHeader(label, c, "", "\n")
  28. ow.m.Lock()
  29. ow.writer.Write([]byte(header))
  30. ow.m.Unlock()
  31. }
  32. }
  33. func (ow *outputWriter) WriteCallProblem(label string, c *C) {
  34. var prefix string
  35. if !ow.Stream {
  36. prefix = "\n-----------------------------------" +
  37. "-----------------------------------\n"
  38. }
  39. header := renderCallHeader(label, c, prefix, "\n\n")
  40. ow.m.Lock()
  41. ow.wroteCallProblemLast = true
  42. ow.writer.Write([]byte(header))
  43. if !ow.Stream {
  44. c.logb.WriteTo(ow.writer)
  45. }
  46. ow.m.Unlock()
  47. }
  48. func (ow *outputWriter) WriteCallSuccess(label string, c *C) {
  49. if ow.Stream || (ow.Verbose && c.kind == testKd) {
  50. // TODO Use a buffer here.
  51. var suffix string
  52. if c.reason != "" {
  53. suffix = " (" + c.reason + ")"
  54. }
  55. if c.status() == succeededSt {
  56. suffix += "\t" + c.timerString()
  57. }
  58. suffix += "\n"
  59. if ow.Stream {
  60. suffix += "\n"
  61. }
  62. header := renderCallHeader(label, c, "", suffix)
  63. ow.m.Lock()
  64. // Resist temptation of using line as prefix above due to race.
  65. if !ow.Stream && ow.wroteCallProblemLast {
  66. header = "\n-----------------------------------" +
  67. "-----------------------------------\n" +
  68. header
  69. }
  70. ow.wroteCallProblemLast = false
  71. ow.writer.Write([]byte(header))
  72. ow.m.Unlock()
  73. }
  74. }
  75. func renderCallHeader(label string, c *C, prefix, suffix string) string {
  76. pc := c.method.PC()
  77. return fmt.Sprintf("%s%s: %s: %s%s", prefix, label, niceFuncPath(pc),
  78. niceFuncName(pc), suffix)
  79. }