Mirror of github.com/gissleh/irc
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
957 B

7 years ago
  1. package irc
  2. import (
  3. "encoding/json"
  4. "log"
  5. )
  6. // DebugLogger is for
  7. type DebugLogger interface {
  8. Println(v ...interface{})
  9. }
  10. type defaultDebugLogger struct{}
  11. func (logger *defaultDebugLogger) Println(v ...interface{}) {
  12. log.Println(v...)
  13. }
  14. // EnableDebug logs all events that passes through it, ignoring killed
  15. // events. It will always include the standard handlers, but any custom
  16. // handlers defined after EnableDebug will not have their effects shown.
  17. // You may pass `nil` as a logger to use the standard log package's Println.
  18. func EnableDebug(logger DebugLogger, indented bool) {
  19. if logger != nil {
  20. logger = &defaultDebugLogger{}
  21. }
  22. Handle(func(event *Event, client *Client) {
  23. var data []byte
  24. var err error
  25. if indented {
  26. data, err = json.MarshalIndent(event, "", " ")
  27. if err != nil {
  28. return
  29. }
  30. } else {
  31. data, err = json.Marshal(event)
  32. if err != nil {
  33. return
  34. }
  35. }
  36. logger.Println(string(data))
  37. })
  38. }