Fixed logging to use log rather than syslog.
authorChris Jones <christian.jones@sri.com>
Thu, 19 Jan 2012 14:18:39 -0600
changeset 76 caa722ab8a0f
parent 75 03a923eb5c01
child 77 edda99a69002
Fixed logging to use log rather than syslog.
examples/interact.go
stream.go
xmpp.go
--- a/examples/interact.go	Thu Jan 19 13:41:42 2012 -0600
+++ b/examples/interact.go	Thu Jan 19 14:18:39 2012 -0600
@@ -9,12 +9,18 @@
 	"flag"
 	"fmt"
 	"log"
+	"log/syslog"
 	"os"
 )
 
 // Demonstrate the API, and allow the user to interact with an XMPP
 // server via the terminal.
 func main() {
+	if false {
+		xmpp.Loglevel = syslog.LOG_DEBUG
+		xmpp.Log = log.New(os.Stderr, "", 0)
+	}
+
 	var jid xmpp.JID
 	flag.Var(&jid, "jid", "JID to log in as")
 	var pw *string = flag.String("pw", "", "password")
--- a/stream.go	Thu Jan 19 13:41:42 2012 -0600
+++ b/stream.go	Thu Jan 19 14:18:39 2012 -0600
@@ -52,14 +52,14 @@
 				}
 			}
 			if Log != nil {
-				Log.Err("read: " + err.Error())
+				Log.Println("read: " + err.Error())
 			}
 			break
 		}
 		nw, err := w.Write(p[:nr])
 		if nw < nr {
 			if Log != nil {
-				Log.Err("read: " + err.Error())
+				Log.Println("read: " + err.Error())
 			}
 			break
 		}
@@ -73,14 +73,14 @@
 		nr, err := r.Read(p)
 		if nr == 0 {
 			if Log != nil {
-				Log.Err("write: " + err.Error())
+				Log.Println("write: " + err.Error())
 			}
 			break
 		}
 		nw, err := cl.socket.Write(p[:nr])
 		if nw < nr {
 			if Log != nil {
-				Log.Err("write: " + err.Error())
+				Log.Println("write: " + err.Error())
 			}
 			break
 		}
@@ -104,7 +104,7 @@
 		if t == nil {
 			if err != io.EOF {
 				if Log != nil {
-					Log.Err("read: " + err.Error())
+					Log.Println("read: " + err.Error())
 				}
 			}
 			break
@@ -122,7 +122,7 @@
 			st, err := parseStream(se)
 			if err != nil {
 				if Log != nil {
-					Log.Err("unmarshal stream: " +
+					Log.Println("unmarshal stream: " +
 						err.Error())
 				}
 				break Loop
@@ -146,9 +146,9 @@
 			obj = &Presence{}
 		default:
 			obj = &Generic{}
-			if Log != nil {
-				Log.Notice("Ignoring unrecognized: " +
-					se.Name.Space + " " + se.Name.Local)
+			if Log != nil && Loglevel >= syslog.LOG_NOTICE {
+				Log.Printf("Ignoring unrecognized: %s %s",
+					se.Name.Space, se.Name.Local)
 			}
 		}
 
@@ -156,7 +156,7 @@
 		err = p.Unmarshal(obj, &se)
 		if err != nil {
 			if Log != nil {
-				Log.Err("unmarshal: " + err.Error())
+				Log.Println("unmarshal: " + err.Error())
 			}
 			break Loop
 		}
@@ -168,7 +168,7 @@
 			err = parseExtended(st, extStanza)
 			if err != nil {
 				if Log != nil {
-					Log.Err("ext unmarshal: " +
+					Log.Println("ext unmarshal: " +
 						err.Error())
 				}
 				break Loop
@@ -228,7 +228,7 @@
 		err := xml.Marshal(w, obj)
 		if err != nil {
 			if Log != nil {
-				Log.Err("write: " + err.Error())
+				Log.Println("write: " + err.Error())
 			}
 			break
 		}
@@ -268,9 +268,9 @@
 			}
 			st, ok := x.(Stanza)
 			if !ok {
-				if Log != nil {
-					Log.Warning(fmt.Sprintf(
-						"Unhandled non-stanza: %v", x))
+				if Log != nil && Loglevel >= syslog.LOG_WARNING {
+					Log.Printf(
+						"Unhandled non-stanza: %v", x)
 				}
 				continue
 			}
@@ -312,8 +312,8 @@
 				break Loop
 			}
 			if x == nil {
-				if Log != nil {
-					Log.Notice("Refusing to send" +
+				if Log != nil && Loglevel >= syslog.LOG_NOTICE {
+					Log.Println("Refusing to send" +
 						" nil stanza")
 				}
 				continue
@@ -333,8 +333,8 @@
 		select {
 		case newFilterOut := <-filterOut:
 			if newFilterOut == nil {
-				if Log != nil {
-					Log.Warning("Received nil filter")
+				if Log != nil && Loglevel >= syslog.LOG_WARNING {
+					Log.Println("Received nil filter")
 				}
 				filterIn <- nil
 				continue
@@ -362,8 +362,8 @@
 }
 
 func (cl *Client) handleStreamError(se *streamError) {
-	if Log != nil {
-		Log.Notice(fmt.Sprintf("Received stream error: %v", se))
+	if Log != nil && Loglevel >= syslog.LOG_NOTICE {
+		Log.Printf("Received stream error: %v", se)
 	}
 	close(cl.Out)
 }
@@ -413,8 +413,8 @@
 	// reader doesn't get woken up unnecessarily.
 	tcp.SetReadTimeout(0)
 
-	if Log != nil {
-		Log.Info("TLS negotiation succeeded.")
+	if Log != nil && Loglevel >= syslog.LOG_INFO {
+		Log.Println("TLS negotiation succeeded.")
 	}
 	cl.Features = nil
 
@@ -462,7 +462,7 @@
 		str, err := b64.DecodeString(srv.Chardata)
 		if err != nil {
 			if Log != nil {
-				Log.Err("SASL challenge decode: " +
+				Log.Println("SASL challenge decode: " +
 					err.Error())
 			}
 			return
@@ -475,12 +475,12 @@
 			cl.saslDigest2(srvMap)
 		}
 	case "failure":
-		if Log != nil {
-			Log.Notice("SASL authentication failed")
+		if Log != nil && Loglevel >= syslog.LOG_NOTICE {
+			Log.Println("SASL authentication failed")
 		}
 	case "success":
-		if Log != nil {
-			Log.Info("Sasl authentication succeeded")
+		if Log != nil && Loglevel >= syslog.LOG_INFO {
+			Log.Println("Sasl authentication succeeded")
 		}
 		cl.Features = nil
 		ss := &stream{To: cl.Jid.Domain, Version: Version}
@@ -498,7 +498,7 @@
 	}
 	if !hasAuth {
 		if Log != nil {
-			Log.Err("Server doesn't support SASL auth")
+			Log.Println("Server doesn't support SASL auth")
 		}
 		return
 	}
@@ -530,7 +530,7 @@
 	cnonce, err := rand.Int(rand.Reader, randSize)
 	if err != nil {
 		if Log != nil {
-			Log.Err("SASL rand: " + err.Error())
+			Log.Println("SASL rand: " + err.Error())
 		}
 		return
 	}
@@ -637,7 +637,7 @@
 	f := func(st Stanza) bool {
 		if st.GetType() == "error" {
 			if Log != nil {
-				Log.Err("Resource binding failed")
+				Log.Println("Resource binding failed")
 			}
 			return false
 		}
@@ -650,28 +650,27 @@
 		}
 		if bindRepl == nil {
 			if Log != nil {
-				Log.Err(fmt.Sprintf("Bad bind reply: %v",
-					st))
+				Log.Printf("Bad bind reply: %v", st)
 			}
 			return false
 		}
 		jidStr := bindRepl.Jid
 		if jidStr == nil || *jidStr == "" {
 			if Log != nil {
-				Log.Err("Can't bind empty resource")
+				Log.Println("Can't bind empty resource")
 			}
 			return false
 		}
 		jid := new(JID)
 		if err := jid.Set(*jidStr); err != nil {
 			if Log != nil {
-				Log.Err(err.Error())
+				Log.Println(err.Error())
 			}
 			return false
 		}
 		cl.Jid = *jid
-		if Log != nil {
-			Log.Info("Bound resource: " + cl.Jid.String())
+		if Log != nil && Loglevel >= syslog.LOG_INFO {
+			Log.Println("Bound resource: " + cl.Jid.String())
 		}
 		cl.bindDone()
 		return false
--- a/xmpp.go	Thu Jan 19 13:41:42 2012 -0600
+++ b/xmpp.go	Thu Jan 19 14:18:39 2012 -0600
@@ -12,6 +12,7 @@
 	"errors"
 	"fmt"
 	"io"
+	"log"
 	"log/syslog"
 	"net"
 	"sync"
@@ -38,7 +39,7 @@
 var (
 	// If non-nil when NewClient() is called, log messages will be
 	// sent to this writer.
-	Log *syslog.Writer
+	Log *log.Logger
 	// Threshold for which messages are logged.
 	Loglevel syslog.Priority = syslog.LOG_NOTICE
 )
@@ -249,13 +250,13 @@
 		}
 		buf.Write(c[:n])
 		if c[0] == '\n' || c[0] == '>' {
-			Log.Debug(buf.String())
+			Log.Println(buf.String())
 			buf.Reset()
 		}
 	}
 	leftover := buf.String()
 	if leftover != "" {
-		Log.Debug(buf.String())
+		Log.Println(buf.String())
 	}
 }
 
@@ -278,8 +279,8 @@
 	f := func(st Stanza) bool {
 		if st.GetType() == "error" {
 			if Log != nil {
-				Log.Err(fmt.Sprintf("Can't start session: %v",
-					st))
+				Log.Printf("Can't start session: %v",
+					st)
 			}
 			ch <- st.GetError()
 			return false