diff --git a/cmd/chihaya/main.go b/cmd/chihaya/main.go index 3473717..0cc265d 100644 --- a/cmd/chihaya/main.go +++ b/cmd/chihaya/main.go @@ -2,13 +2,13 @@ package main import ( "errors" - "log" "net/http" "os" "os/signal" "runtime/pprof" "syscall" + log "github.com/Sirupsen/logrus" "github.com/prometheus/client_golang/prometheus" "github.com/spf13/cobra" @@ -21,7 +21,7 @@ import ( func rootCmdRun(cmd *cobra.Command, args []string) error { cpuProfilePath, _ := cmd.Flags().GetString("cpuprofile") if cpuProfilePath != "" { - log.Println("enabled CPU profiling to " + cpuProfilePath) + log.Infoln("enabled CPU profiling to", cpuProfilePath) f, err := os.Create(cpuProfilePath) if err != nil { return err @@ -42,26 +42,26 @@ func rootCmdRun(cmd *cobra.Command, args []string) error { Addr: cfg.PrometheusAddr, Handler: prometheus.Handler(), } - log.Println("started serving prometheus stats on", cfg.PrometheusAddr) + log.Infoln("started serving prometheus stats on", cfg.PrometheusAddr) if err := promServer.ListenAndServe(); err != nil { - log.Fatal(err) + log.Fatalln("failed to start prometheus server:", err.Error()) } }() // Force the compiler to enforce memory against the storage interface. peerStore, err := memory.New(cfg.Storage) if err != nil { - return err + return errors.New("failed to create memory storage: " + err.Error()) } preHooks, postHooks, err := configFile.CreateHooks() if err != nil { - return err + return errors.New("failed to create hooks: " + err.Error()) } logic := middleware.NewLogic(cfg.Config, peerStore, preHooks, postHooks) if err != nil { - return err + return errors.New("failed to create TrackerLogic: " + err.Error()) } shutdown := make(chan struct{}) @@ -74,7 +74,7 @@ func rootCmdRun(cmd *cobra.Command, args []string) error { httpFrontend = httpfrontend.NewFrontend(logic, cfg.HTTPConfig) go func() { - log.Println("started serving HTTP on", cfg.HTTPConfig.Addr) + log.Infoln("started serving HTTP on", cfg.HTTPConfig.Addr) if err := httpFrontend.ListenAndServe(); err != nil { errChan <- errors.New("failed to cleanly shutdown HTTP frontend: " + err.Error()) } @@ -85,7 +85,7 @@ func rootCmdRun(cmd *cobra.Command, args []string) error { udpFrontend = udpfrontend.NewFrontend(logic, cfg.UDPConfig) go func() { - log.Println("started serving UDP on", cfg.UDPConfig.Addr) + log.Infoln("started serving UDP on", cfg.UDPConfig.Addr) if err := udpFrontend.ListenAndServe(); err != nil { errChan <- errors.New("failed to cleanly shutdown UDP frontend: " + err.Error()) } @@ -114,6 +114,8 @@ func rootCmdRun(cmd *cobra.Command, args []string) error { } } + // TODO(jzelinskie): stop hooks here + close(errChan) }() @@ -125,7 +127,7 @@ func rootCmdRun(cmd *cobra.Command, args []string) error { close(shutdown) closed = true } else { - log.Println(bufErr) + log.Infoln(bufErr) } bufErr = err } diff --git a/middleware/jwt/jwt.go b/middleware/jwt/jwt.go index 1743c1f..8b9c937 100644 --- a/middleware/jwt/jwt.go +++ b/middleware/jwt/jwt.go @@ -11,7 +11,6 @@ import ( "crypto" "encoding/json" "errors" - "log" "net/http" "net/url" "time" @@ -19,6 +18,7 @@ import ( jc "github.com/SermoDigital/jose/crypto" "github.com/SermoDigital/jose/jws" "github.com/SermoDigital/jose/jwt" + log "github.com/Sirupsen/logrus" "github.com/mendsley/gojwk" "github.com/chihaya/chihaya/bittorrent" @@ -64,7 +64,7 @@ func NewHook(cfg Config) middleware.Hook { case <-time.After(cfg.JWKUpdateInterval): resp, err := http.Get(cfg.JWKSetURL) if err != nil { - log.Println("failed to fetch JWK Set: " + err.Error()) + log.Errorln("failed to fetch JWK Set: " + err.Error()) continue } @@ -72,7 +72,7 @@ func NewHook(cfg Config) middleware.Hook { err = json.NewDecoder(resp.Body).Decode(&parsedJWKs) if err != nil { resp.Body.Close() - log.Println("failed to decode JWK JSON: " + err.Error()) + log.Errorln("failed to decode JWK JSON: " + err.Error()) continue } resp.Body.Close() @@ -81,7 +81,7 @@ func NewHook(cfg Config) middleware.Hook { for kid, parsedJWK := range parsedJWKs { publicKey, err := parsedJWK.DecodePublicKey() if err != nil { - log.Println("failed to decode JWK into public key: " + err.Error()) + log.Errorln("failed to decode JWK into public key: " + err.Error()) continue } keys[kid] = publicKey diff --git a/middleware/middleware.go b/middleware/middleware.go index 0778ce0..0c9b86b 100644 --- a/middleware/middleware.go +++ b/middleware/middleware.go @@ -4,9 +4,9 @@ package middleware import ( "context" - "log" "time" + log "github.com/Sirupsen/logrus" "github.com/chihaya/chihaya/bittorrent" "github.com/chihaya/chihaya/frontend" "github.com/chihaya/chihaya/storage" @@ -65,7 +65,7 @@ func (l *Logic) HandleAnnounce(ctx context.Context, req *bittorrent.AnnounceRequ func (l *Logic) AfterAnnounce(ctx context.Context, req *bittorrent.AnnounceRequest, resp *bittorrent.AnnounceResponse) { for _, h := range l.postHooks { if err := h.HandleAnnounce(ctx, req, resp); err != nil { - log.Println("chihaya: post-announce hooks failed:", err.Error()) + log.Errorln("chihaya: post-announce hooks failed:", err.Error()) return } } @@ -90,7 +90,7 @@ func (l *Logic) HandleScrape(ctx context.Context, req *bittorrent.ScrapeRequest) func (l *Logic) AfterScrape(ctx context.Context, req *bittorrent.ScrapeRequest, resp *bittorrent.ScrapeResponse) { for _, h := range l.postHooks { if err := h.HandleScrape(ctx, req, resp); err != nil { - log.Println("chihaya: post-scrape hooks failed:", err.Error()) + log.Errorln("chihaya: post-scrape hooks failed:", err.Error()) return } } diff --git a/storage/memory/peer_store.go b/storage/memory/peer_store.go index 02211a1..1947d48 100644 --- a/storage/memory/peer_store.go +++ b/storage/memory/peer_store.go @@ -3,12 +3,13 @@ package memory import ( "encoding/binary" "errors" - "log" "net" "runtime" "sync" "time" + log "github.com/Sirupsen/logrus" + "github.com/chihaya/chihaya/bittorrent" "github.com/chihaya/chihaya/storage" ) @@ -53,7 +54,7 @@ func New(cfg Config) (storage.PeerStore, error) { return case <-time.After(cfg.GarbageCollectionInterval): before := time.Now().Add(-cfg.PeerLifetime) - log.Println("memory: purging peers with no announces since ", before) + log.Debugln("memory: purging peers with no announces since", before) ps.collectGarbage(before) } } @@ -327,7 +328,6 @@ func (s *peerStore) collectGarbage(cutoff time.Time) error { default: } - log.Printf("memory: collecting garbage. Cutoff time: %s", cutoff.String()) cutoffUnix := cutoff.UnixNano() for _, shard := range s.shards { shard.RLock()