Skip to content

Commit

Permalink
[feat]: added option to export logs not in original format (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
F0x1d committed Jun 29, 2024
1 parent ce59aa6 commit b31b7fb
Show file tree
Hide file tree
Showing 24 changed files with 254 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,16 @@ import android.Manifest
import android.annotation.SuppressLint
import android.app.ActivityManager
import android.app.NotificationManager
import android.app.UiModeManager
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.content.res.Configuration
import android.os.Build
import android.util.TypedValue
import android.view.inputmethod.InputMethodManager
import android.widget.Toast
import androidx.annotation.AttrRes
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContextCompat
import androidx.core.content.getSystemService
Expand All @@ -26,8 +23,12 @@ import java.io.File
import kotlin.system.exitProcess


fun Context.copyText(text: String) = (getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager)
.setPrimaryClip(ClipData.newPlainText("LogFox", text))
fun Context.copyText(text: String) = runCatching {
(getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager)
.setPrimaryClip(ClipData.newPlainText("LogFox", text))
}.onFailure {
toast(Strings.error)
}

val Context.hasPermissionToReadLogs: Boolean get() = ContextCompat.checkSelfPermission(
this,
Expand All @@ -37,7 +38,6 @@ val Context.hasPermissionToReadLogs: Boolean get() = ContextCompat.checkSelfPerm
val Context.notificationManagerCompat get() = NotificationManagerCompat.from(this)
val Context.notificationManager get() = getSystemService<NotificationManager>()!!
val Context.activityManager get() = getSystemService<ActivityManager>()!!
@get:RequiresApi(Build.VERSION_CODES.S) val Context.uiModeManager get() = getSystemService<UiModeManager>()!!
val Context.inputMethodManager get() = getSystemService<InputMethodManager>()!!

fun Context.hardRestartApp() {
Expand Down
12 changes: 12 additions & 0 deletions core/core-navigation/src/main/res/navigation/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@
app:exitAnim="@anim/nav_default_exit_anim"
app:popEnterAnim="@anim/nav_default_pop_enter_anim"
app:popExitAnim="@anim/nav_default_pop_exit_anim" />
<action
android:id="@+id/action_settingsMenuFragment_to_settingsLinksFragment"
app:destination="@id/settingsLinksFragment"
app:enterAnim="@anim/nav_default_enter_anim"
app:exitAnim="@anim/nav_default_exit_anim"
app:popEnterAnim="@anim/nav_default_pop_enter_anim"
app:popExitAnim="@anim/nav_default_pop_exit_anim" />

<fragment
android:id="@+id/settingsUIFragment"
Expand All @@ -56,4 +63,9 @@
android:id="@+id/settingsCrashesFragment"
android:name="com.f0x1d.logfox.feature.settings.ui.fragment.SettingsCrashesFragment"
android:label="SettingsCrashesFragment" />

<fragment
android:id="@+id/settingsLinksFragment"
android:name="com.f0x1d.logfox.feature.settings.ui.fragment.SettingsLinksFragment"
android:label="SettingsLinksFragment" />
</navigation>
2 changes: 2 additions & 0 deletions core/core-preferences/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ plugins {
android.namespace = "com.f0x1d.logfox.preferences"

dependencies {
implementation(project(":data"))

implementation(project(":core:core-database"))

implementation(libs.bundles.androidx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ import android.content.Context
import androidx.core.content.edit
import androidx.preference.PreferenceManager
import com.f0x1d.logfox.database.entity.CrashType
import com.f0x1d.logfox.model.logline.LogLine
import com.f0x1d.logfox.model.preferences.ShowLogValues
import com.f0x1d.logfox.preferences.shared.base.BasePreferences
import dagger.hilt.android.qualifiers.ApplicationContext
import java.util.Date
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class AppPreferences @Inject constructor(
@ApplicationContext context: Context
@ApplicationContext private val context: Context,
): BasePreferences(context) {

companion object {
Expand Down Expand Up @@ -44,6 +47,9 @@ class AppPreferences @Inject constructor(
var resumeLoggingWithBottomTouch
get() = get("pref_resume_logs_with_touch", true)
set(value) { put("pref_resume_logs_with_touch", value) }
var exportLogsInOriginalFormat
get() = get("pref_export_logs_in_original_format", true)
set(value) { put("pref_export_logs_in_original_format", value) }

var showLogDate
get() = get("pref_show_log_date", false)
Expand Down Expand Up @@ -98,7 +104,7 @@ class AppPreferences @Inject constructor(
showLogTid,
showLogPackage,
showLogTag,
showLogContent
showLogContent,
)

fun collectingFor(crashType: CrashType) = get(
Expand All @@ -114,27 +120,19 @@ class AppPreferences @Inject constructor(
putInt("pref_selected_terminal_index", index)
}

override fun providePreferences(context: Context) = PreferenceManager.getDefaultSharedPreferences(context)
}
fun originalOf(
logLine: LogLine,
formatDate: (Long) -> String = { Date(it).toLocaleString() },
formatTime: (Long) -> String = { Date(it).toLocaleString() },
): String = if (exportLogsInOriginalFormat) {
logLine.originalContent
} else {
logLine.formatOriginal(
values = showLogValues,
formatDate = formatDate,
formatTime = formatTime,
)
}

data class ShowLogValues(
val date: Boolean,
val time: Boolean,
val uid: Boolean,
val pid: Boolean,
val tid: Boolean,
val packageName: Boolean,
val tag: Boolean,
val content: Boolean
) {
val asArray = booleanArrayOf(
date,
time,
uid,
pid,
tid,
packageName,
tag,
content
)
override fun providePreferences(context: Context) = PreferenceManager.getDefaultSharedPreferences(context)
}
12 changes: 12 additions & 0 deletions core/core-ui/src/main/res/drawable/ic_add_link.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:tint="?colorSecondary"
android:viewportHeight="24"
android:viewportWidth="24"
android:width="24dp">

<path
android:fillColor="@android:color/white"
android:pathData="M8,11h8v2H8V11zM20.1,12H22c0,-2.76 -2.24,-5 -5,-5h-4v1.9h4C18.71,8.9 20.1,10.29 20.1,12zM3.9,12c0,-1.71 1.39,-3.1 3.1,-3.1h4V7H7c-2.76,0 -5,2.24 -5,5s2.24,5 5,5h4v-1.9H7C5.29,15.1 3.9,13.71 3.9,12zM19,12h-2v3h-3v2h3v3h2v-3h3v-2h-3V12z" />

</vector>
24 changes: 22 additions & 2 deletions data/src/main/kotlin/com/f0x1d/logfox/model/logline/LogLine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.f0x1d.logfox.model.logline

import androidx.annotation.Keep
import com.f0x1d.logfox.model.Identifiable
import com.f0x1d.logfox.model.preferences.ShowLogValues
import java.util.Date

data class LogLine(
override val id: Long,
Expand All @@ -13,8 +15,26 @@ data class LogLine(
val level: LogLevel = LogLevel.INFO,
val tag: String = "",
val content: String,
val original: String
) : Identifiable
val originalContent: String,
) : Identifiable {

fun formatOriginal(
values: ShowLogValues,
formatDate: (Long) -> String = { Date(it).toLocaleString() },
formatTime: (Long) -> String = { Date(it).toLocaleString() },
): String = values.run {
buildString {
if (date) append(formatDate(this@LogLine.dateAndTime) + " ")
if (time) append(formatTime(this@LogLine.dateAndTime) + " ")
if (uid) append(this@LogLine.uid + " ")
if (pid) append(this@LogLine.pid + " ")
if (tid) append(this@LogLine.tid + " ")
if (packageName && this@LogLine.packageName != null) append(this@LogLine.packageName + " ")
if (tag) append(this@LogLine.tag + if (content) ": " else "")
if (content) append(this@LogLine.content)
}
}
}

@Keep
enum class LogLevel(val letter: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fun LogLine(
level = mapLevel(groupValues[5]),
tag = groupValues[6].trim(),
content = groupValues[7],
original = groupValues[0]
originalContent = groupValues[0],
)
}
}.getOrNull()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.f0x1d.logfox.model.preferences

data class ShowLogValues(
val date: Boolean,
val time: Boolean,
val uid: Boolean,
val pid: Boolean,
val tid: Boolean,
val packageName: Boolean,
val tag: Boolean,
val content: Boolean
) {
val asArray = booleanArrayOf(
date,
time,
uid,
pid,
tid,
packageName,
tag,
content
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import com.f0x1d.logfox.model.logline.LogLine
import com.f0x1d.logfox.preferences.shared.AppPreferences

class LogsAdapter(
appPreferences: AppPreferences,
private val appPreferences: AppPreferences,
private val selectedItem: (LogLine, Boolean) -> Unit,
private val copyLog: (LogLine) -> Unit
): BaseListAdapter<LogLine, ItemLogBinding>(diffCallback<LogLine>()) {
Expand Down Expand Up @@ -41,6 +41,6 @@ class LogsAdapter(
override fun createHolder(layoutInflater: LayoutInflater, parent: ViewGroup) = LogViewHolder(
binding = ItemLogBinding.inflate(layoutInflater, parent, false),
selectedItem = selectedItem,
copyLog = copyLog
copyLog = copyLog,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ class LogsFragment: BaseViewModelFragment<LogsViewModel, FragmentLogsBinding>(),
appPreferences = viewModel.appPreferences,
selectedItem = viewModel::selectLine,
copyLog = {
requireContext().copyText(it.original)
requireContext().copyText(
viewModel.appPreferences.originalOf(
logLine = it,
formatDate = viewModel.dateTimeFormatter::formatDate,
formatTime = viewModel.dateTimeFormatter::formatTime,
)
)
snackbar(Strings.text_copied)
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,12 @@ class LogViewHolder(
levelView.textSize = it
}

logText.text = buildString {
adapter<LogsAdapter>()?.logsFormat?.apply {
if (date) append(dateTimeFormatter.formatDate(data.dateAndTime) + " ")
if (time) append(dateTimeFormatter.formatTime(data.dateAndTime) + " ")
if (uid) append(data.uid + " ")
if (pid) append(data.pid + " ")
if (tid) append(data.tid + " ")
if (packageName && data.packageName != null) append(data.packageName + " ")
if (tag) append(data.tag + if (content) ": " else "")
if (content) append(data.content)
}
adapter<LogsAdapter>()?.logsFormat?.let { values ->
logText.text = data.formatOriginal(
values = values,
formatDate = dateTimeFormatter::formatDate,
formatTime = dateTimeFormatter::formatTime,
)
}

levelView.logLevel = data.level
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,13 @@ class LogsViewModel @Inject constructor(

val selectedItems = MutableStateFlow(emptyList<LogLine>())

val selectedItemsContent get() = selectedItems.value.joinToString("\n") { it.original }
val selectedItemsContent get() = selectedItems.value.joinToString("\n") { line ->
appPreferences.originalOf(
logLine = line,
formatDate = dateTimeFormatter::formatDate,
formatTime = dateTimeFormatter::formatTime,
)
}

val logs = combine(
fileUri?.readFileContentsAsFlow(
Expand Down Expand Up @@ -121,7 +127,11 @@ class LogsViewModel @Inject constructor(
ctx.contentResolver.openOutputStream(uri)?.use {
it.write(
selectedItems.value.joinToString("\n") { line ->
line.original
appPreferences.originalOf(
logLine = line,
formatDate = dateTimeFormatter::formatDate,
formatTime = dateTimeFormatter::formatTime,
)
}.encodeToByteArray()
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal fun Uri?.readFileContentsAsFlow(
val logLine = LogLine(id, line, context) ?: LogLine(
id = id,
content = line,
original = line,
originalContent = line,
)

logLines.add(logLine)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package com.f0x1d.logfox.feature.recordings.core.controller.reader

import com.f0x1d.logfox.database.entity.UserFilter
import com.f0x1d.logfox.datetime.DateTimeFormatter
import com.f0x1d.logfox.feature.logging.core.model.filterAndSearch
import com.f0x1d.logfox.feature.recordings.core.controller.reader.base.RecordingReader
import com.f0x1d.logfox.model.logline.LogLine
import com.f0x1d.logfox.preferences.shared.AppPreferences
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import javax.inject.Inject

open class RecordingWithFiltersReader @Inject constructor(): RecordingReader() {
open class RecordingWithFiltersReader @Inject constructor(
appPreferences: AppPreferences,
dateTimeFormatter: DateTimeFormatter,
): RecordingReader(appPreferences, dateTimeFormatter) {

private var filters = emptyList<UserFilter>()
private val filtersMutex = Mutex()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.f0x1d.logfox.feature.recordings.core.controller.reader

import com.f0x1d.logfox.datetime.DateTimeFormatter
import com.f0x1d.logfox.model.logline.LogLine
import com.f0x1d.logfox.preferences.shared.AppPreferences
import kotlinx.coroutines.sync.Mutex
Expand All @@ -10,8 +11,9 @@ import javax.inject.Singleton

@Singleton
class RewritingRecordingReader @Inject constructor(
private val appPreferences: AppPreferences
): RecordingWithFiltersReader() {
appPreferences: AppPreferences,
dateTimeFormatter: DateTimeFormatter,
): RecordingWithFiltersReader(appPreferences, dateTimeFormatter) {

// recordedLines are cleared, so manual dumpLines call may result in some lines be lost
// This stateLines are not cleared and are just held at the size of recordedLines
Expand Down Expand Up @@ -39,9 +41,10 @@ class RewritingRecordingReader @Inject constructor(

override suspend fun writeToFile(content: String) = fileMutex.withLock {
val myContent = stateLinesMutex.withLock {
stateLines.joinToString(separator = "\n") {
/*stateLines.joinToString(separator = "\n") {
it.original
}
}*/
" TODO: return it :) "
}

recordingFile?.writeText(myContent)
Expand Down
Loading

0 comments on commit b31b7fb

Please sign in to comment.