logging.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 returend 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. // msglog provides package wide logging consistancy for discordgo
  28. // the format, a... portion this command follows that of fmt.Printf
  29. // msgL : LogLevel of the message
  30. // caller : 1 + the number of callers away from the message source
  31. // format : Printf style message format
  32. // a ... : comma seperated list of values to pass
  33. func msglog(msgL, caller int, format string, a ...interface{}) {
  34. pc, file, line, _ := runtime.Caller(caller)
  35. files := strings.Split(file, "/")
  36. file = files[len(files)-1]
  37. name := runtime.FuncForPC(pc).Name()
  38. fns := strings.Split(name, ".")
  39. name = fns[len(fns)-1]
  40. msg := fmt.Sprintf(format, a...)
  41. log.Printf("[DG%d] %s:%d:%s() %s\n", msgL, file, line, name, msg)
  42. }
  43. // helper function that wraps msglog for the Session struct
  44. // This adds a check to insure the message is only logged
  45. // if the session log level is equal or higher than the
  46. // message log level
  47. func (s *Session) log(msgL int, format string, a ...interface{}) {
  48. if msgL > s.LogLevel {
  49. return
  50. }
  51. msglog(msgL, 2, format, a...)
  52. }
  53. // helper function that wraps msglog for the VoiceConnection struct
  54. // This adds a check to insure the message is only logged
  55. // if the voice connection log level is equal or higher than the
  56. // message log level
  57. func (v *VoiceConnection) log(msgL int, format string, a ...interface{}) {
  58. if msgL > v.LogLevel {
  59. return
  60. }
  61. msglog(msgL, 2, format, a...)
  62. }
  63. // printJSON is a helper function to display JSON data in a easy to read format.
  64. /* NOT USED ATM
  65. func printJSON(body []byte) {
  66. var prettyJSON bytes.Buffer
  67. error := json.Indent(&prettyJSON, body, "", "\t")
  68. if error != nil {
  69. log.Print("JSON parse error: ", error)
  70. }
  71. log.Println(string(prettyJSON.Bytes()))
  72. }
  73. */