本站源代码
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

109 lines
3.1KB

  1. // Package pretty provides pretty-printing for Go values. This is
  2. // useful during debugging, to avoid wrapping long output lines in
  3. // the terminal.
  4. //
  5. // It provides a function, Formatter, that can be used with any
  6. // function that accepts a format string. It also provides
  7. // convenience wrappers for functions in packages fmt and log.
  8. package pretty
  9. import (
  10. "fmt"
  11. "io"
  12. "log"
  13. "reflect"
  14. )
  15. // Errorf is a convenience wrapper for fmt.Errorf.
  16. //
  17. // Calling Errorf(f, x, y) is equivalent to
  18. // fmt.Errorf(f, Formatter(x), Formatter(y)).
  19. func Errorf(format string, a ...interface{}) error {
  20. return fmt.Errorf(format, wrap(a, false)...)
  21. }
  22. // Fprintf is a convenience wrapper for fmt.Fprintf.
  23. //
  24. // Calling Fprintf(w, f, x, y) is equivalent to
  25. // fmt.Fprintf(w, f, Formatter(x), Formatter(y)).
  26. func Fprintf(w io.Writer, format string, a ...interface{}) (n int, error error) {
  27. return fmt.Fprintf(w, format, wrap(a, false)...)
  28. }
  29. // Log is a convenience wrapper for log.Printf.
  30. //
  31. // Calling Log(x, y) is equivalent to
  32. // log.Print(Formatter(x), Formatter(y)), but each operand is
  33. // formatted with "%# v".
  34. func Log(a ...interface{}) {
  35. log.Print(wrap(a, true)...)
  36. }
  37. // Logf is a convenience wrapper for log.Printf.
  38. //
  39. // Calling Logf(f, x, y) is equivalent to
  40. // log.Printf(f, Formatter(x), Formatter(y)).
  41. func Logf(format string, a ...interface{}) {
  42. log.Printf(format, wrap(a, false)...)
  43. }
  44. // Logln is a convenience wrapper for log.Printf.
  45. //
  46. // Calling Logln(x, y) is equivalent to
  47. // log.Println(Formatter(x), Formatter(y)), but each operand is
  48. // formatted with "%# v".
  49. func Logln(a ...interface{}) {
  50. log.Println(wrap(a, true)...)
  51. }
  52. // Print pretty-prints its operands and writes to standard output.
  53. //
  54. // Calling Print(x, y) is equivalent to
  55. // fmt.Print(Formatter(x), Formatter(y)), but each operand is
  56. // formatted with "%# v".
  57. func Print(a ...interface{}) (n int, errno error) {
  58. return fmt.Print(wrap(a, true)...)
  59. }
  60. // Printf is a convenience wrapper for fmt.Printf.
  61. //
  62. // Calling Printf(f, x, y) is equivalent to
  63. // fmt.Printf(f, Formatter(x), Formatter(y)).
  64. func Printf(format string, a ...interface{}) (n int, errno error) {
  65. return fmt.Printf(format, wrap(a, false)...)
  66. }
  67. // Println pretty-prints its operands and writes to standard output.
  68. //
  69. // Calling Print(x, y) is equivalent to
  70. // fmt.Println(Formatter(x), Formatter(y)), but each operand is
  71. // formatted with "%# v".
  72. func Println(a ...interface{}) (n int, errno error) {
  73. return fmt.Println(wrap(a, true)...)
  74. }
  75. // Sprint is a convenience wrapper for fmt.Sprintf.
  76. //
  77. // Calling Sprint(x, y) is equivalent to
  78. // fmt.Sprint(Formatter(x), Formatter(y)), but each operand is
  79. // formatted with "%# v".
  80. func Sprint(a ...interface{}) string {
  81. return fmt.Sprint(wrap(a, true)...)
  82. }
  83. // Sprintf is a convenience wrapper for fmt.Sprintf.
  84. //
  85. // Calling Sprintf(f, x, y) is equivalent to
  86. // fmt.Sprintf(f, Formatter(x), Formatter(y)).
  87. func Sprintf(format string, a ...interface{}) string {
  88. return fmt.Sprintf(format, wrap(a, false)...)
  89. }
  90. func wrap(a []interface{}, force bool) []interface{} {
  91. w := make([]interface{}, len(a))
  92. for i, x := range a {
  93. w[i] = formatter{v: reflect.ValueOf(x), force: force}
  94. }
  95. return w
  96. }
上海开阖软件有限公司 沪ICP备12045867号-1