Fix up logging a little

To maake it pipeable
This commit is contained in:
2025-03-08 11:14:09 +01:00
parent cc9b99b34c
commit 01c16b7699

46
main.go
View File

@@ -35,13 +35,16 @@ type ExtData struct {
} }
func main() { func main() {
raw := flag.Bool("r", false, "More application friendly output")
flag.Parse() flag.Parse()
dir := flag.Arg(0) dir := flag.Arg(0)
if dir == "" { if dir == "" {
dir = "." dir = "."
} }
dir = NormalizePath(dir) dir = NormalizePath(dir)
if !*raw {
log.Printf("Scanning directory: %s", dir) log.Printf("Scanning directory: %s", dir)
}
files := make(chan string, 10000) files := make(chan string, 10000)
status := make(chan error) status := make(chan error)
@@ -56,7 +59,9 @@ func main() {
defer wg.Done() defer wg.Done()
f, err := os.Open(file) f, err := os.Open(file)
if err != nil { if err != nil {
if !*raw {
log.Printf("Error opening file %s: %v", file, err) log.Printf("Error opening file %s: %v", file, err)
}
return return
} }
@@ -72,8 +77,10 @@ func main() {
//log.Printf("Text file: %s (%s)", file, ext) //log.Printf("Text file: %s (%s)", file, ext)
} }
} else if err := scanner.Err(); err != nil { } else if err := scanner.Err(); err != nil {
if !*raw {
log.Printf("Error reading line from file %s: %v", file, err) log.Printf("Error reading line from file %s: %v", file, err)
} }
}
f.Close() f.Close()
}(file) }(file)
@@ -82,8 +89,15 @@ func main() {
extensionTypeCount.Range(func(key, value any) bool { extensionTypeCount.Range(func(key, value any) bool {
extData := value.(*ExtData) extData := value.(*ExtData)
if extData.ext == "" {
return true
}
if extData.binaryCount > extData.textCount*2 { 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 return true
}) })
@@ -106,9 +120,6 @@ func GetSyncFilesRecursively(input string, output chan string, status chan error
var foldersProcessed int32 var foldersProcessed int32
var activeWorkers int32 var activeWorkers int32
progressTicker := time.NewTicker(200 * time.Millisecond)
defer progressTicker.Stop()
done := make(chan struct{}) done := make(chan struct{})
defer close(done) defer close(done)
@@ -116,27 +127,6 @@ func GetSyncFilesRecursively(input string, output chan string, status chan error
workerPool := make(chan struct{}, 4000) workerPool := make(chan struct{}, 4000)
directories <- input 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{}) allDone := make(chan struct{})
go func() { go func() {
@@ -179,16 +169,12 @@ func GetSyncFilesRecursively(input string, output chan string, status chan error
}() }()
<-allDone <-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) { func processDirectory(directory string, directories chan<- string, output chan<- string, filesProcessed *int32) {
files, err := os.ReadDir(directory) files, err := os.ReadDir(directory)
if err != nil { if err != nil {
log.Printf("Error reading directory %s: %+v", directory, err) //log.Printf("Error reading directory %s: %+v", directory, err)
return return
} }