aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/pacien/tincapp/activities/viewlog/LogLiveData.kt
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/activities/viewlog/LogLiveData.kt')
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/viewlog/LogLiveData.kt74
1 files changed, 74 insertions, 0 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/activities/viewlog/LogLiveData.kt b/app/src/main/java/org/pacien/tincapp/activities/viewlog/LogLiveData.kt
new file mode 100644
index 0000000..f410a8c
--- /dev/null
+++ b/app/src/main/java/org/pacien/tincapp/activities/viewlog/LogLiveData.kt
@@ -0,0 +1,74 @@
1/*
2 * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon
3 * Copyright (C) 2017-2018 Pacien TRAN-GIRARD
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19package org.pacien.tincapp.activities.viewlog
20
21import android.arch.lifecycle.LiveData
22import org.pacien.tincapp.commands.Executor
23import org.pacien.tincapp.commands.Tinc
24import java.util.*
25import kotlin.concurrent.timer
26
27/**
28 * @author pacien
29 */
30class LogLiveData(private val netName: String, private val logLevel: Int, private val logLineSize: Int) : LiveData<List<String>>() {
31 private val updateInterval = 250L // milliseconds
32 private val executor = Executor
33 private val log = LinkedList<String>()
34 private var loggerProcess: Process? = null
35 private var logUpdateTimer: Timer? = null
36
37 override fun onActive() {
38 loggerProcess = startNewLogger()
39 logUpdateTimer = timer(period = updateInterval, action = { outputLog() })
40 }
41
42 override fun onInactive() {
43 loggerProcess?.destroy()
44 logUpdateTimer?.apply { cancel() }?.apply { purge() }
45 }
46
47 private fun startNewLogger(): Process {
48 val newProcess = Tinc.log(netName, logLevel)
49 executor.runAsyncTask { captureProcessOutput(newProcess) }
50 return newProcess
51 }
52
53 private fun captureProcessOutput(process: Process) {
54 process.inputStream?.use { inputStream ->
55 inputStream.bufferedReader().useLines { lines ->
56 lines.forEach { appendToLog(it) }
57 }
58 }
59 }
60
61 private fun appendToLog(line: String) {
62 synchronized(log) {
63 if (log.size >= logLineSize) log.removeFirst()
64 log.addLast(line)
65 }
66 }
67
68 private fun outputLog() {
69 synchronized(log) {
70 val logView = ArrayList(log)
71 postValue(logView)
72 }
73 }
74}