Skip to content

Commit

Permalink
#145: jni setup and build (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
rladstaetter committed Aug 16, 2023
1 parent 5abadb5 commit 81f5790
Show file tree
Hide file tree
Showing 20 changed files with 253 additions and 86 deletions.
9 changes: 5 additions & 4 deletions app/src/main/java/app/logorrr/OsxBridge.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package app.logorrr;

/**
* Provides interface methods for native code
*/
public class OsxBridge {

public static native void printHelloWorldImpl();
public static native void registerPath(String path);

public static void main(final String[] args) {
printHelloWorldImpl();
}
public static native void releasePath(String path);

}
4 changes: 2 additions & 2 deletions app/src/main/scala/app/logorrr/LogoRRRApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ object LogoRRRApp {
val logFormat = """[%1$tF %1$tT.%1$tN] %3$-40s %4$-13s %5$s %6$s %n"""

def main(args: Array[String]): Unit = {
// LogUtil.init()
System.setProperty("user.language", "en")
System.setProperty("java.util.logging.SimpleFormatter.format", logFormat)
javafx.application.Application.launch(classOf[LogoRRRApp], args: _*)
}
Expand All @@ -23,10 +23,10 @@ object LogoRRRApp {

class LogoRRRApp extends javafx.application.Application with CanLog {

logInfo("I'm in" + Paths.get("").toAbsolutePath)

def start(stage: Stage): Unit = {
logInfo(s"Started " + AppMeta.fullAppNameWithVersion)
logInfo(s"Working directory: ${Paths.get("").toAbsolutePath.toString}")
val settings: Settings = SettingsIO.fromFile()
LogoRRRGlobals.set(settings, getHostServices)
LogoRRRStage(stage).show()
Expand Down
18 changes: 12 additions & 6 deletions app/src/main/scala/app/logorrr/LogoRRRAppLauncher.scala
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package app.logorrr

import app.logorrr.util.OsUtil

object LogoRRRAppLauncher {

/** launcher for macos installer to circumvent module loading mechanism by javafx (dirty hack) */
def main(args: Array[String]): Unit = {

test()
if (OsUtil.isMac) {
System.loadLibrary("LogoRRRSwift")
System.loadLibrary("LogoRRR")
}

LogoRRRApp.main(args)
}

private def test(): Unit = {
System.loadLibrary("LogoRRRSwift")
System.loadLibrary("LogoRRR")
OsxBridge.printHelloWorldImpl()
}
// private def test(): Unit = {
// OsxBridge.printHelloWorldImpl()
// OsxBridge.registerPath(Paths.get("/Users/lad/gh/LogoRRR/app/target/app-23.2.0-SNAPSHOT.jar").toAbsolutePath.toString)
// OsxBridge.releasePath(Paths.get("/Users/lad/gh/LogoRRR/app/target/app-23.2.0-SNAPSHOT.jar").toAbsolutePath.toString)
//}
}
11 changes: 9 additions & 2 deletions app/src/main/scala/app/logorrr/conf/LogoRRRGlobals.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package app.logorrr.conf

import app.logorrr.OsxBridge
import app.logorrr.conf.SettingsIO.renderOptions
import app.logorrr.conf.mut.{MutLogFileSettings, MutSettings}
import app.logorrr.io.{FilePaths, Fs}
import app.logorrr.model.LogFileSettings
import app.logorrr.util.CanLog
import app.logorrr.util.{CanLog, OsUtil}
import javafx.application.HostServices
import javafx.beans.property.SimpleObjectProperty
import javafx.stage.Window
Expand Down Expand Up @@ -62,12 +63,18 @@ object LogoRRRGlobals extends CanLog {
def getSomeActive(): Option[String] = mutSettings.getSomeActive()

def removeLogFile(pathAsString: String): Unit = {

mutSettings.removeLogFileSetting(pathAsString)
mutSettings.setSomeActive(mutSettings.getSomeActive() match {
case Some(value) if value == pathAsString => None
case x => x
})
logInfo(s"Removed file ${pathAsString} ...")

if (OsUtil.isMac) {
OsxBridge.releasePath(pathAsString)
}

logInfo(s"Removed file $pathAsString ...")
}

def clearLogFileSettings(): Unit = mutSettings.clearLogFileSettings()
Expand Down
8 changes: 6 additions & 2 deletions app/src/main/scala/app/logorrr/conf/mut/MutSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ class MutSettings {

def getMutLogFileSetting(key: String): MutLogFileSettings = mutLogFileSettingsMapProperty.get(key)

def putMutLogFileSetting(mutLogFileSettings: MutLogFileSettings): Unit = mutLogFileSettingsMapProperty.put(mutLogFileSettings.getPathAsString(), mutLogFileSettings)
def putMutLogFileSetting(mutLogFileSettings: MutLogFileSettings): Unit = {
mutLogFileSettingsMapProperty.put(mutLogFileSettings.getPathAsString(), mutLogFileSettings)
}

def removeLogFileSetting(pathAsString: String): Unit = mutLogFileSettingsMapProperty.remove(pathAsString)

Expand Down Expand Up @@ -75,6 +77,7 @@ class MutSettings {
}

def clearLogFileSettings(): Unit = {

mutLogFileSettingsMapProperty.clear()
setSomeActive(None)
}
Expand All @@ -101,8 +104,9 @@ class MutSettings {

def getStageWidth(): Int = mutStageSettings.getWidth()

def getOrderedLogFileSettings(): Seq[LogFileSettings] =
def getOrderedLogFileSettings(): Seq[LogFileSettings] = {
mutLogFileSettingsMapProperty.get().values.asScala.toSeq.sortWith((lt, gt) => lt.getFirstOpened() < gt.getFirstOpened()).map(_.petrify())
}


}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package app.logorrr.model

import app.logorrr.OsxBridge
import app.logorrr.util.CanLog
import app.logorrr.views.search.{Filter, Fltr}
import javafx.collections.{FXCollections, ObservableList}
Expand Down
27 changes: 23 additions & 4 deletions app/src/main/scala/app/logorrr/model/LogFileReader.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app.logorrr.model

import app.logorrr.util.CanLog
import app.logorrr.OsxBridge
import app.logorrr.util.{CanLog, OsUtil}

import java.nio.charset.StandardCharsets
import java.nio.file.{Files, Path}
Expand All @@ -11,13 +12,31 @@ object LogFileReader extends CanLog {

def readFromFile(logFile: Path): util.List[String] = {
Try {
Files.readAllLines(logFile)
if (OsUtil.isMac) {
logInfo("Registering security bookmark for " + logFile.toAbsolutePath.toString)
OsxBridge.registerPath(logFile.toAbsolutePath.toString)
}

val lines = Files.readAllLines(logFile)

if (lines.isEmpty) {
logWarn(s"${logFile.toAbsolutePath.toString} was empty.")
} else {
logTrace(s"${logFile.toAbsolutePath.toString} has ${lines.size()} lines.")
}
lines
} match {
case Failure(exception) =>
val msg = s"Could not read file ${logFile.toAbsolutePath.toString} with default charset, retrying with ISO_8859_1 ..."
val msg = s"Failed to read ${logFile.toAbsolutePath.toString}, exception: ${exception.getMessage}, retrying ISO_8859_1 ..."
logException(msg, exception)
Try {
Files.readAllLines(logFile, StandardCharsets.ISO_8859_1)
val lines = Files.readAllLines(logFile, StandardCharsets.ISO_8859_1)
if (lines.isEmpty) {
logWarn(s"${logFile.toAbsolutePath.toString} was empty.")
} else {
logTrace(s"${logFile.toAbsolutePath.toString} has ${lines.size()} lines.")
}
lines
} match {
case Failure(exception) =>
val msg = s"Could not read file ${logFile.toAbsolutePath.toString} properly. Reason: ${exception.getMessage}."
Expand Down
12 changes: 9 additions & 3 deletions app/src/main/scala/app/logorrr/model/LogFileSettings.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package app.logorrr.model

import app.logorrr.conf.BlockSettings
import app.logorrr.util.CanLog
import app.logorrr.util.{CanLog, OsUtil}
import app.logorrr.views.search.Filter
import javafx.collections.{FXCollections, ObservableList}
import javafx.scene.paint.Color
Expand Down Expand Up @@ -63,9 +63,15 @@ case class LogFileSettings(pathAsString: String
, someLogEntryInstantFormat: Option[LogEntryInstantFormat]
, autoScroll: Boolean) extends CanLog {

val path: Path = Paths.get(pathAsString)
val path: Path = Paths.get(pathAsString).toAbsolutePath

val isPathValid = Files.isReadable(path) && Files.isRegularFile(path)
val isPathValid =
if (OsUtil.isMac) {
Files.exists(path)
} else {
// without security bookmarks initialized, this returns false on mac
Files.isReadable(path) && Files.isRegularFile(path)
}

def readEntries(): ObservableList[LogEntry] = {
if (isPathValid) {
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/scala/app/logorrr/util/LogoRRRFileChooser.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package app.logorrr.util

import javafx.stage.FileChooser
import javafx.stage.{FileChooser, Window}

import java.nio.file.Path

class LogoRRRFileChooser(title: String) {

def showAndWait(): Option[Path] = {
def showAndWait(window: Window): Option[Path] = {
val fc = new FileChooser
fc.setTitle(title)
Option(fc.showOpenDialog(null)).map(_.toPath)
Option(fc.showOpenDialog(window)).map(_.toPath)
}

}
58 changes: 30 additions & 28 deletions app/src/main/scala/app/logorrr/views/main/LogoRRRMain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import app.logorrr.util.{CanLog, JfxUtils}
import app.logorrr.views.LogFileTab
import javafx.collections.ObservableList
import javafx.scene.layout.BorderPane
import javafx.stage.Window

import java.nio.file.Path
import scala.concurrent.ExecutionContext.Implicits.global
Expand All @@ -15,53 +16,54 @@ import scala.util.{Failure, Success}
class LogoRRRMain(closeStage: => Unit)
extends BorderPane
with CanLog {

val mB = new LogoRRRMenuBar(openLogFile, closeAllLogFiles(), closeStage)
val mB = new LogoRRRMenuBar(getWindow _, openLogFile, closeAllLogFiles(), closeStage)
val ambp = new LogoRRRMainBorderPane()

init()

def getWindow(): Window = getScene.getWindow

def init(): Unit = {
setTop(mB)
setCenter(ambp)
loadLogFiles(LogoRRRGlobals.getOrderedLogFileSettings())
val entries = LogoRRRGlobals.getOrderedLogFileSettings()
if (entries.nonEmpty) {
loadLogFiles(LogoRRRGlobals.getOrderedLogFileSettings())
} else {
logInfo("No log files loaded.")
}
JfxUtils.execOnUiThread(ambp.init())
}

private def loadLogFiles(logs: Seq[LogFileSettings]): Unit = {
Future.sequence(
Future.sequence {
logInfo(s"Loading ${logs.length} log files: " + logs.map(_.pathAsString).mkString("['", "',`'", "']"))
logs.filter(s => !ambp.contains(s.pathAsString)).map(s => Future((s.pathAsString, s.readEntries())))
).onComplete({
tryLfs =>
tryLfs match {
case Success(lfs: Seq[(String, ObservableList[LogEntry])]) =>
lfs.foreach({
case (pathAsString, es) => JfxUtils.execOnUiThread({
ambp.addLogFileTab(LogFileTab(pathAsString, es))
LogoRRRGlobals.getSomeActive() match {
case Some(value) if pathAsString == value =>
selectLog(value)
case _ =>
}
})
})
case Failure(exception) =>
logException("Could not load logfiles", exception)
}

JfxUtils.execOnUiThread(ambp.init())
}.onComplete({
case Success(lfs: Seq[(String, ObservableList[LogEntry])]) =>
lfs.foreach({
case (pathAsString, es) => JfxUtils.execOnUiThread({
logTrace(s"Loading `$pathAsString` with ${es.size()} entries.")
ambp.addLogFileTab(LogFileTab(pathAsString, es))
LogoRRRGlobals.getSomeActive() match {
case Some(value) if pathAsString == value =>
selectLog(value)
case _ =>
}
})
})
case Failure(exception) =>
logException("Could not load logfiles", exception)
})
}

/** called when 'Open File' is or an entry of 'Recent Files' is selected. */
/** called when 'Open File' is selected. */
def openLogFile(path: Path): Unit = {
val pathAsString = path.toAbsolutePath.toString
logTrace(s"Try to open log file $pathAsString")

if (!ambp.contains(pathAsString)) {
val logFileSettings = LogFileSettings(path)
LogoRRRGlobals.updateLogFile(logFileSettings)
ambp.addLogFileTab(LogFileTab(pathAsString, logFileSettings.readEntries()))
selectLog(pathAsString)
ambp.addLogFile(path)
} else {
logTrace("File is already opened.")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,13 @@ class LogoRRRMainBorderPane extends BorderPane with CanLog {
logViewTabPane.init()
}


private def dropLogFile(path: Path): Unit = {
val pathAsString = path.toAbsolutePath.toString

if (Files.exists(path)) {
if (!contains(pathAsString)) {
val logFileSettings = LogFileSettings(path)
LogoRRRGlobals.updateLogFile(logFileSettings)
addLogFileTab(LogFileTab(logFileSettings.pathAsString, logFileSettings.readEntries()))
selectLog(pathAsString)
addLogFile(path)
} else {
logWarn(s"$pathAsString is already opened ...")
}
Expand All @@ -59,6 +58,13 @@ class LogoRRRMainBorderPane extends BorderPane with CanLog {
}
}

def addLogFile(path: Path): Unit = {
val logFileSettings = LogFileSettings(path)
LogoRRRGlobals.updateLogFile(logFileSettings)
addLogFileTab(LogFileTab(logFileSettings.pathAsString, logFileSettings.readEntries()))
selectLog(path.toAbsolutePath.toString)
}

def shutdown(): Unit = logViewTabPane.shutdown()

/** Adds a new logfile to display */
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/scala/app/logorrr/views/main/LogoRRRMenuBar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ package app.logorrr.views.main
import app.logorrr.util.{CanLog, OsUtil}
import app.logorrr.views.menubar.{FileMenu, HelpMenu}
import javafx.scene.control.MenuBar
import javafx.stage.Window

import java.nio.file.Path

class LogoRRRMenuBar(openLogFile: Path => Unit
class LogoRRRMenuBar(getWindow: () => Window
, openLogFile: Path => Unit
, closeAllLogFiles: => Unit
, closeApplication: => Unit)
extends MenuBar
with CanLog {

if (OsUtil.isMac) {
setUseSystemMenuBar(OsUtil.isMac)
}
setUseSystemMenuBar(OsUtil.isMac)

private def init(): Unit = {
getMenus.clear()
getMenus.addAll(new FileMenu(openLogFile, closeAllLogFiles, closeApplication), new HelpMenu(openLogFile))
getMenus.addAll(new FileMenu(getWindow, openLogFile, closeAllLogFiles, closeApplication), new HelpMenu(openLogFile))
}

init()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ object LogoRRRStage {

case class LogoRRRStage(stage: Stage) extends CanLog {

val logorrrMain = new LogoRRRMain(JfxUtils.closeStage(stage))
private val logorrrMain = new LogoRRRMain(JfxUtils.closeStage(stage))

val (width, height) = (LogoRRRGlobals.getStageWidth(), LogoRRRGlobals.getStageHeight())

Expand Down
Loading

0 comments on commit 81f5790

Please sign in to comment.