-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
61 lines (50 loc) · 1.26 KB
/
log.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
"sync/atomic"
"time"
)
var (
logToFile = log.New(io.Discard, "", 0)
logfile *os.File
)
func setupLogs() {
if options.Log {
if err := os.MkdirAll(filepath.Clean(options.LogAt), os.ModePerm); err != nil {
log.Panicf("failed to open logfile directory, error\n:%s", err.Error())
}
logfileName := strings.ReplaceAll(time.Now().Local().Format(time.DateTime)+".log", ":", "-")
file, err := os.OpenFile(filepath.Join(options.LogAt, logfileName), os.O_CREATE|os.O_APPEND|os.O_RDWR, os.ModePerm)
if err != nil {
log.Panicf("failed to open logfile, error\n:%s", err.Error())
}
logfile = file
logToFile = log.New(logfile, "", 0)
}
log.SetPrefix("\r")
log.SetFlags(log.Ltime)
}
func closeLogs() {
if logfile != nil {
logfile.Close()
}
}
var (
imagesRead, totalReads, dirEntriesFound atomic.Uint32
start time.Time
)
func progress() {
start = time.Now()
for {
time.Sleep(time.Second)
seconds := time.Since(start).Seconds()
total := totalReads.Load()
images := imagesRead.Load()
fmt.Printf("\rdirectory entries scanned: %d (%.2f/s); thereof images: %d (%.2f/s)", total, float64(total)/seconds, images, float64(images)/seconds)
}
}