Skip to content

Commit

Permalink
Fix plugins to actually log to file & change output text area font to…
Browse files Browse the repository at this point in the history
… jb mono
  • Loading branch information
serivesmejia committed Oct 27, 2024
1 parent 74ec7f7 commit 97b9963
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 97 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package com.github.serivesmejia.eocvsim.util

import org.slf4j.LoggerFactory
import kotlin.reflect.KClass

fun Any.loggerFor(clazz: KClass<*>) = lazy { LoggerFactory.getLogger(clazz.java) }
fun Any.loggerForThis() = lazy { LoggerFactory.getLogger(this::class.java) }

fun loggerOf(name: String) = lazy { LoggerFactory.getLogger(name) }
package com.github.serivesmejia.eocvsim.util

import org.slf4j.LoggerFactory
import kotlin.reflect.KClass

fun Any.loggerFor(clazz: KClass<*>) = lazy {
LoggerFactory.getLogger(clazz.java)
}

fun Any.loggerForThis() = lazy {
LoggerFactory.getLogger(this::class.java)
}

fun loggerOf(name: String) = lazy {
LoggerFactory.getLogger(name)
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,34 @@ import java.awt.GridBagConstraints
import java.awt.GridBagLayout
import java.awt.Toolkit
import java.awt.datatransfer.StringSelection
import java.awt.Font
import java.io.InputStream
import javax.swing.*


class OutputPanel(
bottomButtonsPanel: BottomButtonsPanel
) : JPanel(GridBagLayout()) {

val outputArea = JTextArea("")

companion object {
val monoFont: Font by lazy {
Font.createFont(
Font.TRUETYPE_FONT,
this::class.java.getResourceAsStream("/fonts/JetBrainsMono-Medium.ttf")
)
}
}

init {
if(bottomButtonsPanel is DefaultBottomButtonsPanel) {
bottomButtonsPanel.outputTextSupplier = { outputArea.text }
}

// JTextArea will use /fonts/JetBrainsMono-Medium.ttf as font
outputArea.font = monoFont.deriveFont(13f)

outputArea.isEditable = false
outputArea.highlighter = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,24 @@ public SLF4JIOReceiver(Logger logger) {
@Override
public void receive(InputStream out, InputStream err, int pid) {
new Thread(() -> {
Thread.currentThread().setContextClassLoader(SLF4JIOReceiver.class.getClassLoader());

Scanner sc = new Scanner(out);
while (sc.hasNextLine()) {
logger.info(sc.nextLine());
}
}, "SLFJ4IOReceiver-out-" + pid).start();

new Thread(() -> {
Thread.currentThread().setContextClassLoader(SLF4JIOReceiver.class.getClassLoader());

Scanner sc = new Scanner(err);
while (sc.hasNextLine()) {
logger.error(sc.nextLine());
}
}, "SLF4JIOReceiver-err-" + pid).start();

logger.debug("SLF4JIOReceiver started for PID: {}", pid); // Debug log to check if the logger is working
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import io.github.deltacv.eocvsim.sandbox.restrictions.MethodCallByteCodeChecker
import io.github.deltacv.eocvsim.sandbox.restrictions.dynamicLoadingMethodBlacklist
import io.github.deltacv.eocvsim.sandbox.restrictions.dynamicLoadingPackageBlacklist
import io.github.deltacv.eocvsim.sandbox.restrictions.dynamicLoadingPackageWhitelist
import org.apache.logging.log4j.core.LoggerContext
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.IOException
Expand All @@ -43,7 +44,11 @@ import java.util.zip.ZipFile
* @param pluginJar the jar file of the plugin
* @param pluginContext the plugin context
*/
class PluginClassLoader(private val pluginJar: File, val classpath: List<File>, val pluginContextProvider: () -> PluginContext) : ClassLoader() {
class PluginClassLoader(
private val pluginJar: File,
val classpath: List<File>,
val pluginContextProvider: () -> PluginContext
) : ClassLoader() {

private val zipFile = try {
ZipFile(pluginJar)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ import io.github.deltacv.eocvsim.plugin.EOCVSimPlugin
import io.github.deltacv.eocvsim.sandbox.nio.SandboxFileSystem

class PluginContext(
val eocvSim: EOCVSim, val fileSystem: SandboxFileSystem, val loader: PluginLoader
val eocvSim: EOCVSim,
val fileSystem: SandboxFileSystem,
val loader: PluginLoader
) {
companion object {
@JvmStatic fun current(plugin: EOCVSimPlugin) = (plugin.javaClass.classLoader as PluginClassLoader).pluginContextProvider()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,19 @@ package io.github.deltacv.eocvsim.plugin.loader

import com.github.serivesmejia.eocvsim.EOCVSim
import com.github.serivesmejia.eocvsim.config.ConfigLoader
import com.github.serivesmejia.eocvsim.gui.dialog.AppendDelegate
import com.github.serivesmejia.eocvsim.gui.dialog.PluginOutput
import com.github.serivesmejia.eocvsim.util.SysUtil
import com.github.serivesmejia.eocvsim.util.event.EventHandler
import com.github.serivesmejia.eocvsim.util.extension.plus
import com.github.serivesmejia.eocvsim.util.loggerForThis
import com.moandjiezana.toml.Toml
import org.apache.logging.log4j.LogManager
import io.github.deltacv.common.util.ParsedVersion
import io.github.deltacv.eocvsim.plugin.EOCVSimPlugin
import io.github.deltacv.eocvsim.sandbox.nio.SandboxFileSystem
import net.lingala.zip4j.ZipFile
import org.apache.logging.log4j.core.LoggerContext
import java.io.File
import java.security.MessageDigest

Expand All @@ -51,7 +55,8 @@ class PluginLoader(
val pluginFile: File,
val classpath: List<File>,
val pluginSource: PluginSource,
val eocvSim: EOCVSim
val eocvSim: EOCVSim,
val appender: AppendDelegate
) {

val logger by loggerForThis()
Expand Down Expand Up @@ -97,7 +102,10 @@ class PluginLoader(
val hasSuperAccess get() = eocvSim.config.superAccessPluginHashes.contains(pluginFileHash)

init {
pluginClassLoader = PluginClassLoader(pluginFile, classpath) {
pluginClassLoader = PluginClassLoader(
pluginFile,
classpath
) {
PluginContext(eocvSim, fileSystem, this)
}
}
Expand Down Expand Up @@ -130,7 +138,7 @@ class PluginLoader(

fetchInfoFromToml()

logger.info("Loading plugin $pluginName v$pluginVersion by $pluginAuthor from ${pluginSource.name}")
appender.appendln("${PluginOutput.SPECIAL_SILENT}Loading plugin $pluginName v$pluginVersion by $pluginAuthor from ${pluginSource.name}")

setupFs()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ import com.github.serivesmejia.eocvsim.util.JavaProcess
import com.github.serivesmejia.eocvsim.util.extension.plus
import com.github.serivesmejia.eocvsim.util.io.EOCVSimFolder
import com.github.serivesmejia.eocvsim.util.loggerForThis
import com.github.serivesmejia.eocvsim.util.loggerOf
import io.github.deltacv.eocvsim.gui.dialog.SuperAccessRequestMain
import io.github.deltacv.eocvsim.plugin.repository.PluginRepositoryManager
import java.io.File
import java.util.*
import java.util.concurrent.locks.ReentrantLock
import kotlin.concurrent.withLock

/**
* Manages the loading, enabling and disabling of plugins
Expand All @@ -55,15 +58,18 @@ class PluginManager(val eocvSim: EOCVSim) {
private val _loadedPluginHashes = mutableListOf<String>()
val loadedPluginHashes get() = _loadedPluginHashes.toList()

private val haltLock = Object()
private val haltLock = ReentrantLock()
private val haltCondition = haltLock.newCondition()

val appender by lazy {
val appender = DialogFactory.createMavenOutput {
synchronized(haltLock) {
haltLock.notify()
haltLock.withLock {
haltCondition.signalAll()
}
}

val logger by loggerOf("PluginOutput")

appender.subscribe {
if(!it.isBlank()) {
val message = it.trimSpecials()
Expand All @@ -78,9 +84,7 @@ class PluginManager(val eocvSim: EOCVSim) {
}

val repositoryManager by lazy {
PluginRepositoryManager(
appender, haltLock
)
PluginRepositoryManager(appender, haltLock, haltCondition)
}

private val _pluginFiles = mutableListOf<File>()
Expand Down Expand Up @@ -130,7 +134,8 @@ class PluginManager(val eocvSim: EOCVSim) {
repositoryManager.resolvedFiles,
if(pluginFile in repositoryManager.resolvedFiles)
PluginSource.REPOSITORY else PluginSource.FILE,
eocvSim
eocvSim,
appender
)
}

Expand Down
Loading

0 comments on commit 97b9963

Please sign in to comment.