From c6de35b7b283d1693c314559effd177cb912862b Mon Sep 17 00:00:00 2001 From: pacien Date: Sun, 25 Feb 2018 01:42:27 +0100 Subject: Add log viewer --- .../pacien/tincapp/activities/StatusActivity.kt | 3 + .../pacien/tincapp/activities/ViewLogActivity.kt | 90 ++++++++++++++++++++++ .../java/org/pacien/tincapp/commands/Executor.kt | 18 ++--- .../main/java/org/pacien/tincapp/commands/Tinc.kt | 5 ++ 4 files changed, 107 insertions(+), 9 deletions(-) create mode 100644 app/src/main/java/org/pacien/tincapp/activities/ViewLogActivity.kt (limited to 'app/src/main/java/org/pacien') diff --git a/app/src/main/java/org/pacien/tincapp/activities/StatusActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/StatusActivity.kt index 1872081..351305c 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/StatusActivity.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/StatusActivity.kt @@ -123,6 +123,9 @@ class StatusActivity : BaseActivity(), AdapterView.OnItemClickListener, SwipeRef TincVpnService.disconnect() } + fun openLogViewer(@Suppress("UNUSED_PARAMETER") i: MenuItem) = + startActivity(Intent(this, ViewLogActivity::class.java)) + private fun writeNetworkInfo(cfg: VpnInterfaceConfiguration) { text_network_name.text = TincVpnService.getCurrentNetName() ?: getString(R.string.value_none) text_network_ip_addresses.setText(cfg.addresses.map { it.toSlashSeparated() }) diff --git a/app/src/main/java/org/pacien/tincapp/activities/ViewLogActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/ViewLogActivity.kt new file mode 100644 index 0000000..67df51b --- /dev/null +++ b/app/src/main/java/org/pacien/tincapp/activities/ViewLogActivity.kt @@ -0,0 +1,90 @@ +package org.pacien.tincapp.activities + +import android.os.Bundle +import android.text.method.ScrollingMovementMethod +import android.view.Menu +import android.view.MenuItem +import kotlinx.android.synthetic.main.base.* +import kotlinx.android.synthetic.main.page_viewlog.* +import org.pacien.tincapp.R +import org.pacien.tincapp.commands.Executor +import org.pacien.tincapp.commands.Tinc +import org.pacien.tincapp.service.TincVpnService + +/** + * @author pacien + */ +class ViewLogActivity : BaseActivity() { + companion object { + private const val LOG_LEVEL = 4 + private const val NEW_LINE = "\n" + } + + private var logger: Process? = null + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + supportActionBar!!.setDisplayHomeAsUpEnabled(true) + layoutInflater.inflate(R.layout.page_viewlog, main_content) + startLogging() + } + + override fun onCreateOptionsMenu(m: Menu): Boolean { + menuInflater.inflate(R.menu.menu_viewlog, m) + return super.onCreateOptionsMenu(m) + } + + override fun onSupportNavigateUp(): Boolean { + finish() + return true + } + + override fun onDestroy() { + stopLogging() + super.onDestroy() + } + + fun toggleLogging(menuItem: MenuItem) { + if (logger == null) { + startLogging() + text_log.movementMethod = null + text_log.setTextIsSelectable(false) + menuItem.setIcon(R.drawable.ic_pause_circle_outline_primary_24dp) + } else { + stopLogging() + text_log.movementMethod = ScrollingMovementMethod.getInstance() + text_log.setTextIsSelectable(true) + menuItem.setIcon(R.drawable.ic_pause_circle_filled_primary_24dp) + } + } + + private fun startLogging(level: Int = LOG_LEVEL) { + text_log.append(NEW_LINE) + text_log.append(resources.getString(R.string.message_log_level_set, level)) + text_log.append(NEW_LINE) + + Tinc.log(TincVpnService.getCurrentNetName()!!, level).let { process -> + logger = process + Executor.runAsyncTask { printLog(process) } + } + } + + private fun stopLogging() { + logger?.destroy() + logger = null + } + + private fun printLog(logger: Process) { + logger.inputStream?.use { inputStream -> + inputStream.bufferedReader().useLines { lines -> + lines.forEach { + text_log.post { + text_log.append(NEW_LINE) + text_log.append(it) + text_log.append(NEW_LINE) + } + } + } + } + } +} diff --git a/app/src/main/java/org/pacien/tincapp/commands/Executor.kt b/app/src/main/java/org/pacien/tincapp/commands/Executor.kt index 39c413e..96e48b9 100644 --- a/app/src/main/java/org/pacien/tincapp/commands/Executor.kt +++ b/app/src/main/java/org/pacien/tincapp/commands/Executor.kt @@ -47,16 +47,16 @@ internal object Executor { } } - fun call(cmd: Command): CompletableFuture> { - val proc = try { - ProcessBuilder(cmd.asList()).start() - } catch (e: IOException) { - throw CommandExecutionException(e.message ?: "Could not start process.") - } + fun run(cmd: Command): Process = try { + ProcessBuilder(cmd.asList()).start() + } catch (e: IOException) { + throw CommandExecutionException(e.message ?: "Could not start process.") + } - return supplyAsyncTask> { - if (proc.waitFor() == 0) read(proc.inputStream) - else throw CommandExecutionException(read(proc.errorStream).lastOrNull() ?: "Non-zero exit status.") + fun call(cmd: Command): CompletableFuture> = run(cmd).let { process -> + supplyAsyncTask> { + if (process.waitFor() == 0) read(process.inputStream) + else throw CommandExecutionException(read(process.errorStream).lastOrNull() ?: "Non-zero exit status.") } } diff --git a/app/src/main/java/org/pacien/tincapp/commands/Tinc.kt b/app/src/main/java/org/pacien/tincapp/commands/Tinc.kt index 556288a..0f4c0a4 100644 --- a/app/src/main/java/org/pacien/tincapp/commands/Tinc.kt +++ b/app/src/main/java/org/pacien/tincapp/commands/Tinc.kt @@ -46,4 +46,9 @@ object Tinc { .withOption("config", AppPaths.confDir(netName).absolutePath) .withArguments("join", invitationUrl)) .thenApply { it.joinToString("\n") } + + fun log(netName: String, level: Int? = null): Process = + Executor.run(newCommand(netName) + .withArguments("log") + .apply { if (level != null) withArguments(level.toString()) }) } -- cgit v1.2.3