fix: client log is not recorded (#141)

This commit is contained in:
Li Wei authored and GitHub committed 2023-07-06 17:15:48 +08:00
1 parent 9249613601
commit abe3cec13d
6 files changed
+23 -37

No files matched your search

+7 -17
View File
@@ -72,7 +72,7 @@ func NewDownloader(cfg *DownloaderConfig) *Downloader {
}
}
logPanic(cfg.StorageDir)
logPanic(filepath.Join(cfg.StorageDir, "logs"))
return d
}
@@ -640,22 +640,12 @@ func (d *Downloader) doContinue(task *Task) (err error) {
// redirect stderr to log file, when panic happened log it
func logPanic(logDir string) {
fp := filepath.Join(logDir, "crash.log")
// if log file not exists, create it
var f *os.File
if _, err := os.Stat(fp); err != nil {
if !os.IsNotExist(err) {
return
}
f, err = os.Create(fp)
if err != nil {
return
}
} else {
f, err = os.OpenFile(fp, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return
}
if err := util.CreateDirIfNotExist(logDir); err != nil {
return
}
f, err := os.Create(filepath.Join(logDir, "crash.log"))
if err != nil {
return
}
paniclog.RedirectStderr(f)
}
+4 -13
View File
@@ -3,7 +3,6 @@ package util
import (
"errors"
"fmt"
"io"
"os"
syspath "path"
"strings"
@@ -83,17 +82,9 @@ func GetSingleDir(paths []string) string {
return dir
}
// check directory is empty
func isEmpty(name string) (bool, error) {
f, err := os.Open(name)
if err != nil {
return false, err
func CreateDirIfNotExist(dir string) error {
if _, err := os.Stat(dir); os.IsNotExist(err) {
return os.MkdirAll(dir, 0o777)
}
defer f.Close()
_, err = f.Readdirnames(1) // Or f.Readdir(1)
if err == io.EOF {
return true, nil
}
return false, err // Either not empty or error, suits both cases
return nil
}