Fix up logging a little
To maake it pipeable
This commit is contained in:
46
main.go
46
main.go
@@ -35,13 +35,16 @@ type ExtData struct {
|
||||
}
|
||||
|
||||
func main() {
|
||||
raw := flag.Bool("r", false, "More application friendly output")
|
||||
flag.Parse()
|
||||
dir := flag.Arg(0)
|
||||
if dir == "" {
|
||||
dir = "."
|
||||
}
|
||||
dir = NormalizePath(dir)
|
||||
if !*raw {
|
||||
log.Printf("Scanning directory: %s", dir)
|
||||
}
|
||||
|
||||
files := make(chan string, 10000)
|
||||
status := make(chan error)
|
||||
@@ -56,7 +59,9 @@ func main() {
|
||||
defer wg.Done()
|
||||
f, err := os.Open(file)
|
||||
if err != nil {
|
||||
if !*raw {
|
||||
log.Printf("Error opening file %s: %v", file, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -72,8 +77,10 @@ func main() {
|
||||
//log.Printf("Text file: %s (%s)", file, ext)
|
||||
}
|
||||
} else if err := scanner.Err(); err != nil {
|
||||
if !*raw {
|
||||
log.Printf("Error reading line from file %s: %v", file, err)
|
||||
}
|
||||
}
|
||||
|
||||
f.Close()
|
||||
}(file)
|
||||
@@ -82,8 +89,15 @@ func main() {
|
||||
|
||||
extensionTypeCount.Range(func(key, value any) bool {
|
||||
extData := value.(*ExtData)
|
||||
if extData.ext == "" {
|
||||
return true
|
||||
}
|
||||
if extData.binaryCount > extData.textCount*2 {
|
||||
log.Printf("Extension: %s, Binary Count: %d, Text Count: %d", extData.ext, extData.binaryCount, extData.textCount)
|
||||
if *raw {
|
||||
fmt.Println(extData.ext)
|
||||
} else {
|
||||
log.Printf("Extension: %q, Binary Count: %d, Text Count: %d", extData.ext, extData.binaryCount, extData.textCount)
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
@@ -106,9 +120,6 @@ func GetSyncFilesRecursively(input string, output chan string, status chan error
|
||||
var foldersProcessed int32
|
||||
var activeWorkers int32
|
||||
|
||||
progressTicker := time.NewTicker(200 * time.Millisecond)
|
||||
defer progressTicker.Stop()
|
||||
|
||||
done := make(chan struct{})
|
||||
defer close(done)
|
||||
|
||||
@@ -116,27 +127,6 @@ func GetSyncFilesRecursively(input string, output chan string, status chan error
|
||||
workerPool := make(chan struct{}, 4000)
|
||||
directories <- input
|
||||
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-progressTicker.C:
|
||||
dirCount := len(directories)
|
||||
workers := atomic.LoadInt32(&activeWorkers)
|
||||
fmt.Printf("\rFiles processed: %8d; Folders processed: %8d; Active workers: %8d; Directory queue: %8d",
|
||||
atomic.LoadInt32(&filesProcessed),
|
||||
atomic.LoadInt32(&foldersProcessed),
|
||||
workers,
|
||||
dirCount)
|
||||
case <-done:
|
||||
// Final progress update
|
||||
fmt.Printf("\nFiles processed: %8d; Folders processed: %8d; Completed successfully\n",
|
||||
atomic.LoadInt32(&filesProcessed),
|
||||
atomic.LoadInt32(&foldersProcessed))
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
allDone := make(chan struct{})
|
||||
|
||||
go func() {
|
||||
@@ -179,16 +169,12 @@ func GetSyncFilesRecursively(input string, output chan string, status chan error
|
||||
}()
|
||||
|
||||
<-allDone
|
||||
|
||||
log.Printf("Files processed: %d; Folders processed: %d",
|
||||
atomic.LoadInt32(&filesProcessed),
|
||||
atomic.LoadInt32(&foldersProcessed))
|
||||
}
|
||||
|
||||
func processDirectory(directory string, directories chan<- string, output chan<- string, filesProcessed *int32) {
|
||||
files, err := os.ReadDir(directory)
|
||||
if err != nil {
|
||||
log.Printf("Error reading directory %s: %+v", directory, err)
|
||||
//log.Printf("Error reading directory %s: %+v", directory, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user