From 9731c6b1415367484395f4b1769c9d9f556ab804 Mon Sep 17 00:00:00 2001 From: Libo Huang Date: Fri, 19 Apr 2024 10:28:58 +0800 Subject: [PATCH] add upload error log --- commands.go | 21 +++++++++++++++++++++ session.go | 8 +++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/commands.go b/commands.go index c555264..92126d8 100644 --- a/commands.go +++ b/commands.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "fmt" + "log" "os" "path" "path/filepath" @@ -343,6 +344,15 @@ func NewPutCommand() cli.Command { if c.Int("w") > 10 || c.Int("w") < 1 { PrintErrorAndExit("max concurrent threads must between (1 - 10)") } + errLog := c.String("err-log") + if errLog != "" { + f, err := os.OpenFile(errLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + PrintErrorAndExit("open error log file: %v", err) + } + defer f.Close() + log.SetOutput(f) + } session.Put( localPath, upPath, @@ -354,6 +364,7 @@ func NewPutCommand() cli.Command { Flags: []cli.Flag{ cli.IntFlag{Name: "w", Usage: "max concurrent threads", Value: 5}, cli.BoolFlag{Name: "all", Usage: "upload all files including hidden files"}, + cli.StringFlag{Name: "err-log", Usage: "upload file error log to file"}, }, } } @@ -372,6 +383,15 @@ func NewUploadCommand() cli.Command { if isWindowsGOOS() { filenames = globFiles(filenames) } + errLog := c.String("err-log") + if errLog != "" { + f, err := os.OpenFile(errLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + PrintErrorAndExit("open error log file: %v", err) + } + defer f.Close() + log.SetOutput(f) + } session.Upload( filenames, c.String("remote"), @@ -384,6 +404,7 @@ func NewUploadCommand() cli.Command { cli.BoolFlag{Name: "all", Usage: "upload all files including hidden files"}, cli.IntFlag{Name: "w", Usage: "max concurrent threads", Value: 5}, cli.StringFlag{Name: "remote", Usage: "remote path", Value: "./"}, + cli.StringFlag{Name: "err-log", Usage: "upload file error log to file"}, }, } } diff --git a/session.go b/session.go index 9bc9f9a..fb25f82 100644 --- a/session.go +++ b/session.go @@ -648,12 +648,18 @@ func (sess *Session) putDir(localPath, upPath string, workers int, withIgnore bo for info := range localFiles { rel, _ := filepath.Rel(localAbsPath, info.fpath) desPath := path.Join(upPath, filepath.ToSlash(rel)) - if fInfo, err := os.Stat(info.fpath); err == nil && fInfo.IsDir() { + fInfo, err := os.Stat(info.fpath) + if err == nil && fInfo.IsDir() { err = sess.updriver.Mkdir(desPath) } else { err = sess.putFileWithProgress(info.fpath, desPath, info.fInfo) } if err != nil { + log.Printf("put %s to %s error: %s", info.fpath, desPath, err) + if upyun.IsTooManyRequests(err) { + time.Sleep(time.Second) + continue + } return } }