logging.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. "bytes"
  10. "encoding/json"
  11. "fmt"
  12. "log"
  13. "runtime"
  14. "strings"
  15. )
  16. const (
  17. // Critical Errors that could lead to data loss or panic
  18. // Only errors that would not be returned to a calling function
  19. LogError int = iota
  20. // Very abnormal events.
  21. // Errors that are also returend to a calling function.
  22. LogWarning
  23. // Normal non-error activity
  24. // Generally, not overly spammy events
  25. LogInformational
  26. // Very detailed non-error activity
  27. // All HTTP/Websocket packets.
  28. // Very spammy and will impact performance
  29. LogDebug
  30. )
  31. // msglog provides package wide logging consistancy for discordgo
  32. // the format, a... portion this command follows that of fmt.Printf
  33. // msgL : LogLevel of the message
  34. // caller : 1 + the number of callers away from the message source
  35. // format : Printf style message format
  36. // a ... : comma seperated list of values to pass
  37. func msglog(msgL, caller int, format string, a ...interface{}) {
  38. pc, file, line, _ := runtime.Caller(caller)
  39. files := strings.Split(file, "/")
  40. file = files[len(files)-1]
  41. name := runtime.FuncForPC(pc).Name()
  42. fns := strings.Split(name, ".")
  43. name = fns[len(fns)-1]
  44. msg := fmt.Sprintf(format, a...)
  45. log.Printf("[DG%d] %s:%d:%s() %s\n", msgL, file, line, name, msg)
  46. }
  47. // helper function that wraps msglog for the Session struct
  48. // This adds a check to insure the message is only logged
  49. // if the session log level is equal or higher than the
  50. // message log level
  51. func (s *Session) log(msgL int, format string, a ...interface{}) {
  52. if s.Debug { // Deprecated
  53. s.LogLevel = LogDebug
  54. }
  55. if msgL > s.LogLevel {
  56. return
  57. }
  58. msglog(msgL, 2, format, a...)
  59. }
  60. // helper function that wraps msglog for the VoiceConnection struct
  61. // This adds a check to insure the message is only logged
  62. // if the voice connection log level is equal or higher than the
  63. // message log level
  64. func (v *VoiceConnection) log(msgL int, format string, a ...interface{}) {
  65. if v.Debug { // Deprecated
  66. v.LogLevel = LogDebug
  67. }
  68. if msgL > v.LogLevel {
  69. return
  70. }
  71. msglog(msgL, 2, format, a...)
  72. }
  73. // printJSON is a helper function to display JSON data in a easy to read format.
  74. func printJSON(body []byte) {
  75. var prettyJSON bytes.Buffer
  76. error := json.Indent(&prettyJSON, body, "", "\t")
  77. if error != nil {
  78. log.Print("JSON parse error: ", error)
  79. }
  80. log.Println(string(prettyJSON.Bytes()))
  81. }