Skip to content

Commit

Permalink
支持调试日志
Browse files Browse the repository at this point in the history
  • Loading branch information
orestonce committed May 23, 2024
1 parent 656c545 commit 89d1caf
Show file tree
Hide file tree
Showing 7 changed files with 353 additions and 269 deletions.
16 changes: 16 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ func (this *DownloadEnv) StartDownload(req StartDownload_Req) (errMsg string) {
//this.cancelFn()
this.status.IsRunning = false
this.status.Locker.Unlock()

this.logFileLocker.Lock()
if this.logFile != nil {
this.logFile.Close()
this.logFile = nil
}
this.logFileLocker.Unlock()
}()
return ""
}
Expand Down Expand Up @@ -237,6 +244,15 @@ func (this *DownloadEnv) runDownload(req StartDownload_Req, skipList []SkipTsUni
}
}

if req.DebugLog {
this.logFile, err = os.OpenFile(filepath.Join(downloadDir, "debug.txt"), os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
this.setErrMsg("os.WriteUrl error: " + err.Error())
return
}
this.logToFile("m3u8 url: " + req.M3u8Url)
}

beginSeq := parseBeginSeq(m3u8Body)
// 获取m3u8地址的内容体
encInfo, err := this.getEncryptInfo(req.M3u8Url, string(m3u8Body))
Expand Down
1 change: 1 addition & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func init() {
downloadCmd.Flags().IntVarP(&gRunReq.ThreadCount, "ThreadCount", "", 8, "下载线程数")
downloadCmd.Flags().BoolVarP(&gRunReq.SkipMergeTs, "SkipMergeTs", "", false, "不合并ts为mp4")
downloadCmd.Flags().BoolVarP(&gRunReq.Skip_EXT_X_DISCONTINUITY, "Skip_EXT_X_DISCONTINUITY", "", false, "跳过 #EXT-X-DISCONTINUITY 标签包裹的ts")
downloadCmd.Flags().BoolVarP(&gRunReq.DebugLog, "DebugLog", "", false, "调试日志")
rootCmd.AddCommand(downloadCmd)
curlCmd.DisableFlagParsing = true
rootCmd.AddCommand(curlCmd)
Expand Down
59 changes: 53 additions & 6 deletions download.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"io"
"io/ioutil"
"net/http"
"net/http/httputil"
"net/url"
"os"
"path"
Expand Down Expand Up @@ -60,15 +61,18 @@ type StartDownload_Req struct {
SkipCacheCheck bool // 不缓存已下载的m3u8的文件信息
SkipMergeTs bool // 不合并ts为mp4
Skip_EXT_X_DISCONTINUITY bool // 跳过 #EXT-X-DISCONTINUITY 标签包裹的ts
DebugLog bool // 调试日志
}

type DownloadEnv struct {
cancelFn func()
ctx context.Context
nowClient *http.Client
header http.Header
sleepTh int32
status SpeedStatus
cancelFn func()
ctx context.Context
nowClient *http.Client
header http.Header
sleepTh int32
status SpeedStatus
logFile *os.File
logFileLocker sync.Mutex
}

func parseBeginSeq(body []byte) uint64 {
Expand Down Expand Up @@ -392,22 +396,65 @@ func (this *DownloadEnv) doGetRequest(urlS string) (data []byte, err error) {
}
req = req.WithContext(this.ctx)
req.Header = this.header

var logBuf *bytes.Buffer

this.logFileLocker.Lock()
if this.logFile != nil {
logBuf = bytes.NewBuffer(nil)
reqBytes, _ := httputil.DumpRequest(req, false)
logBuf.WriteString("httpReq:\n" + string(reqBytes) + "\n")
}
this.logFileLocker.Unlock()

resp, err := this.nowClient.Do(req)
if logBuf != nil && resp != nil {
respBytes, _ := httputil.DumpResponse(resp, false)
logBuf.WriteString("httpResp:\n" + string(respBytes) + "\n")
}

if err != nil {
if logBuf != nil {
logBuf.WriteString("error1:" + err.Error() + "\n")
this.logToFile(logBuf.String())
}
return nil, err
}
defer resp.Body.Close()

content, err := io.ReadAll(resp.Body)
if err != nil {
if logBuf != nil {
logBuf.WriteString("error2:" + err.Error() + "\n")
this.logToFile(logBuf.String())
}
return nil, err
}
if resp.StatusCode != 200 {
if logBuf != nil {
logBuf.WriteString("error3\n")
this.logToFile(logBuf.String())
}
return content, errors.New("resp.Status: " + resp.Status + " " + urlS)
}
if logBuf != nil {
this.logToFile(logBuf.String())
}
return content, nil
}

func (this *DownloadEnv) logToFile(body string) {
this.logFileLocker.Lock()
defer this.logFileLocker.Unlock()

if this.logFile == nil {
return
}

timeStr := time.Now().Format("2006-01-02_15:04:05")
fmt.Fprintf(this.logFile, "===>time: %s\n%s\n", timeStr, body)
}

func (this *DownloadEnv) GetIsCancel() bool {
this.status.Locker.Lock()
defer this.status.Locker.Unlock()
Expand Down
Loading

0 comments on commit 89d1caf

Please sign in to comment.