aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java
diff options
context:
space:
mode:
authorpacien2018-02-25 01:42:27 +0100
committerpacien2018-02-25 01:42:27 +0100
commitc6de35b7b283d1693c314559effd177cb912862b (patch)
tree03a8157dbb389222a45abaeabf85662ef99177ee /app/src/main/java
parenteb31e3770f8c79ee0debb866e0fd56f9c37eb1be (diff)
downloadtincapp-c6de35b7b283d1693c314559effd177cb912862b.tar.gz
Add log viewer
Diffstat (limited to 'app/src/main/java')
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/StatusActivity.kt3
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/ViewLogActivity.kt90
-rw-r--r--app/src/main/java/org/pacien/tincapp/commands/Executor.kt18
-rw-r--r--app/src/main/java/org/pacien/tincapp/commands/Tinc.kt5
4 files changed, 107 insertions, 9 deletions
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
123 TincVpnService.disconnect() 123 TincVpnService.disconnect()
124 } 124 }
125 125
126 fun openLogViewer(@Suppress("UNUSED_PARAMETER") i: MenuItem) =
127 startActivity(Intent(this, ViewLogActivity::class.java))
128
126 private fun writeNetworkInfo(cfg: VpnInterfaceConfiguration) { 129 private fun writeNetworkInfo(cfg: VpnInterfaceConfiguration) {
127 text_network_name.text = TincVpnService.getCurrentNetName() ?: getString(R.string.value_none) 130 text_network_name.text = TincVpnService.getCurrentNetName() ?: getString(R.string.value_none)
128 text_network_ip_addresses.setText(cfg.addresses.map { it.toSlashSeparated() }) 131 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 @@
1package org.pacien.tincapp.activities
2
3import android.os.Bundle
4import android.text.method.ScrollingMovementMethod
5import android.view.Menu
6import android.view.MenuItem
7import kotlinx.android.synthetic.main.base.*
8import kotlinx.android.synthetic.main.page_viewlog.*
9import org.pacien.tincapp.R
10import org.pacien.tincapp.commands.Executor
11import org.pacien.tincapp.commands.Tinc
12import org.pacien.tincapp.service.TincVpnService
13
14/**
15 * @author pacien
16 */
17class ViewLogActivity : BaseActivity() {
18 companion object {
19 private const val LOG_LEVEL = 4
20 private const val NEW_LINE = "\n"
21 }
22
23 private var logger: Process? = null
24
25 override fun onCreate(savedInstanceState: Bundle?) {
26 super.onCreate(savedInstanceState)
27 supportActionBar!!.setDisplayHomeAsUpEnabled(true)
28 layoutInflater.inflate(R.layout.page_viewlog, main_content)
29 startLogging()
30 }
31
32 override fun onCreateOptionsMenu(m: Menu): Boolean {
33 menuInflater.inflate(R.menu.menu_viewlog, m)
34 return super.onCreateOptionsMenu(m)
35 }
36
37 override fun onSupportNavigateUp(): Boolean {
38 finish()
39 return true
40 }
41
42 override fun onDestroy() {
43 stopLogging()
44 super.onDestroy()
45 }
46
47 fun toggleLogging(menuItem: MenuItem) {
48 if (logger == null) {
49 startLogging()
50 text_log.movementMethod = null
51 text_log.setTextIsSelectable(false)
52 menuItem.setIcon(R.drawable.ic_pause_circle_outline_primary_24dp)
53 } else {
54 stopLogging()
55 text_log.movementMethod = ScrollingMovementMethod.getInstance()
56 text_log.setTextIsSelectable(true)
57 menuItem.setIcon(R.drawable.ic_pause_circle_filled_primary_24dp)
58 }
59 }
60
61 private fun startLogging(level: Int = LOG_LEVEL) {
62 text_log.append(NEW_LINE)
63 text_log.append(resources.getString(R.string.message_log_level_set, level))
64 text_log.append(NEW_LINE)
65
66 Tinc.log(TincVpnService.getCurrentNetName()!!, level).let { process ->
67 logger = process
68 Executor.runAsyncTask { printLog(process) }
69 }
70 }
71
72 private fun stopLogging() {
73 logger?.destroy()
74 logger = null
75 }
76
77 private fun printLog(logger: Process) {
78 logger.inputStream?.use { inputStream ->
79 inputStream.bufferedReader().useLines { lines ->
80 lines.forEach {
81 text_log.post {
82 text_log.append(NEW_LINE)
83 text_log.append(it)
84 text_log.append(NEW_LINE)
85 }
86 }
87 }
88 }
89 }
90}
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 {
47 } 47 }
48 } 48 }
49 49
50 fun call(cmd: Command): CompletableFuture<List<String>> { 50 fun run(cmd: Command): Process = try {
51 val proc = try { 51 ProcessBuilder(cmd.asList()).start()
52 ProcessBuilder(cmd.asList()).start() 52 } catch (e: IOException) {
53 } catch (e: IOException) { 53 throw CommandExecutionException(e.message ?: "Could not start process.")
54 throw CommandExecutionException(e.message ?: "Could not start process.") 54 }
55 }
56 55
57 return supplyAsyncTask<List<String>> { 56 fun call(cmd: Command): CompletableFuture<List<String>> = run(cmd).let { process ->
58 if (proc.waitFor() == 0) read(proc.inputStream) 57 supplyAsyncTask<List<String>> {
59 else throw CommandExecutionException(read(proc.errorStream).lastOrNull() ?: "Non-zero exit status.") 58 if (process.waitFor() == 0) read(process.inputStream)
59 else throw CommandExecutionException(read(process.errorStream).lastOrNull() ?: "Non-zero exit status.")
60 } 60 }
61 } 61 }
62 62
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 {
46 .withOption("config", AppPaths.confDir(netName).absolutePath) 46 .withOption("config", AppPaths.confDir(netName).absolutePath)
47 .withArguments("join", invitationUrl)) 47 .withArguments("join", invitationUrl))
48 .thenApply<String> { it.joinToString("\n") } 48 .thenApply<String> { it.joinToString("\n") }
49
50 fun log(netName: String, level: Int? = null): Process =
51 Executor.run(newCommand(netName)
52 .withArguments("log")
53 .apply { if (level != null) withArguments(level.toString()) })
49} 54}