Skip to content

Commit

Permalink
Adding basic analysis and organizing the logs better
Browse files Browse the repository at this point in the history
  • Loading branch information
spf13 committed May 16, 2019
1 parent 43650bf commit fb03458
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion file-util.go
Expand Up @@ -166,7 +166,7 @@ func getAllFilePaths(dir string) []string {
if validFileType(fullPath) {
filePaths = append(filePaths, path.Join(fullPath))
} else {
Info.Println(fullPath, "\tskipping, unrecognized filetype")
Info.Println(fullPath, "\t skipping, unrecognized filetype")
}
}
}
Expand Down
28 changes: 20 additions & 8 deletions main.go
Expand Up @@ -9,6 +9,8 @@ import (
"log"
"os"
"regexp"
"text/tabwriter"
"time"
)

var (
Expand All @@ -18,6 +20,7 @@ var (
mvDuplicates bool
tinyFiles bool
dryRun bool
analyze bool
logPath string
version = "undefined"
reDateTime = regexp.MustCompile(`(\d{4}):(\d{2}):(\d{2}) (\d{2}):(\d{2}):(\d{2})`)
Expand All @@ -38,6 +41,7 @@ func init() {
mvPtr := flag.Bool("move-dupes", false, "Move duplicates to their correct location")
tinyPtr := flag.Bool("copy-tiny", false, "Copy really small images (<5kb)")
dryPtr := flag.Bool("dryrun", false, "Don't actually do anything")
analyzePtr := flag.Bool("analyze", false, "Track how long operations are taking")

flag.Parse()

Expand All @@ -52,30 +56,33 @@ func init() {
tinyFiles = *tinyPtr
logPath = *logPtr
dryRun = *dryPtr
analyze = *analyzePtr

inputPath = flag.Args()[0]
}

func main() {

logFile, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Fatalln("Failed to open log file", logPath, ":", err)
}

multiWarn := io.MultiWriter(logFile, ioutil.Discard)
multiErr := io.MultiWriter(logFile, os.Stderr)
wr := tabwriter.NewWriter(logFile, 10, 8, 3, ' ', 0)
multiWarn := io.MultiWriter(wr, ioutil.Discard)
multiErr := io.MultiWriter(wr, os.Stderr)

Info = log.New(logFile, "INFO: \t", log.Ldate|log.Ltime)
Warn = log.New(multiWarn, "WARN: \t", log.Ldate|log.Ltime)
Error = log.New(multiErr, "ERROR: \t", log.Ldate|log.Ltime|log.Lshortfile)
Info = log.New(wr, "INFO: ", log.Ldate|log.Ltime)
Warn = log.New(multiWarn, "WARN: ", log.Ldate|log.Ltime)
Error = log.New(multiErr, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile)
Info.Println("************************************************")
if dryRun {
Info.Println(" * * * * DRY RUN * * * * ")
} else {
Info.Println(" > > > > NEW RUN < < < < ")
}
Info.Println("************************************************")
}

func main() {
defer wr.Flush()

createDirIfNotExists(outputPath)

Expand Down Expand Up @@ -165,3 +172,8 @@ func getMediaFiles(paths []string, processMetaData bool) map[[20]byte]*MediaFile

return outputMap
}

func timeTrack(start time.Time, name string) {
elapsed := time.Since(start)
log.Printf("%s took %s", name, elapsed)
}
7 changes: 7 additions & 0 deletions media-file.go
Expand Up @@ -47,6 +47,7 @@ func NewMediaFile(path string, processMetaData bool) *MediaFile {
return nil
}

startSHA := time.Now()
bytes := make([]byte, 4000000)

// Only read the first 4 MB of large files
Expand All @@ -62,6 +63,9 @@ func NewMediaFile(path string, processMetaData bool) *MediaFile {
return nil
}
}
if analyze {
timeTrack(startSHA, "SHA Generation")
}

mediaFile := &MediaFile{
path: path,
Expand Down Expand Up @@ -121,6 +125,9 @@ func (m *MediaFile) Error(input ...string) {
}

func (m *MediaFile) processMetaData(file *os.File) {
if analyze {
defer timeTrack(time.Now(), "EXIF analysis")
}
// fmt.Println(m.path)

var d *time.Time
Expand Down

0 comments on commit fb03458

Please sign in to comment.