From 01c16b7699d79ddb40e8e51d4bceb22d7823de7a Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Sat, 8 Mar 2025 11:14:09 +0100 Subject: [PATCH] Fix up logging a little To maake it pipeable --- main.go | 52 +++++++++++++++++++--------------------------------- 1 file changed, 19 insertions(+), 33 deletions(-) diff --git a/main.go b/main.go index 9d04f05..b67dfeb 100644 --- a/main.go +++ b/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) - log.Printf("Scanning directory: %s", 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 { - log.Printf("Error opening file %s: %v", file, err) + if !*raw { + log.Printf("Error opening file %s: %v", file, err) + } return } @@ -72,7 +77,9 @@ func main() { //log.Printf("Text file: %s (%s)", file, ext) } } else if err := scanner.Err(); err != nil { - log.Printf("Error reading line from file %s: %v", file, err) + if !*raw { + log.Printf("Error reading line from file %s: %v", file, err) + } } f.Close() @@ -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 }