log->glog; more bencode fixes

This commit is contained in:
Jimmy Zelinskie 2014-06-30 23:21:08 -04:00
parent 834e584f2d
commit 9cb5b82dc7
5 changed files with 36 additions and 33 deletions

View file

@ -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
}

22
main.go
View file

@ -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)
}
}

View file

@ -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)
}
}

View file

@ -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) {

View file

@ -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,