From 3c99a8c654a4864066261b555c4e4cbd964f7ae7 Mon Sep 17 00:00:00 2001 From: fumiama Date: Tue, 10 Aug 2021 17:22:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=B1=82=E7=BA=A7log?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 ++++- go.mod | 2 ++ go.sum | 16 ++++++++++++++++ imgdiff.go | 1 - storage.go | 35 ++++++++++++++++++++++++++++------- 5 files changed, 50 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 562b596..d8f59f9 100644 --- a/README.md +++ b/README.md @@ -18,4 +18,7 @@ Pick a random image ### func Saveimg(r io.Reader, imgdir string, uid string) string Save image into imgdir with name like 编码后哈希.webp ### func Addimage(name string) -manually add an image name into map \ No newline at end of file +manually add an image name into map +### func Imgexsits(name string) bool +Return whether the name is in map +### func Setloglevel(level log.Level) \ No newline at end of file diff --git a/go.mod b/go.mod index d9cfb18..b0ef1d9 100644 --- a/go.mod +++ b/go.mod @@ -6,4 +6,6 @@ require ( github.com/corona10/goimagehash v1.0.3 github.com/fumiama/go-base16384 v1.1.0 github.com/kolesa-team/go-webp v1.0.0 + github.com/sirupsen/logrus v1.8.1 + github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816 ) diff --git a/go.sum b/go.sum index 088ec56..7c8bc0e 100644 --- a/go.sum +++ b/go.sum @@ -1,13 +1,29 @@ github.com/corona10/goimagehash v1.0.3 h1:NZM518aKLmoNluluhfHGxT3LGOnrojrxhGn63DR/CZA= github.com/corona10/goimagehash v1.0.3/go.mod h1:VkvE0mLn84L4aF8vCb6mafVajEb6QYMHl2ZJLn0mOGI= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fumiama/go-base16384 v1.1.0 h1:4npregEpgJ4HtHYNMcw7eabSSO8GIqfG3xGZWtcYcIA= github.com/fumiama/go-base16384 v1.1.0/go.mod h1:YCaxEQyBX717X2lMlJvdccNhA83zhKRR/ipPOIIIQj8= github.com/kolesa-team/go-webp v1.0.0 h1:HFE7NhW1jkM59HgUwRdcSJ+VVZz7F1RZk6VFY27YZpQ= github.com/kolesa-team/go-webp v1.0.0/go.mod h1:P0sDD6OLl4jBVcwCcYRuu1C+kUrR4V19LYWs+yV3UcY= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ= github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= +github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= +github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816 h1:J6v8awz+me+xeb/cUTotKgceAYouhIB3pjzgRd6IlGk= +github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816/go.mod h1:tzym/CEb5jnFI+Q0k4Qq3+LvRF4gO3E2pxS8fHP8jcA= golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d h1:RNPAfi2nHY7C2srAV8A49jpsYr0ADedCk1wq6fTMTvs= golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/imgdiff.go b/imgdiff.go index dcc3774..46679f4 100644 --- a/imgdiff.go +++ b/imgdiff.go @@ -1,4 +1,3 @@ -// Package imago 图片处理相关 package imago import ( diff --git a/storage.go b/storage.go index 63c0c0a..435c179 100644 --- a/storage.go +++ b/storage.go @@ -1,8 +1,8 @@ +// Package imago Image saving & comparing tool for go based on webp. package imago import ( "bytes" - "fmt" "image" "io" "math/rand" @@ -14,6 +14,8 @@ import ( "github.com/kolesa-team/go-webp/decoder" "github.com/kolesa-team/go-webp/encoder" "github.com/kolesa-team/go-webp/webp" + log "github.com/sirupsen/logrus" + easy "github.com/t-tomalak/logrus-easy-formatter" ) var ( @@ -21,6 +23,20 @@ var ( mutex sync.Mutex ) +func init() { + log.SetFormatter(&easy.Formatter{ + TimestampFormat: "2006-01-02 15:04:05", + LogFormat: "[imago][%time%][%lvl%]: %msg% \n", + }) + log.SetLevel(log.DebugLevel) +} + +// Setloglevel +func Setloglevel(level log.Level) { + log.SetLevel(level) +} + +// Imgexsits Return whether the name is in map func Imgexsits(name string) bool { index := name[:3] tail := name[3:] @@ -46,10 +62,10 @@ func Addimage(name string) { defer mutex.Unlock() if images[index] == nil { images[index] = make([]string, 0) - fmt.Println("[addimage] create index", index, ".") + log.Debugln("[addimage] create index", index, ".") } images[index] = append(images[index], tail) - fmt.Println("[addimage] index", index, "append file", tail, ".") + log.Debugln("[addimage] index", index, "append file", tail, ".") images["sum"] = append(images["sum"], name) } @@ -64,44 +80,49 @@ func Saveimgbytes(b []byte, imgdir string, uid string, force bool) string { if err == nil { iswebp = true } else { - fmt.Printf("[saveimg] decode image error: %v\n", err) + log.Errorf("[saveimg] decode image error: %v\n", err) return "\"stat\": \"notanimg\"" } } dh, err := GetDHashStr(img) if err != nil { + log.Errorf("[saveimg] get dhash error: %v\n", err) return "\"stat\": \"dherr\"" } if force && Imgexsits(dh) { + log.Debugf("[saveimg] force find similar image %s.\n", dh) return "\"stat\":\"exist\", \"img\": \"" + url.QueryEscape(dh) + "\"" } else { for _, name := range images["sum"] { diff, err := HammDistance(dh, name) if err == nil && diff < 10 { // 认为是一张图片 - fmt.Printf("[saveimg] old %s.\n", name) + log.Debugf("[saveimg] old %s.\n", name) return "\"stat\":\"exist\", \"img\": \"" + url.QueryEscape(name) + "\"" } } } f, err := os.Create(imgdir + dh + ".webp") if err != nil { + log.Errorf("[saveimg] create webp file error: %v\n", err) return "\"stat\": \"ioerr\"" } defer f.Close() if !iswebp { options, err := encoder.NewLossyEncoderOptions(encoder.PresetDefault, 75) if err != nil || webp.Encode(f, img, options) != nil { + log.Errorf("[saveimg] encode webp error: %v\n", err) return "\"stat\": \"encerr\"" } } else { r.Seek(0, io.SeekStart) c, err := io.Copy(f, r) if err != nil { + log.Errorf("[saveimg] copy file error: %v\n", err) return "\"stat\": \"ioerr\"" } - fmt.Printf("[saveimg] save %d bytes.\n", c) + log.Debugf("[saveimg] save %d bytes.\n", c) } - fmt.Printf("[saveimg] new %s.\n", dh) + log.Debugf("[saveimg] new %s.\n", dh) return "\"stat\":\"success\", \"img\": \"" + url.QueryEscape(dh) + "\"" }