Browse Source

Initial add of new logging system

This adds a LogLevel setting for both Websocket and VoiceConnections
that can be configured to set the specific log level desired. This
also adds a new logging function that adds additional helpful
information when printing out log messages.
Bruce Marriner 9 years ago
parent
commit
fc7ce3db94
4 changed files with 86 additions and 37 deletions
  1. 82 0
      logging.go
  2. 2 1
      structs.go
  3. 0 35
      util.go
  4. 2 1
      voice.go

+ 82 - 0
logging.go

@@ -0,0 +1,82 @@
+// Discordgo - Discord bindings for Go
+// Available at https://github.com/bwmarrin/discordgo
+
+// Copyright 2015-2016 Bruce Marriner <bruce@sqls.net>.  All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// This file contains code related to discordgo package logging
+
+package discordgo
+
+import (
+	"bytes"
+	"encoding/json"
+	"fmt"
+	"log"
+	"runtime"
+	"strings"
+)
+
+const (
+	LogError int = iota
+	LogWarning
+	LogNotice
+	LogDebug
+)
+
+// TODO: Merge util.go code into here
+
+// dumps debug information to stdout
+func msglog(cfgL, msgL int, format string, a ...interface{}) {
+
+	if msgL > cfgL {
+		return
+	}
+
+	pc, file, line, _ := runtime.Caller(1)
+
+	files := strings.Split(file, "/")
+	file = files[len(files)-1]
+
+	name := runtime.FuncForPC(pc).Name()
+	fns := strings.Split(name, ".")
+	name = fns[len(fns)-1]
+
+	msg := fmt.Sprintf(format, a...)
+
+	log.Printf("%s:%d:%s %s\n", file, line, name, msg)
+}
+
+func (s *Session) log(msgL int, format string, a ...interface{}) {
+
+	if s.Debug { // Deprecated
+		s.LogLevel = LogDebug
+	}
+	msglog(s.LogLevel, msgL, format, a...)
+}
+
+func (v *VoiceConnection) log(msgL int, format string, a ...interface{}) {
+
+	if v.Debug { // Deprecated
+		v.LogLevel = LogDebug
+	}
+
+	msglog(v.LogLevel, msgL, format, a...)
+}
+
+// printEvent prints out a WSAPI event.
+func printEvent(e *Event) {
+	log.Println(fmt.Sprintf("Event. Type: %s, State: %d Operation: %d Direction: %d", e.Type, e.State, e.Operation, e.Direction))
+	printJSON(e.RawData)
+}
+
+// printJSON is a helper function to display JSON data in a easy to read format.
+func printJSON(body []byte) {
+	var prettyJSON bytes.Buffer
+	error := json.Indent(&prettyJSON, body, "", "\t")
+	if error != nil {
+		log.Print("JSON parse error: ", error)
+	}
+	log.Println(string(prettyJSON.Bytes()))
+}

+ 2 - 1
structs.go

@@ -30,7 +30,8 @@ type Session struct {
 	Token string
 
 	// Debug for printing JSON request/responses
-	Debug bool
+	Debug    bool // Deprecated, will be removed.
+	LogLevel int
 
 	// Should the session reconnect the websocket on errors.
 	ShouldReconnectOnError bool

+ 0 - 35
util.go

@@ -1,35 +0,0 @@
-// Discordgo - Discord bindings for Go
-// Available at https://github.com/bwmarrin/discordgo
-
-// Copyright 2015-2016 Bruce Marriner <bruce@sqls.net>.  All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-// This file contains utility functions for the discordgo package. These
-// functions are not exported and are likely to change substantially in
-// the future to match specific needs of the discordgo package itself.
-
-package discordgo
-
-import (
-	"bytes"
-	"encoding/json"
-	"fmt"
-	"log"
-)
-
-// printEvent prints out a WSAPI event.
-func printEvent(e *Event) {
-	log.Println(fmt.Sprintf("Event. Type: %s, State: %d Operation: %d Direction: %d", e.Type, e.State, e.Operation, e.Direction))
-	printJSON(e.RawData)
-}
-
-// printJSON is a helper function to display JSON data in a easy to read format.
-func printJSON(body []byte) {
-	var prettyJSON bytes.Buffer
-	error := json.Indent(&prettyJSON, body, "", "\t")
-	if error != nil {
-		log.Print("JSON parse error: ", error)
-	}
-	log.Println(string(prettyJSON.Bytes()))
-}

+ 2 - 1
voice.go

@@ -32,7 +32,8 @@ import (
 type VoiceConnection struct {
 	sync.RWMutex
 
-	Debug     bool // If true, print extra logging
+	Debug     bool // If true, print extra logging -- DEPRECATED
+	LogLevel  int
 	Ready     bool // If true, voice is ready to send/receive audio
 	UserID    string
 	GuildID   string