logger.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // +build codegen
  2. package api
  3. import (
  4. "fmt"
  5. "io"
  6. "sync"
  7. )
  8. var debugLogger *logger
  9. var initDebugLoggerOnce sync.Once
  10. // logger provides a basic logging
  11. type logger struct {
  12. w io.Writer
  13. }
  14. // LogDebug initialize's the debug logger for the components in the api
  15. // package to log debug lines to.
  16. //
  17. // Panics if called multiple times.
  18. //
  19. // Must be used prior to any model loading or code gen.
  20. func LogDebug(w io.Writer) {
  21. var initialized bool
  22. initDebugLoggerOnce.Do(func() {
  23. debugLogger = &logger{
  24. w: w,
  25. }
  26. initialized = true
  27. })
  28. if !initialized && debugLogger != nil {
  29. panic("LogDebug called multiple times. Can only be called once")
  30. }
  31. }
  32. // Logf logs using the fmt printf pattern. Appends a new line to the end of the
  33. // logged statement.
  34. func (l *logger) Logf(format string, args ...interface{}) {
  35. if l == nil {
  36. return
  37. }
  38. fmt.Fprintf(l.w, format+"\n", args...)
  39. }
  40. // Logln logs using the fmt println pattern.
  41. func (l *logger) Logln(args ...interface{}) {
  42. if l == nil {
  43. return
  44. }
  45. fmt.Fprintln(l.w, args...)
  46. }