debug.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package raft
  2. import (
  3. "log"
  4. "os"
  5. )
  6. //------------------------------------------------------------------------------
  7. //
  8. // Variables
  9. //
  10. //------------------------------------------------------------------------------
  11. const (
  12. Debug = 1
  13. Trace = 2
  14. )
  15. var logLevel int = 0
  16. var logger *log.Logger
  17. func init() {
  18. logger = log.New(os.Stdout, "[raft]", log.Lmicroseconds)
  19. }
  20. //------------------------------------------------------------------------------
  21. //
  22. // Functions
  23. //
  24. //------------------------------------------------------------------------------
  25. func LogLevel() int {
  26. return logLevel
  27. }
  28. func SetLogLevel(level int) {
  29. logLevel = level
  30. }
  31. //--------------------------------------
  32. // Warnings
  33. //--------------------------------------
  34. // Prints to the standard logger. Arguments are handled in the manner of
  35. // fmt.Print.
  36. func warn(v ...interface{}) {
  37. logger.Print(v...)
  38. }
  39. // Prints to the standard logger. Arguments are handled in the manner of
  40. // fmt.Printf.
  41. func warnf(format string, v ...interface{}) {
  42. logger.Printf(format, v...)
  43. }
  44. // Prints to the standard logger. Arguments are handled in the manner of
  45. // fmt.Println.
  46. func warnln(v ...interface{}) {
  47. logger.Println(v...)
  48. }
  49. //--------------------------------------
  50. // Basic debugging
  51. //--------------------------------------
  52. // Prints to the standard logger if debug mode is enabled. Arguments
  53. // are handled in the manner of fmt.Print.
  54. func debug(v ...interface{}) {
  55. if logLevel >= Debug {
  56. logger.Print(v...)
  57. }
  58. }
  59. // Prints to the standard logger if debug mode is enabled. Arguments
  60. // are handled in the manner of fmt.Printf.
  61. func debugf(format string, v ...interface{}) {
  62. if logLevel >= Debug {
  63. logger.Printf(format, v...)
  64. }
  65. }
  66. // Prints to the standard logger if debug mode is enabled. Arguments
  67. // are handled in the manner of fmt.Println.
  68. func debugln(v ...interface{}) {
  69. if logLevel >= Debug {
  70. logger.Println(v...)
  71. }
  72. }
  73. //--------------------------------------
  74. // Trace-level debugging
  75. //--------------------------------------
  76. // Prints to the standard logger if trace debugging is enabled. Arguments
  77. // are handled in the manner of fmt.Print.
  78. func trace(v ...interface{}) {
  79. if logLevel >= Trace {
  80. logger.Print(v...)
  81. }
  82. }
  83. // Prints to the standard logger if trace debugging is enabled. Arguments
  84. // are handled in the manner of fmt.Printf.
  85. func tracef(format string, v ...interface{}) {
  86. if logLevel >= Trace {
  87. logger.Printf(format, v...)
  88. }
  89. }
  90. // Prints to the standard logger if trace debugging is enabled. Arguments
  91. // are handled in the manner of debugln.
  92. func traceln(v ...interface{}) {
  93. if logLevel >= Trace {
  94. logger.Println(v...)
  95. }
  96. }