logging.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Discordgo - Discord bindings for Go
  2. // Available at https://github.com/bwmarrin/discordgo
  3. // Copyright 2015-2016 Bruce Marriner <bruce@sqls.net>. All rights reserved.
  4. // Use of this source code is governed by a BSD-style
  5. // license that can be found in the LICENSE file.
  6. // This file contains code related to discordgo package logging
  7. package discordgo
  8. import (
  9. "fmt"
  10. "log"
  11. "runtime"
  12. "strings"
  13. )
  14. const (
  15. // LogError level is used for critical errors that could lead to data loss
  16. // or panic that would not be returned to a calling function.
  17. LogError int = iota
  18. // LogWarning level is used for very abnormal events and errors that are
  19. // also returned to a calling function.
  20. LogWarning
  21. // LogInformational level is used for normal non-error activity
  22. LogInformational
  23. // LogDebug level is for very detailed non-error activity. This is
  24. // very spammy and will impact performance.
  25. LogDebug
  26. )
  27. // Logger can be used to replace the standard logging for discordgo
  28. var Logger func(msgL, caller int, format string, a ...interface{})
  29. // msglog provides package wide logging consistency for discordgo
  30. // the format, a... portion this command follows that of fmt.Printf
  31. // msgL : LogLevel of the message
  32. // caller : 1 + the number of callers away from the message source
  33. // format : Printf style message format
  34. // a ... : comma separated list of values to pass
  35. func msglog(msgL, caller int, format string, a ...interface{}) {
  36. if Logger != nil {
  37. Logger(msgL, caller, format, a...)
  38. } else {
  39. pc, file, line, _ := runtime.Caller(caller)
  40. files := strings.Split(file, "/")
  41. file = files[len(files)-1]
  42. name := runtime.FuncForPC(pc).Name()
  43. fns := strings.Split(name, ".")
  44. name = fns[len(fns)-1]
  45. msg := fmt.Sprintf(format, a...)
  46. log.Printf("[DG%d] %s:%d:%s() %s\n", msgL, file, line, name, msg)
  47. }
  48. }
  49. // helper function that wraps msglog for the Session struct
  50. // This adds a check to insure the message is only logged
  51. // if the session log level is equal or higher than the
  52. // message log level
  53. func (s *Session) log(msgL int, format string, a ...interface{}) {
  54. if msgL > s.LogLevel {
  55. return
  56. }
  57. msglog(msgL, 2, format, a...)
  58. }
  59. // helper function that wraps msglog for the VoiceConnection struct
  60. // This adds a check to insure the message is only logged
  61. // if the voice connection log level is equal or higher than the
  62. // message log level
  63. func (v *VoiceConnection) log(msgL int, format string, a ...interface{}) {
  64. if msgL > v.LogLevel {
  65. return
  66. }
  67. msglog(msgL, 2, format, a...)
  68. }
  69. // printJSON is a helper function to display JSON data in a easy to read format.
  70. /* NOT USED ATM
  71. func printJSON(body []byte) {
  72. var prettyJSON bytes.Buffer
  73. error := json.Indent(&prettyJSON, body, "", "\t")
  74. if error != nil {
  75. log.Print("JSON parse error: ", error)
  76. }
  77. log.Println(string(prettyJSON.Bytes()))
  78. }
  79. */