Skip to content

Commit

Permalink
Update how logs are printed
Browse files Browse the repository at this point in the history
- The SimpleLogger class now handles all logs
- SimpleLogger uses fansi to use colors for different log levels
- Logs go to the stderr instead of stdout
  • Loading branch information
AlexITC committed Aug 30, 2020
1 parent a5666c6 commit cb7e87b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 25 deletions.
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ lazy val root = (project in file("."))
buildInfoUsePackageAsPath := true,
libraryDependencies ++= Seq(
"com.lihaoyi" %% "os-lib" % "0.7.1",
"com.lihaoyi" %% "fansi" % "0.2.7",
"com.google.guava" % "guava" % "28.0-jre",
"com.drewnoakes" % "metadata-extractor" % "2.14.0",
"com.monovore" %% "decline" % "1.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ object CommandAppHelper {

def run(source: os.Path, output: os.Path, dryRun: Boolean): Unit = {
val args = FileOrganizerTask.Arguments(inputRoot = source, outputBaseRoot = output, dryRun = dryRun)
new FileOrganizerTask().run(args)
val logger = new SimpleLogger
new FileOrganizerTask(logger).run(args)
}

def findPotentialDate(sourceFile: os.Path): Set[String] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,32 @@ object FileOrganizerTask {
}
}

class FileOrganizerTask {
class FileOrganizerTask(logger: SimpleLogger) {

import FileOrganizerTask._

def run(args: Arguments): Unit = {
validate(args)

println("Loading already processed files, it may take some minutes, be patient")
logger.info("Loading already processed files, it may take some minutes, be patient")
val (processedFiles, invalidProcessedFiles) = FileOrganizerService.load(args.outputRoot)(trackProgress)
println(s"Already processed files loaded: ${processedFiles.size}")
logger.info(s"Already processed files loaded: ${processedFiles.size}")
if (invalidProcessedFiles.nonEmpty) {
println(
s"WARNING: There are ${invalidProcessedFiles.size} files on the output folder without enough metadata to process, which you need to organize manually"
logger.warn(
s"There are ${invalidProcessedFiles.size} files on the output folder without enough metadata to process, which you need to organize manually"
)
}

println("Loading files to process, it may take some minutes, be patient")
logger.info("Loading files to process, it may take some minutes, be patient")
val (filesToProcess, invalidFilesToProcess) = FileOrganizerService.load(args.inputRoot)(trackProgress)
println(s"Files to process loaded: ${filesToProcess.size}")
logger.info(s"Files to process loaded: ${filesToProcess.size}")
if (invalidFilesToProcess.nonEmpty) {
println(
s"WARNING: There are ${invalidFilesToProcess.size} files on the input folder without enough metadata to process"
logger.warn(
s"There are ${invalidFilesToProcess.size} files on the input folder without enough metadata to process"
)
}

println(s"Indexing now... it may take some minutes, be patient")
logger.info(s"Indexing now... it may take some minutes, be patient")
val allFiles = filesToProcess.data.keys.foldLeft(processedFiles) {
case (acc, currentHash) =>
acc + filesToProcess.data.getOrElse(currentHash, List.empty)
Expand All @@ -61,33 +61,33 @@ class FileOrganizerTask {
}
}

println("Initial indexing done")
println(s"- Unique files: ${allFiles.size}")
println(s"- Already organized files: ${processedFiles.size}")
println(s"- New duplicated files: ${newDuplicated.size}")
println(s"- New unique files to organize: ${newUnique.size}")
println()
logger.info("Initial indexing done")
logger.info(s"- Unique files: ${allFiles.size}")
logger.info(s"- Already organized files: ${processedFiles.size}")
logger.info(s"- New duplicated files: ${newDuplicated.size}")
logger.info(s"- New unique files to organize: ${newUnique.size}")
logger.info("")

if (args.dryRun) {
println("Files not affected because dry-run is enabled")
logger.info("Files not affected because dry-run is enabled")
} else {
// Move duplicated files
println(s"Moving duplicated files to: ${args.duplicatedRoot}")
logger.info(s"Moving duplicated files to: ${args.duplicatedRoot}")
newDuplicated.zipWithIndex.foreach {
case (file, index) =>
trackProgress(current = index, total = newDuplicated.size)
FileOrganizerService.safeMove(destinationDirectory = args.duplicatedRoot, sourceFile = file.source)
}

// Move files without metadata
println(s"Moving invalid files to: ${args.invalidRoot}")
logger.info(s"Moving invalid files to: ${args.invalidRoot}")
invalidFilesToProcess.zipWithIndex.foreach {
case (file, index) =>
trackProgress(current = index, total = invalidFilesToProcess.size)
FileOrganizerService.safeMove(destinationDirectory = args.invalidRoot, sourceFile = file)
}

println(s"Organizing unique files to: ${args.outputRoot}")
logger.info(s"Organizing unique files to: ${args.outputRoot}")
newUnique.zipWithIndex.foreach {
case (file, index) =>
trackProgress(current = index, total = newDuplicated.size)
Expand All @@ -98,12 +98,12 @@ class FileOrganizerTask {
)
}

println("Cleaning up empty directories")
logger.info("Cleaning up empty directories")
FileOrganizerService.cleanEmptyDirectories(args.inputRoot)
FileOrganizerService.cleanEmptyDirectories(args.outputRoot)
}

println("Done")
logger.info("Done")
}

private def trackProgress(current: Int, total: Int): Unit = {
Expand All @@ -114,13 +114,13 @@ class FileOrganizerTask {
val currentPercent = percent(current)
val previous = percent(current - 1)
if (currentPercent > previous && currentPercent % 5 == 0) {
println(s"Progress: $currentPercent%")
logger.info(fansi.Color.Blue(s"Progress: $currentPercent%").render)
}
}
}

private def exit(msg: String): Unit = {
println(s"FATAL: $msg")
logger.fatal(s"FATAL: $msg")
sys.exit(1)
}

Expand Down
16 changes: 16 additions & 0 deletions src/main/scala/net/wiringbits/myphototimeline/SimpleLogger.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package net.wiringbits.myphototimeline

class SimpleLogger {

def info(msg: String): Unit = {
System.err.println(msg)
}

def warn(msg: String): Unit = {
System.err.println(s"${fansi.Color.Yellow("WARNING")}: $msg")
}

def fatal(msg: String): Unit = {
System.err.println(s"${fansi.Color.Red("FATAL")}: $msg")
}
}

0 comments on commit cb7e87b

Please sign in to comment.