mirror of
https://git.sr.ht/~thestr4ng3r/chiaki
synced 2025-08-21 14:03:11 -07:00
Add LogManager to Android
This commit is contained in:
parent
7257ceb2ce
commit
03a4d53d72
5 changed files with 169 additions and 88 deletions
|
@ -96,8 +96,12 @@ ChiakiErrorCode android_chiaki_file_log_init(ChiakiLog *log, uint32_t level, con
|
||||||
CHIAKI_LOGE(log, "Failed to open log file %s for writing: %s", file, strerror(errno));
|
CHIAKI_LOGE(log, "Failed to open log file %s for writing: %s", file, strerror(errno));
|
||||||
return CHIAKI_ERR_UNKNOWN;
|
return CHIAKI_ERR_UNKNOWN;
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
log->user = f;
|
log->user = f;
|
||||||
log->cb = log_cb_android_file;
|
log->cb = log_cb_android_file;
|
||||||
|
CHIAKI_LOGI(log, "Logging to file %s", file);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return CHIAKI_ERR_SUCCESS;
|
return CHIAKI_ERR_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,74 @@
|
||||||
|
/*
|
||||||
|
* This file is part of Chiaki.
|
||||||
|
*
|
||||||
|
* Chiaki is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* Chiaki is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with Chiaki. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.metallic.chiaki.common
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import java.io.File
|
||||||
|
import java.io.FilenameFilter
|
||||||
|
import java.text.SimpleDateFormat
|
||||||
|
import java.util.*
|
||||||
|
import java.util.regex.Pattern
|
||||||
|
|
||||||
|
private val dateFormat = SimpleDateFormat("yyyy-MM-dd_HH-mm-ss-SSSSSS", Locale.US)
|
||||||
|
private val filePrefix = "chiaki_session_"
|
||||||
|
private val filePostfix = ".log"
|
||||||
|
private val fileRegex = Regex("$filePrefix(.*)$filePostfix")
|
||||||
|
private val keepLogFilesCount = 5
|
||||||
|
|
||||||
|
class LogFile private constructor(val logManager: LogManager, val filename: String)
|
||||||
|
{
|
||||||
|
val date = fileRegex.matchEntire(filename)?.groupValues?.get(1)?.let {
|
||||||
|
dateFormat.parse(it)
|
||||||
|
} ?: throw IllegalArgumentException()
|
||||||
|
|
||||||
|
val file get() = File(logManager.baseDir, filename)
|
||||||
|
|
||||||
|
companion object
|
||||||
|
{
|
||||||
|
fun fromFilename(logManager: LogManager, filename: String) = try { LogFile(logManager, filename) } catch(e: IllegalArgumentException) { null }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class LogManager(context: Context)
|
||||||
|
{
|
||||||
|
val baseDir = File(context.filesDir, "session_logs").also {
|
||||||
|
it.mkdirs()
|
||||||
|
}
|
||||||
|
|
||||||
|
val files: List<LogFile> get() =
|
||||||
|
(baseDir.list { _, s -> s.matches(fileRegex) }?.toList() ?: listOf()).mapNotNull {
|
||||||
|
LogFile.fromFilename(this, it)
|
||||||
|
}.sortedByDescending {
|
||||||
|
it.date
|
||||||
|
}
|
||||||
|
|
||||||
|
fun createNewFile(): LogFile
|
||||||
|
{
|
||||||
|
val currentFiles = files
|
||||||
|
if(currentFiles.size > keepLogFilesCount)
|
||||||
|
{
|
||||||
|
currentFiles.subList(keepLogFilesCount, currentFiles.size).forEach {
|
||||||
|
it.file.delete()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val date = Date()
|
||||||
|
val filename = "$filePrefix${dateFormat.format(date)}$filePostfix"
|
||||||
|
return LogFile.fromFilename(this, filename)!!
|
||||||
|
}
|
||||||
|
}
|
|
@ -22,6 +22,7 @@ import android.util.Log
|
||||||
import android.view.*
|
import android.view.*
|
||||||
import androidx.lifecycle.LiveData
|
import androidx.lifecycle.LiveData
|
||||||
import androidx.lifecycle.MutableLiveData
|
import androidx.lifecycle.MutableLiveData
|
||||||
|
import com.metallic.chiaki.common.LogManager
|
||||||
import com.metallic.chiaki.lib.*
|
import com.metallic.chiaki.lib.*
|
||||||
|
|
||||||
sealed class StreamState
|
sealed class StreamState
|
||||||
|
@ -32,7 +33,7 @@ data class StreamStateCreateError(val error: CreateError): StreamState()
|
||||||
data class StreamStateQuit(val reason: QuitReason, val reasonString: String?): StreamState()
|
data class StreamStateQuit(val reason: QuitReason, val reasonString: String?): StreamState()
|
||||||
data class StreamStateLoginPinRequest(val pinIncorrect: Boolean): StreamState()
|
data class StreamStateLoginPinRequest(val pinIncorrect: Boolean): StreamState()
|
||||||
|
|
||||||
class StreamSession(val connectInfo: ConnectInfo, val logVerbose: Boolean, val input: StreamInput)
|
class StreamSession(val connectInfo: ConnectInfo, val logManager: LogManager, val logVerbose: Boolean, val input: StreamInput)
|
||||||
{
|
{
|
||||||
var session: Session? = null
|
var session: Session? = null
|
||||||
private set
|
private set
|
||||||
|
@ -69,7 +70,7 @@ class StreamSession(val connectInfo: ConnectInfo, val logVerbose: Boolean, val i
|
||||||
return
|
return
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
val session = Session(connectInfo, null, logVerbose) // TODO: log file
|
val session = Session(connectInfo, logManager.createNewFile().file.absolutePath, logVerbose)
|
||||||
_state.value = StreamStateConnecting
|
_state.value = StreamStateConnecting
|
||||||
session.eventCallback = this::eventCallback
|
session.eventCallback = this::eventCallback
|
||||||
session.start()
|
session.start()
|
||||||
|
|
|
@ -34,6 +34,7 @@ import androidx.fragment.app.Fragment
|
||||||
import androidx.lifecycle.*
|
import androidx.lifecycle.*
|
||||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||||
import com.metallic.chiaki.R
|
import com.metallic.chiaki.R
|
||||||
|
import com.metallic.chiaki.common.LogManager
|
||||||
import com.metallic.chiaki.common.Preferences
|
import com.metallic.chiaki.common.Preferences
|
||||||
import com.metallic.chiaki.common.ext.viewModelFactory
|
import com.metallic.chiaki.common.ext.viewModelFactory
|
||||||
import com.metallic.chiaki.lib.ConnectInfo
|
import com.metallic.chiaki.lib.ConnectInfo
|
||||||
|
@ -69,7 +70,7 @@ class StreamActivity : AppCompatActivity(), View.OnSystemUiVisibilityChangeListe
|
||||||
}
|
}
|
||||||
|
|
||||||
viewModel = ViewModelProviders.of(this, viewModelFactory {
|
viewModel = ViewModelProviders.of(this, viewModelFactory {
|
||||||
StreamViewModel(Preferences(this), connectInfo)
|
StreamViewModel(Preferences(this), LogManager(this), connectInfo)
|
||||||
})[StreamViewModel::class.java]
|
})[StreamViewModel::class.java]
|
||||||
|
|
||||||
setContentView(R.layout.activity_stream)
|
setContentView(R.layout.activity_stream)
|
||||||
|
|
|
@ -20,16 +20,17 @@ package com.metallic.chiaki.stream
|
||||||
import androidx.lifecycle.LiveData
|
import androidx.lifecycle.LiveData
|
||||||
import androidx.lifecycle.MutableLiveData
|
import androidx.lifecycle.MutableLiveData
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
|
import com.metallic.chiaki.common.LogManager
|
||||||
import com.metallic.chiaki.session.StreamSession
|
import com.metallic.chiaki.session.StreamSession
|
||||||
import com.metallic.chiaki.common.Preferences
|
import com.metallic.chiaki.common.Preferences
|
||||||
import com.metallic.chiaki.lib.*
|
import com.metallic.chiaki.lib.*
|
||||||
import com.metallic.chiaki.session.StreamInput
|
import com.metallic.chiaki.session.StreamInput
|
||||||
|
|
||||||
class StreamViewModel(val preferences: Preferences, val connectInfo: ConnectInfo): ViewModel()
|
class StreamViewModel(val preferences: Preferences, val logManager: LogManager, val connectInfo: ConnectInfo): ViewModel()
|
||||||
{
|
{
|
||||||
private var _session: StreamSession? = null
|
private var _session: StreamSession? = null
|
||||||
val input = StreamInput(preferences)
|
val input = StreamInput(preferences)
|
||||||
val session = StreamSession(connectInfo, preferences.logVerbose, input)
|
val session = StreamSession(connectInfo, logManager, preferences.logVerbose, input)
|
||||||
|
|
||||||
private var _onScreenControlsEnabled = MutableLiveData<Boolean>(preferences.onScreenControlsEnabled)
|
private var _onScreenControlsEnabled = MutableLiveData<Boolean>(preferences.onScreenControlsEnabled)
|
||||||
val onScreenControlsEnabled: LiveData<Boolean> get() = _onScreenControlsEnabled
|
val onScreenControlsEnabled: LiveData<Boolean> get() = _onScreenControlsEnabled
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue