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
|
@ -29,114 +29,118 @@
|
||||||
|
|
||||||
void log_cb_android(ChiakiLogLevel level, const char *msg, void *user)
|
void log_cb_android(ChiakiLogLevel level, const char *msg, void *user)
|
||||||
{
|
{
|
||||||
int prio;
|
int prio;
|
||||||
switch(level)
|
switch(level)
|
||||||
{
|
{
|
||||||
case CHIAKI_LOG_DEBUG:
|
case CHIAKI_LOG_DEBUG:
|
||||||
prio = ANDROID_LOG_DEBUG;
|
prio = ANDROID_LOG_DEBUG;
|
||||||
break;
|
break;
|
||||||
case CHIAKI_LOG_VERBOSE:
|
case CHIAKI_LOG_VERBOSE:
|
||||||
prio = ANDROID_LOG_VERBOSE;
|
prio = ANDROID_LOG_VERBOSE;
|
||||||
break;
|
break;
|
||||||
case CHIAKI_LOG_INFO:
|
case CHIAKI_LOG_INFO:
|
||||||
prio = ANDROID_LOG_INFO;
|
prio = ANDROID_LOG_INFO;
|
||||||
break;
|
break;
|
||||||
case CHIAKI_LOG_WARNING:
|
case CHIAKI_LOG_WARNING:
|
||||||
prio = ANDROID_LOG_ERROR;
|
prio = ANDROID_LOG_ERROR;
|
||||||
break;
|
break;
|
||||||
case CHIAKI_LOG_ERROR:
|
case CHIAKI_LOG_ERROR:
|
||||||
prio = ANDROID_LOG_ERROR;
|
prio = ANDROID_LOG_ERROR;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
prio = ANDROID_LOG_INFO;
|
prio = ANDROID_LOG_INFO;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
__android_log_write(prio, LOG_TAG, msg);
|
__android_log_write(prio, LOG_TAG, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void log_cb_android_file(ChiakiLogLevel level, const char *msg, void *user)
|
static void log_cb_android_file(ChiakiLogLevel level, const char *msg, void *user)
|
||||||
{
|
{
|
||||||
log_cb_android(level, msg, user);
|
log_cb_android(level, msg, user);
|
||||||
FILE *f = user;
|
FILE *f = user;
|
||||||
if(!f)
|
if(!f)
|
||||||
return;
|
return;
|
||||||
switch(level)
|
switch(level)
|
||||||
{
|
{
|
||||||
case CHIAKI_LOG_DEBUG:
|
case CHIAKI_LOG_DEBUG:
|
||||||
fwrite("[D] ", 4, 1, f);
|
fwrite("[D] ", 4, 1, f);
|
||||||
break;
|
break;
|
||||||
case CHIAKI_LOG_VERBOSE:
|
case CHIAKI_LOG_VERBOSE:
|
||||||
fwrite("[V] ", 4, 1, f);
|
fwrite("[V] ", 4, 1, f);
|
||||||
break;
|
break;
|
||||||
case CHIAKI_LOG_INFO:
|
case CHIAKI_LOG_INFO:
|
||||||
fwrite("[I] ", 4, 1, f);
|
fwrite("[I] ", 4, 1, f);
|
||||||
break;
|
break;
|
||||||
case CHIAKI_LOG_WARNING:
|
case CHIAKI_LOG_WARNING:
|
||||||
fwrite("[W] ", 4, 1, f);
|
fwrite("[W] ", 4, 1, f);
|
||||||
break;
|
break;
|
||||||
case CHIAKI_LOG_ERROR:
|
case CHIAKI_LOG_ERROR:
|
||||||
fwrite("[E] ", 4, 1, f);
|
fwrite("[E] ", 4, 1, f);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
fwrite("[?] ", 4, 1, f);
|
fwrite("[?] ", 4, 1, f);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
fwrite(msg, strlen(msg), 1, f);
|
fwrite(msg, strlen(msg), 1, f);
|
||||||
fwrite("\n", 1, 1, f);
|
fwrite("\n", 1, 1, f);
|
||||||
}
|
}
|
||||||
|
|
||||||
ChiakiErrorCode android_chiaki_file_log_init(ChiakiLog *log, uint32_t level, const char *file)
|
ChiakiErrorCode android_chiaki_file_log_init(ChiakiLog *log, uint32_t level, const char *file)
|
||||||
{
|
{
|
||||||
chiaki_log_init(log, level, log_cb_android, NULL);
|
chiaki_log_init(log, level, log_cb_android, NULL);
|
||||||
if(file)
|
if(file)
|
||||||
{
|
{
|
||||||
FILE *f = fopen(file, "w+");
|
FILE *f = fopen(file, "w+");
|
||||||
if(!f)
|
if(!f)
|
||||||
{
|
{
|
||||||
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;
|
||||||
}
|
}
|
||||||
log->user = f;
|
else
|
||||||
log->cb = log_cb_android_file;
|
{
|
||||||
}
|
log->user = f;
|
||||||
return CHIAKI_ERR_SUCCESS;
|
log->cb = log_cb_android_file;
|
||||||
|
CHIAKI_LOGI(log, "Logging to file %s", file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return CHIAKI_ERR_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
void android_chiaki_file_log_fini(ChiakiLog *log)
|
void android_chiaki_file_log_fini(ChiakiLog *log)
|
||||||
{
|
{
|
||||||
if(log->user)
|
if(log->user)
|
||||||
{
|
{
|
||||||
FILE *f = log->user;
|
FILE *f = log->user;
|
||||||
fclose(f);
|
fclose(f);
|
||||||
log->user = NULL;
|
log->user = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static void android_chiaki_log_cb(ChiakiLogLevel level, const char *msg, void *user)
|
static void android_chiaki_log_cb(ChiakiLogLevel level, const char *msg, void *user)
|
||||||
{
|
{
|
||||||
log_cb_android(level, msg, NULL);
|
log_cb_android(level, msg, NULL);
|
||||||
|
|
||||||
AndroidChiakiJNILog *log = user;
|
AndroidChiakiJNILog *log = user;
|
||||||
JNIEnv *env = attach_thread_jni();
|
JNIEnv *env = attach_thread_jni();
|
||||||
if(!env)
|
if(!env)
|
||||||
return;
|
return;
|
||||||
E->CallVoidMethod(env, log->java_log, log->java_log_meth, (jint)level, jnistr_from_ascii(env, msg));
|
E->CallVoidMethod(env, log->java_log, log->java_log_meth, (jint)level, jnistr_from_ascii(env, msg));
|
||||||
(*global_vm)->DetachCurrentThread(global_vm);
|
(*global_vm)->DetachCurrentThread(global_vm);
|
||||||
}
|
}
|
||||||
|
|
||||||
void android_chiaki_jni_log_init(AndroidChiakiJNILog *log, JNIEnv *env, jobject java_log)
|
void android_chiaki_jni_log_init(AndroidChiakiJNILog *log, JNIEnv *env, jobject java_log)
|
||||||
{
|
{
|
||||||
log->java_log = E->NewGlobalRef(env, java_log);
|
log->java_log = E->NewGlobalRef(env, java_log);
|
||||||
jclass log_class = E->GetObjectClass(env, log->java_log);
|
jclass log_class = E->GetObjectClass(env, log->java_log);
|
||||||
log->java_log_meth = E->GetMethodID(env, log_class, "log", "(ILjava/lang/String;)V");
|
log->java_log_meth = E->GetMethodID(env, log_class, "log", "(ILjava/lang/String;)V");
|
||||||
log->log.level_mask = (uint32_t)E->GetIntField(env, log->java_log, E->GetFieldID(env, log_class, "levelMask", "I"));
|
log->log.level_mask = (uint32_t)E->GetIntField(env, log->java_log, E->GetFieldID(env, log_class, "levelMask", "I"));
|
||||||
log->log.cb = android_chiaki_log_cb;
|
log->log.cb = android_chiaki_log_cb;
|
||||||
log->log.user = log;
|
log->log.user = log;
|
||||||
}
|
}
|
||||||
|
|
||||||
void android_chiaki_jni_log_fini(AndroidChiakiJNILog *log, JNIEnv *env)
|
void android_chiaki_jni_log_fini(AndroidChiakiJNILog *log, JNIEnv *env)
|
||||||
{
|
{
|
||||||
E->DeleteGlobalRef(env, log->java_log);
|
E->DeleteGlobalRef(env, log->java_log);
|
||||||
}
|
}
|
|
@ -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