From c6de35b7b283d1693c314559effd177cb912862b Mon Sep 17 00:00:00 2001 From: pacien Date: Sun, 25 Feb 2018 01:42:27 +0100 Subject: Add log viewer --- app/src/main/AndroidManifest.xml | 6 ++ .../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 ++ .../ic_pause_circle_filled_primary_24dp.xml | 9 +++ .../ic_pause_circle_outline_primary_24dp.xml | 9 +++ .../main/res/drawable/ic_receipt_primary_24dp.xml | 9 +++ app/src/main/res/layout/page_viewlog.xml | 13 ++++ app/src/main/res/menu/menu_status.xml | 8 ++ app/src/main/res/menu/menu_viewlog.xml | 13 ++++ app/src/main/res/values/strings.xml | 3 + 12 files changed, 177 insertions(+), 9 deletions(-) create mode 100644 app/src/main/java/org/pacien/tincapp/activities/ViewLogActivity.kt create mode 100644 app/src/main/res/drawable/ic_pause_circle_filled_primary_24dp.xml create mode 100644 app/src/main/res/drawable/ic_pause_circle_outline_primary_24dp.xml create mode 100644 app/src/main/res/drawable/ic_receipt_primary_24dp.xml create mode 100644 app/src/main/res/layout/page_viewlog.xml create mode 100644 app/src/main/res/menu/menu_viewlog.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 6826920..8fc83b3 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -48,6 +48,12 @@ + + + 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()) }) } diff --git a/app/src/main/res/drawable/ic_pause_circle_filled_primary_24dp.xml b/app/src/main/res/drawable/ic_pause_circle_filled_primary_24dp.xml new file mode 100644 index 0000000..84ac134 --- /dev/null +++ b/app/src/main/res/drawable/ic_pause_circle_filled_primary_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_pause_circle_outline_primary_24dp.xml b/app/src/main/res/drawable/ic_pause_circle_outline_primary_24dp.xml new file mode 100644 index 0000000..9374a21 --- /dev/null +++ b/app/src/main/res/drawable/ic_pause_circle_outline_primary_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_receipt_primary_24dp.xml b/app/src/main/res/drawable/ic_receipt_primary_24dp.xml new file mode 100644 index 0000000..4ce8a76 --- /dev/null +++ b/app/src/main/res/drawable/ic_receipt_primary_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/layout/page_viewlog.xml b/app/src/main/res/layout/page_viewlog.xml new file mode 100644 index 0000000..a7afed8 --- /dev/null +++ b/app/src/main/res/layout/page_viewlog.xml @@ -0,0 +1,13 @@ + diff --git a/app/src/main/res/menu/menu_status.xml b/app/src/main/res/menu/menu_status.xml index 4b7c7dc..7415aeb 100644 --- a/app/src/main/res/menu/menu_status.xml +++ b/app/src/main/res/menu/menu_status.xml @@ -11,4 +11,12 @@ android:title="@string/menu_disconnect" app:showAsAction="ifRoom"/> + + diff --git a/app/src/main/res/menu/menu_viewlog.xml b/app/src/main/res/menu/menu_viewlog.xml new file mode 100644 index 0000000..ee3c60f --- /dev/null +++ b/app/src/main/res/menu/menu_viewlog.xml @@ -0,0 +1,13 @@ + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 14d1180..4a412ae 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -14,6 +14,8 @@ About this app Configure Disconnect + Show log + Toggle logging Tinc network name Name of this node @@ -80,6 +82,7 @@ A passphrase is required to unlock the keyring. Tinc daemon exited during startup:\n%1$s\n\nCheck the logs for more details. Loading… + Log level set to %1$d. none yes -- cgit v1.2.3