debug output improved

This commit is contained in:
milarin 2023-01-15 19:43:36 +01:00
parent 6a25a13b3e
commit a1947c92a5
5 changed files with 69 additions and 30 deletions

View File

@ -1,6 +1,8 @@
package main package main
import "io" import (
"io"
)
func NewWriterFromByteChan(ch chan byte) *ByteChanWriter { func NewWriterFromByteChan(ch chan byte) *ByteChanWriter {
return &ByteChanWriter{ch} return &ByteChanWriter{ch}

25
filehandle.go Normal file
View File

@ -0,0 +1,25 @@
package main
import (
"io"
)
type FileHandle struct {
File string
Chan chan byte
Writer io.Writer
}
func (fh *FileHandle) Close() error {
close(fh.Chan)
return nil
}
func NewFileHandle(path string) *FileHandle {
out := make(chan byte, 4096)
return &FileHandle{
File: path,
Chan: out,
Writer: NewWriterFromByteChan(out),
}
}

View File

@ -14,21 +14,27 @@ import (
"github.com/fatih/color" "github.com/fatih/color"
) )
func HandleFile(path string) <-chan byte { func HandleFile(fh *FileHandle) {
out := make(chan byte, 1024) defer fh.Close()
defer close(out) path := fh.File
w := NewWriterFromByteChan(out) w := fh.Writer
fmt.Fprint(w, color.MagentaString("%s file found: %s\n", time.Now().Format("2006-01-02 15:04:05"), path)) fmt.Fprint(w, color.MagentaString("%s file found: %s\n", time.Now().Format("2006-01-02 15:04:05"), path))
for _, parser := range parsers.Parsers { fmt.Fprint(w, "\ttry parsers: ")
for i, parser := range parsers.Parsers {
parsedFile, ok := parser.FileParser(&parser, path) parsedFile, ok := parser.FileParser(&parser, path)
if !ok { if !ok {
fmt.Fprint(w, color.YellowString("\tnot parsable with parser '%s'\n", parser.Identity)) fmt.Fprint(w, color.YellowString(parser.Identity))
if i < len(parsers.Parsers)-1 {
fmt.Fprint(w, ", ")
} else {
fmt.Fprintln(w, color.RedString("\n\tfile ignored"))
}
continue continue
} }
fmt.Fprint(w, color.GreenString("\tparsable with parser '%s'\n", parser.Identity)) fmt.Fprintln(w, color.GreenString(parser.Identity))
anime, err := logic.SearchAnimeByTitle(parsedFile.OriginalAnimeTitle) anime, err := logic.SearchAnimeByTitle(parsedFile.OriginalAnimeTitle)
if err != nil { if err != nil {
@ -40,8 +46,6 @@ func HandleFile(path string) <-chan byte {
HandleParsedFile(w, parsedFile) HandleParsedFile(w, parsedFile)
break break
} }
return out
} }
func HandleParsedFile(w io.Writer, parsedFile *model.ParsedFile) { func HandleParsedFile(w io.Writer, parsedFile *model.ParsedFile) {
@ -50,12 +54,12 @@ func HandleParsedFile(w io.Writer, parsedFile *model.ParsedFile) {
// debug output // debug output
if animeEpNotExistLocally { if animeEpNotExistLocally {
fmt.Fprint(w, color.YellowString("\tfile exists locally\n")) fmt.Fprintln(w, "\tfile exists locally")
fmt.Fprint(w, color.YellowString("\t local file: %s\n", FilePrio2Str(oldFilePrio))) fmt.Fprintf(w, "\t local file: %s\n", FilePrio2Str(oldFilePrio))
fmt.Fprint(w, color.YellowString("\t new file: %s\n", FilePrio2Str(newFilePrio))) fmt.Fprintf(w, "\t new file: %s\n", FilePrio2Str(newFilePrio))
if newFilePrio.Priority > oldFilePrio.Priority { if newFilePrio.Priority > oldFilePrio.Priority {
fmt.Fprint(w, color.GreenString("\t overwrite local file\n")) fmt.Fprint(w, color.GreenString("\t overwrite local file\n"))
} else { } else if !DeleteLowPriorityFiles {
fmt.Fprint(w, color.YellowString("\t ignore new file\n")) fmt.Fprint(w, color.YellowString("\t ignore new file\n"))
} }
} }
@ -87,7 +91,7 @@ func OrganizeAnimeEpisode(w io.Writer, parsedFile *model.ParsedFile) error {
newFile := logic.GetAnimeEpFilepath(parsedFile.AnimeEpisode(), filepath.Ext(parsedFile.File)) newFile := logic.GetAnimeEpFilepath(parsedFile.AnimeEpisode(), filepath.Ext(parsedFile.File))
lockFile := logic.GetAnimeEpFilepath(parsedFile.AnimeEpisode(), "lock") lockFile := logic.GetAnimeEpFilepath(parsedFile.AnimeEpisode(), "lock")
fmt.Fprint(w, color.BlueString("\tmove file\n\t from: '%s'\n\t to: '%s'\n", oldFile, newFile)) fmt.Fprintf(w, "\tmove file\n\t from: '%s'\n\t to: '%s'\n", oldFile, newFile)
if err := os.MkdirAll(filepath.Dir(newFile), os.ModePerm); err != nil { if err := os.MkdirAll(filepath.Dir(newFile), os.ModePerm); err != nil {
return err return err
@ -130,7 +134,7 @@ func OrganizeAnimeEpisode(w io.Writer, parsedFile *model.ParsedFile) error {
return err return err
} }
fmt.Fprint(w, color.BlueString("\t done (copied %s in %s)\n", FormatBytes(written), time.Since(start).Truncate(100*time.Millisecond))) fmt.Fprintf(w, "\t done (copied %s in %s)\n", FormatBytes(written), time.Since(start).Truncate(100*time.Millisecond))
return nil return nil
} }

19
main.go
View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"os"
"os/exec" "os/exec"
"git.milar.in/milarin/channel" "git.milar.in/milarin/channel"
@ -9,6 +8,8 @@ import (
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
) )
var Runner = InitializeRunner()
func main() { func main() {
// check for ffprobe in PATH // check for ffprobe in PATH
if _, err := exec.LookPath("ffprobe"); err != nil { if _, err := exec.LookPath("ffprobe"); err != nil {
@ -22,18 +23,18 @@ func main() {
logic.PrintPriorityTables() logic.PrintPriorityTables()
fsChan, err := WatchDirectory(fsnotify.Create, DownloadPath) fileChan, err := WatchDirectory(fsnotify.Create, DownloadPath)
if err != nil { if err != nil {
panic(err) // TODO error handling panic(err) // TODO error handling
} }
outputChan := channel.MapWithRunner(fsChan, InitializeRunner(), HandleFile) fileHandleChan := channel.Map(fileChan, NewFileHandle)
channel.Each(outputChan, PrintByteChanFunc(os.Stdout)) workChan, logChan := channel.Tee(fileHandleChan)
go channel.Each(workChan, HandleFileInRunner)
channel.Each(logChan, PrintFileHandle)
} }
func InitializeRunner() channel.Runner { func HandleFileInRunner(fh *FileHandle) {
if ThreadCount <= 0 { Runner.Run(func() { HandleFile(fh) })
return channel.NewUnlimitedRunner()
}
return channel.NewLimitedRunner(ThreadCount)
} }

View File

@ -3,10 +3,19 @@ package main
import ( import (
"fmt" "fmt"
"io" "io"
"os"
"git.milar.in/milarin/channel"
"git.milar.in/milarin/gmath" "git.milar.in/milarin/gmath"
) )
func InitializeRunner() channel.Runner {
if ThreadCount <= 0 {
return channel.NewUnlimitedRunner()
}
return channel.NewLimitedRunner(ThreadCount)
}
func FormatBytes[T gmath.Integer](bytes T) string { func FormatBytes[T gmath.Integer](bytes T) string {
value := float64(bytes) value := float64(bytes)
@ -34,10 +43,8 @@ func PrintByteChan(w io.Writer, ch <-chan byte) error {
return nil return nil
} }
func PrintByteChanFunc(w io.Writer) func(ch <-chan byte) { func PrintFileHandle(fh *FileHandle) {
return func(ch <-chan byte) { if err := PrintByteChan(os.Stdout, fh.Chan); err != nil {
if err := PrintByteChan(w, ch); err != nil { panic(err) // TODO error handling
panic(err) // TODO error handling
}
} }
} }