From 11b248fc1ffa854c9a2f50b5fb62cdb29c2c8d5d Mon Sep 17 00:00:00 2001 From: Jarek Sacha Date: Wed, 7 Jun 2023 20:25:56 -0400 Subject: [PATCH] Log session to ~/.ijp_imagej_launcher.log #6 --- notes/v.0.2.0.md | 5 +++ .../ij_plugins/imagej_launcher/Logger.scala | 42 ++++++++++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 notes/v.0.2.0.md diff --git a/notes/v.0.2.0.md b/notes/v.0.2.0.md new file mode 100644 index 0000000..3be1e00 --- /dev/null +++ b/notes/v.0.2.0.md @@ -0,0 +1,5 @@ +### New Features + +* Use launcher location to infer `ij-dir`. #5 +* Session log is saved to `~/.ijp_imagej_launcher.log` to facilitate troubleshooting when not running from command + prompt. The log is reset for each session. #6 diff --git a/src/main/scala/ij_plugins/imagej_launcher/Logger.scala b/src/main/scala/ij_plugins/imagej_launcher/Logger.scala index baa5390..9963f40 100644 --- a/src/main/scala/ij_plugins/imagej_launcher/Logger.scala +++ b/src/main/scala/ij_plugins/imagej_launcher/Logger.scala @@ -5,20 +5,60 @@ package ij_plugins.imagej_launcher -import ij_plugins.imagej_launcher.Logger.Level +import ij_plugins.imagej_launcher.Logger.{Level, logToFile} +import os.Path + +import java.nio.file.{Files, StandardOpenOption} +import scala.util.control.NonFatal class Logger(val level: Level = Level.Info): + def debug(msg: String): Unit = pprint(Level.Debug, msg) def info(msg: String): Unit = pprint(Level.Info, msg) def error(msg: String): Unit = pprint(Level.Error, msg) private def pprint(l: Level, message: String): Unit = + val m = f"${l.name}%-5s: $message" if l.level <= level.level then println(f"${l.name}%-5s: $message") + logToFile(m) object Logger: + + private val LogFile: Path = os.home / ".ijp_imagej_launcher.log" + private var logFileReset: Boolean = true + enum Level(val level: Int, val name: String): case Off extends Level(0, "OFF") case Error extends Level(200, "ERROR") case Info extends Level(400, "INFO") case Debug extends Level(500, "DEBUG") case All extends Level(Int.MaxValue, "DEBUG") + + private def logToFile(message: String): Unit = + try + if logFileReset && os.exists(LogFile) then + val _ = os.remove(LogFile) + logFileReset = false + + val data = readableTimeStamp() + ": " + message + "\n" + val lines = data.getBytes() + val _ = Files.write( + LogFile.toNIO, + lines, + StandardOpenOption.CREATE, + StandardOpenOption.APPEND, + StandardOpenOption.WRITE + ) + catch + case NonFatal(_) => + // Ignore exceptions + + private def readableTimeStamp(): String = + // Simple implementation returns GMT time (no date) + // Use custom code as DateTimeFormatter is not available in Scala Native 0.4.14 + val t = System.currentTimeMillis() + val sss = t % 1000 + val ss = (t / 1000L).toInt % 60 + val mm = (t / (1000L * 60L)).toInt % 60 + val hh = (t / (1000L * 60L * 60L)).toInt % 24 + f"$hh%02d-$mm%02d-$ss%02d_$sss%03dZ"