Skip to content

Commit

Permalink
Allow disable File Logger (#20)
Browse files Browse the repository at this point in the history
* Allow disable File Logger

* Remove share persisted log if file logger is disabled
  • Loading branch information
jdelga authored Dec 16, 2024
1 parent 24ee17b commit 35db726
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@ import java.util.concurrent.Executors
internal var appLoggerBLInstance: AppLoggerBL? = null

@JvmOverloads
fun initAppLogger(context: Context, logCategories: List<LogCategory> = emptyList()) {
fun initAppLogger(
context: Context,
logCategories: List<LogCategory> = emptyList(),
logToDisk: Boolean = true
) {
appLoggerBLInstance = AppLoggerBL(
fileLogger = AppFileLogger(
appContext = context,
executor = Executor(Executors.newSingleThreadExecutor())),
fileLogger = if (logToDisk) {
AppFileLogger(context,Executor(Executors.newSingleThreadExecutor()))
} else {
null
},
consoleLogger = AppConsoleLogger()
).apply {
init(context, logCategories)
Expand All @@ -35,6 +41,9 @@ internal fun getLogs(): LiveData<List<LogEntry>> =
internal fun getCategories(): List<LogCategory> =
appLoggerBLInstance?.categories ?: emptyList()

internal fun arePersistedLogsEnabled() : Boolean =
appLoggerBLInstance?.arePersistedLogsEnabled() ?: false

internal fun getPersistedLogs(callback: TaskCallback<Uri>) {
appLoggerBLInstance?.getPersistedLogs(callback)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import java.util.*
import kotlin.collections.ArrayList

internal open class AppLoggerBL(
private val fileLogger: AppFileLogger,
private val fileLogger: AppFileLogger?,
private val consoleLogger: AppConsoleLogger
) {
private val logs: LinkedList<LogEntry> = LinkedList()
Expand All @@ -32,7 +32,7 @@ internal open class AppLoggerBL(
}
}.groupBy({ it.first }, { it.second })
logsData.value = emptyList()
fileLogger.init()
fileLogger?.init()
}

open fun log(@LogPriority priority: Int, tag: String, message: String, throwable: Throwable?) {
Expand All @@ -47,19 +47,22 @@ internal open class AppLoggerBL(
message = messageToLog
)
)
fileLogger.log(priority, tag, messageToLog)
fileLogger?.log(priority, tag, messageToLog)
consoleLogger.log(priority, tag, messageToLog)
}

open fun getLogs(): LiveData<List<LogEntry>> =
logsData

open fun arePersistedLogsEnabled(): Boolean =
fileLogger != null

open fun getPersistedLogs(callback: TaskCallback<Uri>) {
fileLogger.getReport(callback)
fileLogger?.getReport(callback)
}

fun storeVisibleLogs(visibleLogs: String, callback: TaskCallback<Uri>) {
fileLogger.storeVisibleLogs(visibleLogs, callback)
fileLogger?.storeVisibleLogs(visibleLogs, callback)
}

private fun addLog(logEntry: LogEntry) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import com.google.android.material.chip.Chip
import com.google.android.material.chip.ChipGroup
import com.telefonica.androidlogger.R
import com.telefonica.androidlogger.domain.LogCategory
import com.telefonica.androidlogger.domain.arePersistedLogsEnabled
import com.telefonica.androidlogger.io.executor.TaskCallback
import com.telefonica.androidlogger.ui.adapter.LogListAdapter
import com.telefonica.androidlogger.ui.viewmodel.AppLoggerViewModel
Expand Down Expand Up @@ -79,7 +80,9 @@ class AppLoggerActivity : AppCompatActivity() {

override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_app_logger, menu)

if (!arePersistedLogsEnabled()) {
menu.removeItem(R.id.action_share_all)
}
searchView = (menu.findItem(R.id.action_search).actionView as SearchView).apply {
val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager
setSearchableInfo(searchManager.getSearchableInfo(componentName))
Expand Down

0 comments on commit 35db726

Please sign in to comment.