diff options
author | pacien | 2018-03-04 18:05:16 +0100 |
---|---|---|
committer | pacien | 2018-03-04 18:07:33 +0100 |
commit | 44d00258bf0c7786ec6db9c19809db126e6c8172 (patch) | |
tree | a5ec66a2fc0c68f76d5a3a860a6eef3578b80594 /app | |
parent | c19a82fd6af5e4b6bb0f11933408c83aff66651f (diff) | |
download | tincapp-44d00258bf0c7786ec6db9c19809db126e6c8172.tar.gz |
Log view performance tweak and limiting
Diffstat (limited to 'app')
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/activities/ViewLogActivity.kt | 36 | ||||
-rw-r--r-- | app/src/main/res/values/strings.xml | 1 |
2 files changed, 26 insertions, 11 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/activities/ViewLogActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/ViewLogActivity.kt index d9fb733..ebdd22a 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/ViewLogActivity.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/ViewLogActivity.kt | |||
@@ -10,16 +10,22 @@ import org.pacien.tincapp.R | |||
10 | import org.pacien.tincapp.commands.Executor | 10 | import org.pacien.tincapp.commands.Executor |
11 | import org.pacien.tincapp.commands.Tinc | 11 | import org.pacien.tincapp.commands.Tinc |
12 | import org.pacien.tincapp.service.TincVpnService | 12 | import org.pacien.tincapp.service.TincVpnService |
13 | import java.util.* | ||
14 | import kotlin.concurrent.timer | ||
13 | 15 | ||
14 | /** | 16 | /** |
15 | * @author pacien | 17 | * @author pacien |
16 | */ | 18 | */ |
17 | class ViewLogActivity : BaseActivity() { | 19 | class ViewLogActivity : BaseActivity() { |
18 | companion object { | 20 | companion object { |
21 | private const val LOG_LINES = 250 | ||
19 | private const val LOG_LEVEL = 5 | 22 | private const val LOG_LEVEL = 5 |
20 | private const val NEW_LINE = "\n" | 23 | private const val NEW_LINE = "\n" |
24 | private const val UPDATE_INTERVAL = 250L // ms | ||
21 | } | 25 | } |
22 | 26 | ||
27 | private val log = LinkedList<String>() | ||
28 | private var logUpdateTimer: Timer? = null | ||
23 | private var logger: Process? = null | 29 | private var logger: Process? = null |
24 | 30 | ||
25 | override fun onCreate(savedInstanceState: Bundle?) { | 31 | override fun onCreate(savedInstanceState: Bundle?) { |
@@ -59,32 +65,40 @@ class ViewLogActivity : BaseActivity() { | |||
59 | } | 65 | } |
60 | 66 | ||
61 | private fun startLogging(level: Int = LOG_LEVEL) { | 67 | private fun startLogging(level: Int = LOG_LEVEL) { |
62 | text_log.append(NEW_LINE) | 68 | appendLog(resources.getString(R.string.message_log_level_set, level)) |
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 -> | 69 | Tinc.log(TincVpnService.getCurrentNetName()!!, level).let { process -> |
67 | logger = process | 70 | logger = process |
68 | Executor.runAsyncTask { printLog(process) } | 71 | Executor.runAsyncTask { printLog(process) } |
69 | } | 72 | } |
73 | logUpdateTimer = timer(period = UPDATE_INTERVAL, action = { updateLog() }) | ||
70 | } | 74 | } |
71 | 75 | ||
72 | private fun stopLogging() { | 76 | private fun stopLogging() { |
73 | logger?.destroy() | 77 | logger?.destroy() |
74 | logger = null | 78 | logger = null |
79 | logUpdateTimer?.cancel() | ||
80 | logUpdateTimer?.purge() | ||
81 | logUpdateTimer = null | ||
82 | appendLog(resources.getString(R.string.message_log_paused)) | ||
83 | updateLog() | ||
75 | } | 84 | } |
76 | 85 | ||
77 | private fun printLog(logger: Process) { | 86 | private fun printLog(logger: Process) { |
78 | logger.inputStream?.use { inputStream -> | 87 | logger.inputStream?.use { inputStream -> |
79 | inputStream.bufferedReader().useLines { lines -> | 88 | inputStream.bufferedReader().useLines { lines -> |
80 | lines.forEach { | 89 | lines.forEach { appendLog(it) } |
81 | text_log.post { | ||
82 | text_log.append(NEW_LINE) | ||
83 | text_log.append(it) | ||
84 | text_log.append(NEW_LINE) | ||
85 | } | ||
86 | } | ||
87 | } | 90 | } |
88 | } | 91 | } |
89 | } | 92 | } |
93 | |||
94 | private fun appendLog(line: String) = synchronized(this) { | ||
95 | if (log.size >= LOG_LINES) log.removeFirst() | ||
96 | log.addLast(line) | ||
97 | } | ||
98 | |||
99 | private fun updateLog() = synchronized(this) { | ||
100 | log.joinToString(NEW_LINE + NEW_LINE, NEW_LINE, NEW_LINE).let { | ||
101 | text_log.post { text_log.text = it } | ||
102 | } | ||
103 | } | ||
90 | } | 104 | } |
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4a412ae..6eaed13 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml | |||
@@ -83,6 +83,7 @@ | |||
83 | <string name="message_daemon_exited">Tinc daemon exited during startup:\n%1$s\n\nCheck the logs for more details.</string> | 83 | <string name="message_daemon_exited">Tinc daemon exited during startup:\n%1$s\n\nCheck the logs for more details.</string> |
84 | <string name="message_loading">Loading…</string> | 84 | <string name="message_loading">Loading…</string> |
85 | <string name="message_log_level_set">Log level set to %1$d.</string> | 85 | <string name="message_log_level_set">Log level set to %1$d.</string> |
86 | <string name="message_log_paused">Logging paused.</string> | ||
86 | 87 | ||
87 | <string name="value_none">none</string> | 88 | <string name="value_none">none</string> |
88 | <string name="value_yes">yes</string> | 89 | <string name="value_yes">yes</string> |