Browse Source

Changed path for output to [user.home], refactor code.

debug-writer
Roman Sedaikin 4 years ago committed by Rsedaikin
parent
commit
c616cf3bf6
  1. 27
      debug-writer/src/main/kotlin/debugwriter/Main.kt
  2. 34
      debug-writer/src/main/kotlin/debugwriter/Utils.kt

27
debug-writer/src/main/kotlin/debugwriter/Main.kt

@ -26,12 +26,15 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
private val fileName = "debug-info.txt"
private val fileName = "${System.getProperty("user.home")}/.debug-writer/debug-info.txt"
private var output by mutableStateOf("")
private var isReady by mutableStateOf(false)
private var isReady by mutableStateOf(true)
fun main() {
enableDebugWritingTo(fileName)
val result = enableDebugWritingTo(fileName)
if (!result) {
output = "Failed to cteate file: $fileName"
}
Window(
title = "DebugWriter",
@ -56,15 +59,26 @@ fun main() {
}
Row(modifier = Modifier.fillMaxWidth()) {
Button("Refresh", Modifier.weight(1f), { writeDebugInfo() })
Button("Open file", Modifier.weight(1f), { revealDebugOutput(fileName) })
Button(
text = "Open file",
modifier = Modifier.weight(1f),
action = {
if(!revealDebugOutput(fileName)) {
output = "Failed to open file: $fileName"
}
}
)
Button("Close", Modifier.weight(1f), { window.close() })
}
}
}
}
}
if (result) {
writeDebugInfo()
}
}
}
private fun writeDebugInfo() {
@ -72,6 +86,9 @@ private fun writeDebugInfo() {
GlobalScope.async {
delay(2000L)
isReady = true
output = readDebugOutput(fileName)
val result = readDebugOutput(fileName)
output = if (result.isEmpty())
"Something went wrong and $fileName is empty."
else result
}
}

34
debug-writer/src/main/kotlin/debugwriter/Utils.kt

@ -1,27 +1,49 @@
package debugwriter
import java.awt.Desktop
import java.io.File
import java.io.FileOutputStream
import java.io.InputStream
import java.io.PrintStream
import java.awt.Desktop
import java.nio.file.Paths
import org.jetbrains.skiko.hostOs
import org.jetbrains.skiko.OS
fun enableDebugWritingTo(fileName: String = "output.txt") {
fun enableDebugWritingTo(fileName: String = "output.txt"): Boolean {
System.setProperty("skiko.hardwareInfo.enabled", "true")
val stream = PrintStream(FileOutputStream(fileName))
try {
val directory = File(Paths.get(fileName).getParent().toString())
if (!directory.exists()) {
directory.mkdir()
}
val stream = PrintStream(FileOutputStream("$fileName"))
System.setOut(stream)
return true
} catch(e: Exception) {
return false
}
}
fun readDebugOutput(fileName: String = "output.txt"): String {
val inputStream: InputStream = File(fileName).inputStream()
val file = File("$fileName")
if (!file.exists()) {
return "File $fileName does not exist."
}
val inputStream: InputStream = file.inputStream()
return inputStream.bufferedReader().use { it.readText() }
}
fun revealDebugOutput(fileName: String = "output.txt") {
val file = File("${System.getProperty("user.dir")}/$fileName")
fun revealDebugOutput(fileName: String = "output.txt"): Boolean {
val file = File(fileName)
if (!file.exists()) {
return false
}
try {
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().open(file)
}
return true
} catch(e: Exception) {
return false
}
}
Loading…
Cancel
Save