From 9cb5b82dc7d4692771af37e57cffcffa964244cc Mon Sep 17 00:00:00 2001
From: Jimmy Zelinskie <jimmyzelinskie@gmail.com>
Date: Mon, 30 Jun 2014 23:21:08 -0400
Subject: [PATCH] log->glog; more bencode fixes

---
 config/config.go         |  6 +++---
 main.go                  | 22 +++++++++++-----------
 server/serve_announce.go | 11 +++--------
 server/serve_scrape.go   | 26 +++++++++++++++++---------
 server/server.go         |  4 ++--
 5 files changed, 36 insertions(+), 33 deletions(-)

diff --git a/config/config.go b/config/config.go
index 8b0f3b1..c239bac 100644
--- a/config/config.go
+++ b/config/config.go
@@ -11,7 +11,7 @@ import (
 	"os"
 	"time"
 
-	log "github.com/golang/glog"
+	"github.com/golang/glog"
 )
 
 // Duration wraps a time.Duration and adds JSON marshalling.
@@ -88,7 +88,7 @@ func New() *Config {
 // New.
 func Open(path string) (*Config, error) {
 	if path == "" {
-		log.V(1).Info("using default config")
+		glog.V(1).Info("using default config")
 		return New(), nil
 	}
 
@@ -102,7 +102,7 @@ func Open(path string) (*Config, error) {
 	if err != nil {
 		return nil, err
 	}
-	log.V(1).Infof("loaded config file: %s", path)
+	glog.V(1).Infof("loaded config file: %s", path)
 	return conf, nil
 }
 
diff --git a/main.go b/main.go
index 19d5ddc..f7fa8a0 100644
--- a/main.go
+++ b/main.go
@@ -11,7 +11,7 @@ import (
 	"runtime"
 	"runtime/pprof"
 
-	log "github.com/golang/glog"
+	"github.com/golang/glog"
 
 	"github.com/chihaya/chihaya/config"
 	_ "github.com/chihaya/chihaya/drivers/backend/mock"
@@ -37,29 +37,29 @@ func main() {
 	if profile {
 		f, err := os.Create("chihaya.cpu")
 		if err != nil {
-			log.Fatalf("failed to create profile file: %s\n", err)
+			glog.Fatalf("failed to create profile file: %s\n", err)
 		}
 		defer f.Close()
 
 		pprof.StartCPUProfile(f)
-		log.V(1).Info("started profiling")
+		glog.V(1).Info("started profiling")
 
 		defer func() {
 			pprof.StopCPUProfile()
-			log.V(1).Info("stopped profiling")
+			glog.V(1).Info("stopped profiling")
 		}()
 	}
 
 	// Load the config file.
 	conf, err := config.Open(configPath)
 	if err != nil {
-		log.Fatalf("failed to parse configuration file: %s\n", err)
+		glog.Fatalf("failed to parse configuration file: %s\n", err)
 	}
 
 	// Create a new server.
 	s, err := server.New(conf)
 	if err != nil {
-		log.Fatalf("failed to create server: %s\n", err)
+		glog.Fatalf("failed to create server: %s\n", err)
 	}
 
 	// Spawn a goroutine to handle interrupts and safely shut down.
@@ -68,24 +68,24 @@ func main() {
 		signal.Notify(interrupts, os.Interrupt)
 
 		<-interrupts
-		log.V(1).Info("caught interrupt, shutting down...")
+		glog.V(1).Info("caught interrupt, shutting down...")
 
 		err := s.Stop()
 		if err != nil {
-			log.Fatalf("failed to shutdown cleanly: %s", err)
+			glog.Fatalf("failed to shutdown cleanly: %s", err)
 		}
 
-		log.V(1).Info("shutdown cleanly")
+		glog.V(1).Info("shutdown cleanly")
 
 		<-interrupts
 
-		log.Flush()
+		glog.Flush()
 		os.Exit(0)
 	}()
 
 	// Start the server listening and handling requests.
 	err = s.ListenAndServe()
 	if err != nil {
-		log.Fatalf("failed to start server: %s\n", err)
+		glog.Fatalf("failed to start server: %s\n", err)
 	}
 }
diff --git a/server/serve_announce.go b/server/serve_announce.go
index 09d5741..d9f6acd 100644
--- a/server/serve_announce.go
+++ b/server/serve_announce.go
@@ -9,7 +9,7 @@ import (
 	"io"
 	"net/http"
 
-	log "github.com/golang/glog"
+	"github.com/golang/glog"
 
 	"github.com/chihaya/chihaya/bencode"
 	"github.com/chihaya/chihaya/drivers/tracker"
@@ -74,19 +74,14 @@ func (s Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
 	w.(http.Flusher).Flush()
 
 	if s.conf.Private {
-
-		log.V(5).Infof(
+		glog.V(5).Infof(
 			"announce: ip: %s user: %s torrent: %s",
 			announce.IP,
 			user.ID,
 			torrent.ID,
 		)
 	} else {
-		log.V(5).Infof(
-			"announce: ip: %s torrent: %s",
-			announce.IP,
-			torrent.ID,
-		)
+		glog.V(5).Infof("announce: ip: %s torrent: %s", announce.IP, torrent.ID)
 	}
 }
 
diff --git a/server/serve_scrape.go b/server/serve_scrape.go
index 93ed05b..078d1b0 100644
--- a/server/serve_scrape.go
+++ b/server/serve_scrape.go
@@ -10,7 +10,7 @@ import (
 	"net/http"
 	"strings"
 
-	log "github.com/golang/glog"
+	"github.com/golang/glog"
 
 	"github.com/chihaya/chihaya/bencode"
 	"github.com/chihaya/chihaya/models"
@@ -52,21 +52,29 @@ func (s *Server) serveScrape(w http.ResponseWriter, r *http.Request) {
 	}
 
 	bencoder := bencode.NewEncoder(w)
-	bencoder.Encode("d")
+	fmt.Fprintf(w, "d")
 	bencoder.Encode("files")
 	for _, torrent := range torrents {
 		writeTorrentStatus(w, torrent)
 	}
-	bencoder.Encode("e")
+	fmt.Fprintf(w, "e")
 
 	w.(http.Flusher).Flush()
 
-	log.V(5).Infof(
-		"scrape: ip: %s user: %s torrents: %s",
-		r.RemoteAddr,
-		user.ID,
-		strings.Join(torrentIDs, ", "),
-	)
+	if s.conf.Private {
+		glog.V(5).Infof(
+			"scrape: ip: %s user: %s torrents: %s",
+			r.RemoteAddr,
+			user.ID,
+			strings.Join(torrentIDs, ", "),
+		)
+	} else {
+		glog.V(5).Infof(
+			"scrape: ip: %s torrents: %s",
+			r.RemoteAddr,
+			strings.Join(torrentIDs, ", "),
+		)
+	}
 }
 
 func writeTorrentStatus(w io.Writer, t *models.Torrent) {
diff --git a/server/server.go b/server/server.go
index f879f8b..f4d623f 100644
--- a/server/server.go
+++ b/server/server.go
@@ -16,7 +16,7 @@ import (
 	"time"
 
 	"github.com/etix/stoppableListener"
-	log "github.com/golang/glog"
+	"github.com/golang/glog"
 
 	"github.com/chihaya/chihaya/config"
 	"github.com/chihaya/chihaya/drivers/backend"
@@ -130,7 +130,7 @@ func fail(err error, w http.ResponseWriter, r *http.Request) {
 
 	w.(http.Flusher).Flush()
 
-	log.V(5).Infof(
+	glog.V(5).Infof(
 		"failed request: ip: %s failure: %s",
 		r.RemoteAddr,
 		errmsg,