/logger/logger.go

https://code.google.com/p/gobuild/ · Go · 100 lines · 52 code · 13 blank · 35 comment · 7 complexity · 585cf3f08a98086d6c5dffc568dbaf80 MD5 · raw file

  1. // Copyright 2009-2012 by Maurice Gilden. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. /*
  5. A collection of helper functions for console output.
  6. */
  7. package logger
  8. import "fmt"
  9. import "os"
  10. const (
  11. DEBUG = -1 + iota // start with -1
  12. DEFAULT // should be 0
  13. WARN
  14. ERROR
  15. )
  16. var verbosity int
  17. /*
  18. Sets the verbosity level. Possible values are DEBUG, DEFAULT, WARN and ERROR.
  19. DEFAULT is the standard value which will print everything but debug messages.
  20. */
  21. func SetVerbosityLevel(level int) { verbosity = level }
  22. /*
  23. Prints debug messages. Same syntax as fmt.Printf.
  24. */
  25. func Debug(format string, v ...interface{}) {
  26. if verbosity <= DEBUG {
  27. fmt.Printf("DEBUG: ")
  28. fmt.Printf(format, v...)
  29. }
  30. }
  31. /*
  32. Prints a debug message if enabled but without the "DEBUG: " prefix.
  33. Same syntax as fmt.Printf.
  34. */
  35. func DebugContinue(format string, v ...interface{}) {
  36. if verbosity <= DEBUG {
  37. fmt.Printf(" ")
  38. fmt.Printf(format, v...)
  39. }
  40. }
  41. /*
  42. Prints an info message if enabled. This is for general feedback about what
  43. gobuild is currently doing. Same syntax as fmt.Printf.
  44. */
  45. func Info(format string, v ...interface{}) {
  46. if verbosity <= DEFAULT {
  47. fmt.Printf(format, v...)
  48. }
  49. }
  50. /*
  51. Prints a warning if warnings are enabled. Same syntax as fmt.Printf.
  52. */
  53. func Warn(format string, v ...interface{}) {
  54. if verbosity <= WARN {
  55. fmt.Print("WARNING: ")
  56. fmt.Printf(format, v...)
  57. }
  58. }
  59. /*
  60. Prints a warning message if enabled but without the "WARNING: " prefix.
  61. Same syntax as fmt.Printf.
  62. */
  63. func WarnContinue(format string, v ...interface{}) {
  64. if verbosity <= WARN {
  65. fmt.Print(" ")
  66. fmt.Printf(format, v...)
  67. }
  68. }
  69. /*
  70. Prints an error message. Same syntax as fmt.Printf.
  71. */
  72. func Error(format string, v ...interface{}) {
  73. if verbosity <= ERROR {
  74. fmt.Fprint(os.Stderr, "ERROR: ")
  75. fmt.Fprintf(os.Stderr, format, v...)
  76. }
  77. }
  78. /*
  79. Prints an error message but without the "ERROR: " prefix.
  80. Same syntax as fmt.Printf.
  81. */
  82. func ErrorContinue(format string, v ...interface{}) {
  83. if verbosity <= ERROR {
  84. fmt.Fprint(os.Stderr, " ")
  85. fmt.Fprintf(os.Stderr, format, v...)
  86. }
  87. }