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. 29
      debug-writer/src/main/kotlin/debugwriter/Main.kt
  2. 40
      debug-writer/src/main/kotlin/debugwriter/Utils.kt

29
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,14 +59,25 @@ 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() })
}
}
}
}
}
writeDebugInfo()
if (result) {
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
}
}

40
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))
System.setOut(stream)
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")
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().open(file)
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