From c616cf3bf682e1ca7558e0c155bc5a07adbf48e3 Mon Sep 17 00:00:00 2001 From: Roman Sedaikin Date: Wed, 5 May 2021 18:03:06 +0300 Subject: [PATCH] Changed path for output to [user.home], refactor code. --- .../src/main/kotlin/debugwriter/Main.kt | 29 +++++++++++--- .../src/main/kotlin/debugwriter/Utils.kt | 40 ++++++++++++++----- 2 files changed, 54 insertions(+), 15 deletions(-) diff --git a/debug-writer/src/main/kotlin/debugwriter/Main.kt b/debug-writer/src/main/kotlin/debugwriter/Main.kt index 07e4259ec4..71192d4176 100644 --- a/debug-writer/src/main/kotlin/debugwriter/Main.kt +++ b/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 } } \ No newline at end of file diff --git a/debug-writer/src/main/kotlin/debugwriter/Utils.kt b/debug-writer/src/main/kotlin/debugwriter/Utils.kt index fc6ae79274..3b34db77a1 100644 --- a/debug-writer/src/main/kotlin/debugwriter/Utils.kt +++ b/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 } } \ No newline at end of file