From d1f25d856184fbd6f40c90f1b46cff29fab1f2d8 Mon Sep 17 00:00:00 2001 From: jonas747 Date: Mon, 21 Mar 2016 17:13:39 +0100 Subject: [PATCH] Changed fmt.Print to log.Print --- oauth2_test.go | 25 +++++++++++++------------ restapi.go | 15 ++++++++------- state.go | 4 ++-- util.go | 7 ++++--- voice.go | 47 ++++++++++++++++++++++++----------------------- wsapi.go | 20 ++++++++++---------- 6 files changed, 61 insertions(+), 57 deletions(-) diff --git a/oauth2_test.go b/oauth2_test.go index 394cdbf..c177ca7 100644 --- a/oauth2_test.go +++ b/oauth2_test.go @@ -2,6 +2,7 @@ package discordgo_test import ( "fmt" + "log" "os" "github.com/bwmarrin/discordgo" @@ -18,7 +19,7 @@ func ExampleApplication() { // Create a new Discordgo session dg, err := discordgo.New(Token) if err != nil { - fmt.Println(err) + log.Println(err) return } @@ -27,31 +28,31 @@ func ExampleApplication() { ap.Name = "TestApp" ap.Description = "TestDesc" ap, err = dg.ApplicationCreate(ap) - fmt.Printf("ApplicationCreate: err: %+v, app: %+v\n", err, ap) + log.Printf("ApplicationCreate: err: %+v, app: %+v\n", err, ap) // Get a specific Application by it's ID ap, err = dg.Application(ap.ID) - fmt.Printf("Application: err: %+v, app: %+v\n", err, ap) + log.Printf("Application: err: %+v, app: %+v\n", err, ap) // Update an existing Application with new values ap.Description = "Whooooa" ap, err = dg.ApplicationUpdate(ap.ID, ap) - fmt.Printf("ApplicationUpdate: err: %+v, app: %+v\n", err, ap) + log.Printf("ApplicationUpdate: err: %+v, app: %+v\n", err, ap) // create a new bot account for this application bot, err := dg.ApplicationBotCreate(ap.ID, "") - fmt.Printf("BotCreate: err: %+v, bot: %+v\n", err, bot) + log.Printf("BotCreate: err: %+v, bot: %+v\n", err, bot) // Get a list of all applications for the authenticated user apps, err := dg.Applications() - fmt.Printf("Applications: err: %+v, apps : %+v\n", err, apps) + log.Printf("Applications: err: %+v, apps : %+v\n", err, apps) for k, v := range apps { - fmt.Printf("Applications: %d : %+v\n", k, v) + log.Printf("Applications: %d : %+v\n", k, v) } // Delete the application we created. err = ap.Delete() - fmt.Printf("Delete: err: %+v\n", err) + log.Printf("Delete: err: %+v\n", err) return } @@ -64,7 +65,7 @@ func ExampleApplicationConvertBot() { dg, err := discordgo.New("myemail", "mypassword") if err != nil { - fmt.Println(err) + log.Println(err) return } @@ -73,14 +74,14 @@ func ExampleApplicationConvertBot() { ap.Name = "Application Name" ap.Description = "Application Description" ap, err = dg.ApplicationCreate(ap) - fmt.Printf("ApplicationCreate: err: %+v, app: %+v\n", err, ap) + log.Printf("ApplicationCreate: err: %+v, app: %+v\n", err, ap) // create a bot account bot, err := dg.ApplicationBotCreate(ap.ID, "existing bot user account token") - fmt.Printf("BotCreate: err: %+v, bot: %+v\n", err, bot) + log.Printf("BotCreate: err: %+v, bot: %+v\n", err, bot) if err != nil { - fmt.Printf("You can not login with your converted bot user using the below token\n%s\n", bot.Token) + log.Printf("You can not login with your converted bot user using the below token\n%s\n", bot.Token) } return diff --git a/restapi.go b/restapi.go index 38a765a..f1674c9 100644 --- a/restapi.go +++ b/restapi.go @@ -20,6 +20,7 @@ import ( _ "image/png" // For PNG decoding "io" "io/ioutil" + "log" "mime/multipart" "net/http" "net/url" @@ -35,7 +36,7 @@ var ErrJSONUnmarshal = errors.New("json unmarshal") func (s *Session) Request(method, urlStr string, data interface{}) (response []byte, err error) { if s.Debug { - fmt.Println("API REQUEST PAYLOAD :: [" + fmt.Sprintf("%+v", data) + "]") + log.Println("API REQUEST PAYLOAD :: [" + fmt.Sprintf("%+v", data) + "]") } var body []byte @@ -53,7 +54,7 @@ func (s *Session) Request(method, urlStr string, data interface{}) (response []b func (s *Session) request(method, urlStr, contentType string, b []byte) (response []byte, err error) { if s.Debug { - fmt.Printf("API REQUEST %8s :: %s\n", method, urlStr) + log.Printf("API REQUEST %8s :: %s\n", method, urlStr) } req, err := http.NewRequest(method, urlStr, bytes.NewBuffer(b)) @@ -73,7 +74,7 @@ func (s *Session) request(method, urlStr, contentType string, b []byte) (respons if s.Debug { for k, v := range req.Header { - fmt.Printf("API REQUEST HEADER :: [%s] = %+v\n", k, v) + log.Printf("API REQUEST HEADER :: [%s] = %+v\n", k, v) } } @@ -86,7 +87,7 @@ func (s *Session) request(method, urlStr, contentType string, b []byte) (respons defer func() { err := resp.Body.Close() if err != nil { - fmt.Println("error closing resp body") + log.Println("error closing resp body") } }() @@ -97,11 +98,11 @@ func (s *Session) request(method, urlStr, contentType string, b []byte) (respons if s.Debug { - fmt.Printf("API RESPONSE STATUS :: %s\n", resp.Status) + log.Printf("API RESPONSE STATUS :: %s\n", resp.Status) for k, v := range resp.Header { - fmt.Printf("API RESPONSE HEADER :: [%s] = %+v\n", k, v) + log.Printf("API RESPONSE HEADER :: [%s] = %+v\n", k, v) } - fmt.Printf("API RESPONSE BODY :: [%s]\n", response) + log.Printf("API RESPONSE BODY :: [%s]\n", response) } switch resp.StatusCode { diff --git a/state.go b/state.go index 901fd31..58f52c5 100644 --- a/state.go +++ b/state.go @@ -14,7 +14,7 @@ package discordgo import ( "errors" - "fmt" + "log" ) // ErrNilState is returned when the state is nil. @@ -433,7 +433,7 @@ func (s *State) MessageAdd(message *Message) error { for len(c.Messages) > s.MaxMessageCount { err := s.MessageRemove(c.Messages[0]) if err != nil { - fmt.Println("message remove error: ", err) + log.Println("message remove error: ", err) } } s.Lock() diff --git a/util.go b/util.go index 20ab084..935103b 100644 --- a/util.go +++ b/util.go @@ -15,11 +15,12 @@ import ( "bytes" "encoding/json" "fmt" + "log" ) // printEvent prints out a WSAPI event. func printEvent(e *Event) { - fmt.Println(fmt.Sprintf("Event. Type: %s, State: %d Operation: %d Direction: %d", e.Type, e.State, e.Operation, e.Direction)) + log.Println(fmt.Sprintf("Event. Type: %s, State: %d Operation: %d Direction: %d", e.Type, e.State, e.Operation, e.Direction)) printJSON(e.RawData) } @@ -28,7 +29,7 @@ func printJSON(body []byte) { var prettyJSON bytes.Buffer error := json.Indent(&prettyJSON, body, "", "\t") if error != nil { - fmt.Print("JSON parse error: ", error) + log.Print("JSON parse error: ", error) } - fmt.Println(string(prettyJSON.Bytes())) + log.Println(string(prettyJSON.Bytes())) } diff --git a/voice.go b/voice.go index 5c093c6..926a90e 100644 --- a/voice.go +++ b/voice.go @@ -13,6 +13,7 @@ import ( "encoding/binary" "encoding/json" "fmt" + "log" "net" "runtime" "strings" @@ -88,7 +89,7 @@ func (v *VoiceConnection) Speaking(b bool) (err error) { data := voiceSpeakingOp{5, voiceSpeakingData{b, 0}} err = v.wsConn.WriteJSON(data) if err != nil { - fmt.Println("Speaking() write json error:", err) + log.Println("Speaking() write json error:", err) return } @@ -141,7 +142,7 @@ func (v *VoiceConnection) Close() { if v.udpConn != nil { err := v.udpConn.Close() if err != nil { - fmt.Println("error closing udp connection: ", err) + log.Println("error closing udp connection: ", err) } v.udpConn = nil } @@ -149,7 +150,7 @@ func (v *VoiceConnection) Close() { if v.wsConn != nil { err := v.wsConn.Close() if err != nil { - fmt.Println("error closing websocket connection: ", err) + log.Println("error closing websocket connection: ", err) } v.wsConn = nil } @@ -241,7 +242,7 @@ func (v *VoiceConnection) open() (err error) { vg := fmt.Sprintf("wss://%s", strings.TrimSuffix(v.endpoint, ":80")) v.wsConn, _, err = websocket.DefaultDialer.Dial(vg, nil) if err != nil { - fmt.Println("VOICE error opening websocket:", err) + log.Println("VOICE error opening websocket:", err) return } @@ -259,7 +260,7 @@ func (v *VoiceConnection) open() (err error) { err = v.wsConn.WriteJSON(data) if err != nil { - fmt.Println("VOICE error sending init packet:", err) + log.Println("VOICE error sending init packet:", err) return } @@ -282,7 +283,7 @@ func (v *VoiceConnection) wsListen(wsConn *websocket.Conn, close <-chan struct{} // TODO: add reconnect, matching wsapi.go:listen() // TODO: Handle this problem better. // TODO: needs proper logging - fmt.Println("VoiceConnection Listen Error:", err) + log.Println("VoiceConnection Listen Error:", err) return } @@ -301,13 +302,13 @@ func (v *VoiceConnection) wsListen(wsConn *websocket.Conn, close <-chan struct{} func (v *VoiceConnection) wsEvent(messageType int, message []byte) { if v.Debug { - fmt.Println("wsEvent received: ", messageType) + log.Println("wsEvent received: ", messageType) printJSON(message) } var e Event if err := json.Unmarshal(message, &e); err != nil { - fmt.Println("wsEvent Unmarshall error: ", err) + log.Println("wsEvent Unmarshall error: ", err) return } @@ -316,7 +317,7 @@ func (v *VoiceConnection) wsEvent(messageType int, message []byte) { case 2: // READY if err := json.Unmarshal(e.RawData, &v.op2); err != nil { - fmt.Println("voiceWS.onEvent OP2 Unmarshall error: ", err) + log.Println("voiceWS.onEvent OP2 Unmarshall error: ", err) printJSON(e.RawData) // TODO: Better error logging return } @@ -328,7 +329,7 @@ func (v *VoiceConnection) wsEvent(messageType int, message []byte) { // Start the UDP connection err := v.udpOpen() if err != nil { - fmt.Println("Error opening udp connection: ", err) + log.Println("Error opening udp connection: ", err) return } @@ -357,7 +358,7 @@ func (v *VoiceConnection) wsEvent(messageType int, message []byte) { case 4: // udp encryption secret key v.op4 = voiceOP4{} if err := json.Unmarshal(e.RawData, &v.op4); err != nil { - fmt.Println("voiceWS.onEvent OP4 Unmarshall error: ", err) + log.Println("voiceWS.onEvent OP4 Unmarshall error: ", err) printJSON(e.RawData) return } @@ -370,7 +371,7 @@ func (v *VoiceConnection) wsEvent(messageType int, message []byte) { voiceSpeakingUpdate := &VoiceSpeakingUpdate{} if err := json.Unmarshal(e.RawData, voiceSpeakingUpdate); err != nil { - fmt.Println("voiceWS.onEvent VoiceSpeakingUpdate Unmarshal error: ", err) + log.Println("voiceWS.onEvent VoiceSpeakingUpdate Unmarshal error: ", err) printJSON(e.RawData) return } @@ -380,7 +381,7 @@ func (v *VoiceConnection) wsEvent(messageType int, message []byte) { } default: - fmt.Println("UNKNOWN VOICE OP: ", e.Operation) + log.Println("UNKNOWN VOICE OP: ", e.Operation) printJSON(e.RawData) } @@ -409,7 +410,7 @@ func (v *VoiceConnection) wsHeartbeat(wsConn *websocket.Conn, close <-chan struc for { err = wsConn.WriteJSON(voiceHeartbeatOp{3, int(time.Now().Unix())}) if err != nil { - fmt.Println("wsHeartbeat send error: ", err) + log.Println("wsHeartbeat send error: ", err) return } @@ -470,14 +471,14 @@ func (v *VoiceConnection) udpOpen() (err error) { host := fmt.Sprintf("%s:%d", strings.TrimSuffix(v.endpoint, ":80"), v.op2.Port) addr, err := net.ResolveUDPAddr("udp", host) if err != nil { - fmt.Println("udpOpen resolve addr error: ", err) + log.Println("udpOpen resolve addr error: ", err) // TODO better logging return } v.udpConn, err = net.DialUDP("udp", nil, addr) if err != nil { - fmt.Println("udpOpen dial udp error: ", err) + log.Println("udpOpen dial udp error: ", err) // TODO better logging return } @@ -488,7 +489,7 @@ func (v *VoiceConnection) udpOpen() (err error) { binary.BigEndian.PutUint32(sb, v.op2.SSRC) _, err = v.udpConn.Write(sb) if err != nil { - fmt.Println("udpOpen udp write error : ", err) + log.Println("udpOpen udp write error : ", err) // TODO better logging return } @@ -500,12 +501,12 @@ func (v *VoiceConnection) udpOpen() (err error) { rb := make([]byte, 70) rlen, _, err := v.udpConn.ReadFromUDP(rb) if err != nil { - fmt.Println("udpOpen udp read error : ", err) + log.Println("udpOpen udp read error : ", err) // TODO better logging return } if rlen < 70 { - fmt.Println("VoiceConnection RLEN should be 70 but isn't") + log.Println("VoiceConnection RLEN should be 70 but isn't") } // Loop over position 4 though 20 to grab the IP address @@ -527,7 +528,7 @@ func (v *VoiceConnection) udpOpen() (err error) { err = v.wsConn.WriteJSON(data) if err != nil { - fmt.Println("udpOpen write json error:", err) + log.Println("udpOpen write json error:", err) return } @@ -559,7 +560,7 @@ func (v *VoiceConnection) udpKeepAlive(udpConn *net.UDPConn, close <-chan struct _, err = udpConn.Write(packet) if err != nil { - fmt.Println("udpKeepAlive udp write error : ", err) + log.Println("udpKeepAlive udp write error : ", err) return } @@ -633,7 +634,7 @@ func (v *VoiceConnection) opusSender(udpConn *net.UDPConn, close <-chan struct{} _, err := udpConn.Write(sendbuf) if err != nil { - fmt.Println("error writing to udp connection: ", err) + log.Println("error writing to udp connection: ", err) return } @@ -677,7 +678,7 @@ func (v *VoiceConnection) opusReceiver(udpConn *net.UDPConn, close <-chan struct for { rlen, err := udpConn.Read(recvbuf) if err != nil { - fmt.Println("opusReceiver UDP Read error:", err) + log.Println("opusReceiver UDP Read error:", err) return } diff --git a/wsapi.go b/wsapi.go index dc9ed3e..2a4dc69 100644 --- a/wsapi.go +++ b/wsapi.go @@ -15,8 +15,8 @@ import ( "compress/zlib" "encoding/json" "errors" - "fmt" "io" + "log" "net/http" "reflect" "runtime" @@ -137,7 +137,7 @@ func (s *Session) listen(wsConn *websocket.Conn, listening <-chan interface{}) { // OnDisconnect is fired. err := s.Close() if err != nil { - fmt.Println("error closing session connection: ", err) + log.Println("error closing session connection: ", err) } // Attempt to reconnect, with expenonential backoff up to 10 minutes. @@ -190,7 +190,7 @@ func (s *Session) heartbeat(wsConn *websocket.Conn, listening <-chan interface{} for { err = wsConn.WriteJSON(heartbeatOp{1, int(time.Now().Unix())}) if err != nil { - fmt.Println("Error sending heartbeat:", err) + log.Println("Error sending heartbeat:", err) return } @@ -256,13 +256,13 @@ func (s *Session) event(messageType int, message []byte) { if messageType == 2 { z, err1 := zlib.NewReader(reader) if err1 != nil { - fmt.Println(err1) + log.Println(err1) return } defer func() { err := z.Close() if err != nil { - fmt.Println("error closing zlib:", err) + log.Println("error closing zlib:", err) } }() reader = z @@ -271,7 +271,7 @@ func (s *Session) event(messageType int, message []byte) { var e *Event decoder := json.NewDecoder(reader) if err = decoder.Decode(&e); err != nil { - fmt.Println(err) + log.Println(err) return } @@ -287,14 +287,14 @@ func (s *Session) event(messageType int, message []byte) { // Attempt to unmarshal our event. // If there is an error we should handle the event itself. if err = unmarshal(e.RawData, i); err != nil { - fmt.Println("Unable to unmarshal event data.", err) + log.Println("Unable to unmarshal event data.", err) // Ready events must fire, even if they are empty. if e.Type != "READY" { i = e } } } else { - fmt.Println("Unknown event.") + log.Println("Unknown event.") i = e } @@ -400,7 +400,7 @@ func (s *Session) onVoiceStateUpdate(se *Session, st *VoiceStateUpdate) { // be moved to a small helper function self, err := s.User("@me") // TODO: move to Login/New if err != nil { - fmt.Println(err) + log.Println(err) return } @@ -453,7 +453,7 @@ func (s *Session) onVoiceServerUpdate(se *Session, st *VoiceServerUpdate) { // so, that's what the next call does. err := voice.open() if err != nil { - fmt.Println("onVoiceServerUpdate Voice.Open error: ", err) + log.Println("onVoiceServerUpdate Voice.Open error: ", err) // TODO better logging } }