From 8376b0d53cae91af0e180c05b9cb92c3f3f30d58 Mon Sep 17 00:00:00 2001 From: shxsun Date: Mon, 24 Mar 2014 21:16:00 +0800 Subject: [PATCH] use modeles/log instead log --- modules/avatar/avatar.go | 28 +++++++++++++--------------- modules/avatar/avatar_test.go | 13 +++++++++---- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/modules/avatar/avatar.go b/modules/avatar/avatar.go index 1a18d8a7..0ba20294 100644 --- a/modules/avatar/avatar.go +++ b/modules/avatar/avatar.go @@ -3,6 +3,14 @@ // license that can be found in the LICENSE file. // for www.gravatar.com image cache + +/* +It is recommend to use this way + + cacheDir := "./cache" + defaultImg := "./default.jpg" + http.Handle("/avatar/", avatar.HttpHandler(cacheDir, defaultImg)) +*/ package avatar import ( @@ -14,7 +22,6 @@ import ( "image/jpeg" "image/png" "io" - "log" "net/http" "net/url" "os" @@ -23,6 +30,7 @@ import ( "sync" "time" + "github.com/gogits/gogs/modules/log" "github.com/nfnt/resize" ) @@ -30,12 +38,6 @@ var ( gravatar = "http://www.gravatar.com/avatar" ) -func debug(a ...interface{}) { - if true { - log.Println(a...) - } -} - // hash email to md5 string // keep this func in order to make this package indenpent func HashEmail(email string) string { @@ -125,7 +127,7 @@ func (this *Avatar) UpdateTimeout(timeout time.Duration) error { var err error select { case <-time.After(timeout): - err = errors.New("get gravatar image timeout") + err = fmt.Errorf("get gravatar image %s timeout", this.Hash) case err = <-thunder.GoFetch(gravatar+"/"+this.Hash+"?"+this.reqParams, this.imagePath): } @@ -150,16 +152,14 @@ func (this *avatarHandler) mustInt(r *http.Request, defaultValue int, keys ...st func (this *avatarHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { urlPath := r.URL.Path hash := urlPath[strings.LastIndex(urlPath, "/")+1:] - //hash = HashEmail(hash) - size := this.mustInt(r, 80, "s", "size") // size = 80*80 + size := this.mustInt(r, 80, "s", "size") // default size = 80*80 avatar := New(hash, this.cacheDir) avatar.AlterImage = this.altImage if avatar.Expired() { err := avatar.UpdateTimeout(time.Millisecond * 500) if err != nil { - debug(err) - //log.Trace("avatar update error: %v", err) + log.Trace("avatar update error: %v", err) } } if modtime, err := avatar.Modtime(); err == nil { @@ -177,8 +177,7 @@ func (this *avatarHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "image/jpeg") err := avatar.Encode(w, size) if err != nil { - //log.Warn("avatar encode error: %v", err) // will panic when err != nil - debug(err) + log.Warn("avatar encode error: %v", err) w.WriteHeader(500) } } @@ -251,7 +250,6 @@ func (this *thunderTask) Fetch() { var client = &http.Client{} func (this *thunderTask) fetch() error { - //log.Println("thunder, fetch", this.Url) req, _ := http.NewRequest("GET", this.Url, nil) req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8") req.Header.Set("Accept-Encoding", "gzip,deflate,sdch") diff --git a/modules/avatar/avatar_test.go b/modules/avatar/avatar_test.go index a337959c..4656d6f0 100644 --- a/modules/avatar/avatar_test.go +++ b/modules/avatar/avatar_test.go @@ -4,13 +4,14 @@ package avatar_test import ( - "log" + "errors" "os" "strconv" "testing" "time" "github.com/gogits/gogs/modules/avatar" + "github.com/gogits/gogs/modules/log" ) const TMPDIR = "test-avatar" @@ -28,7 +29,7 @@ func TestFetchMany(t *testing.T) { os.Mkdir(TMPDIR, 0755) defer os.RemoveAll(TMPDIR) - log.Println("start") + t.Log("start") var n = 5 ch := make(chan bool, n) for i := 0; i < n; i++ { @@ -36,14 +37,14 @@ func TestFetchMany(t *testing.T) { hash := avatar.HashEmail(strconv.Itoa(i) + "ssx205@gmail.com") a := avatar.New(hash, TMPDIR) a.Update() - log.Println("finish", hash) + t.Log("finish", hash) ch <- true }(i) } for i := 0; i < n; i++ { <-ch } - log.Println("end") + t.Log("end") } // cat @@ -54,3 +55,7 @@ func TestHttp(t *testing.T) { http.ListenAndServe(":8001", nil) } */ + +func TestLogTrace(t *testing.T) { + log.Trace("%v", errors.New("console log test")) +}