Logging code cleanup

This commit is contained in:
Bruce Marriner 2016-04-28 08:36:04 -05:00
parent df818b5931
commit 609e7ad682

View file

@ -20,30 +20,33 @@ import (
const ( const (
// Logs critical errors that can lead to data loss or panic // Critical Errors that could lead to data loss or panic
// also, only logs errors that would never be returned to // Only errors that would not be returned to a calling function
// a calling function. Such as errors within goroutines.
LogError int = iota LogError int = iota
// Logs very abnormal events even if they're also returend as // Very abnormal events.
// an error to the calling code. // Errors that are also returend to a calling function.
LogWarning LogWarning
// Logs normal non-error activity like connect/disconnects // Normal non-error activity
// Generally, not overly spammy events
LogInformational LogInformational
// Logs detailed activity including all HTTP/Websocket packets. // Detailed activity
// All HTTP/Websocket packets.
// Very spammy and will impact performance
LogDebug LogDebug
) )
// logs messages to stderr // msglog provides package wide logging consistancy for discordgo
func msglog(cfgL, msgL int, format string, a ...interface{}) { // the format, a... portion this command follows that of fmt.Printf
// msgL : LogLevel of the message
// caller : 1 + the number of callers away from the message source
// format : Printf style message format
// a ... : comma seperated list of values to pass
func msglog(msgL, caller int, format string, a ...interface{}) {
if msgL > cfgL { pc, file, line, _ := runtime.Caller(caller)
return
}
pc, file, line, _ := runtime.Caller(1)
files := strings.Split(file, "/") files := strings.Split(file, "/")
file = files[len(files)-1] file = files[len(files)-1]
@ -54,26 +57,41 @@ func msglog(cfgL, msgL int, format string, a ...interface{}) {
msg := fmt.Sprintf(format, a...) msg := fmt.Sprintf(format, a...)
log.Printf("%s:%d:%s %s\n", file, line, name, msg) log.Printf("[DG%d] %s:%d %s %s\n", msgL, file, line, name, msg)
} }
// helper function that wraps msglog for the Session struct // helper function that wraps msglog for the Session struct
// This adds a check to insure the message is only logged
// if the session log level is equal or higher than the
// message log level
func (s *Session) log(msgL int, format string, a ...interface{}) { func (s *Session) log(msgL int, format string, a ...interface{}) {
if s.Debug { // Deprecated if s.Debug { // Deprecated
s.LogLevel = LogDebug s.LogLevel = LogDebug
} }
msglog(s.LogLevel, msgL, format, a...)
if msgL > s.LogLevel {
return
}
msglog(msgL, 2, format, a...)
} }
// helper function that wraps msglog for the VoiceConnection struct // helper function that wraps msglog for the VoiceConnection struct
// This adds a check to insure the message is only logged
// if the voice connection log level is equal or higher than the
// message log level
func (v *VoiceConnection) log(msgL int, format string, a ...interface{}) { func (v *VoiceConnection) log(msgL int, format string, a ...interface{}) {
if v.Debug { // Deprecated if v.Debug { // Deprecated
v.LogLevel = LogDebug v.LogLevel = LogDebug
} }
msglog(v.LogLevel, msgL, format, a...) if msgL > v.LogLevel {
return
}
msglog(msgL, 2, format, a...)
} }
// printEvent prints out a WSAPI event. // printEvent prints out a WSAPI event.