Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sharing visible logs via file #15

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,8 @@ internal fun getCategories(): List<LogCategory> =

internal fun getPersistedLogs(callback: TaskCallback<Uri>) {
appLoggerBLInstance?.getPersistedLogs(callback)
}

internal fun storeVisibleLogs(visibleLogs: String, callback: TaskCallback<Uri>) {
appLoggerBLInstance?.storeVisibleLogs(visibleLogs, callback)
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ internal open class AppLoggerBL(
fileLogger.getReport(callback)
}

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

private fun addLog(logEntry: LogEntry) {
synchronized(logs) {
logs.addLast(logEntry)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,23 @@ internal open class AppFileLogger(
callback = callback
)

private fun buildLogReportFile(): File {
val outputDir = File(appContext.cacheDir, REPORTS_DIRECTORY).apply { mkdir() }
val outputFile =
File.createTempFile(TEMPORAL_FILE_PREFIX, TEMPORAL_FILE_EXTENSION, outputDir)
fun storeVisibleLogs(visibleLogs: String, callback: TaskCallback<Uri>) {
executor.submit(
task = {
val outputFile = createTempFile()
outputFile.writeText(visibleLogs)
FileProvider.getUriForFile(
appContext,
appContext.packageName + FILE_PROVIDER_AUTHORITY_SUFFIX,
outputFile
)
},
callback = callback
)
}

private fun buildLogReportFile(): File {
val outputFile = createTempFile()
var source: Source? = null
var sink: BufferedSink? = null
try {
Expand All @@ -77,6 +89,11 @@ internal open class AppFileLogger(
return outputFile
}

private fun createTempFile(): File {
val outputDir = File(appContext.cacheDir, REPORTS_DIRECTORY).apply { mkdir() }
return File.createTempFile(TEMPORAL_FILE_PREFIX, TEMPORAL_FILE_EXTENSION, outputDir)
}

private fun buildLogReportStream(): InputStream =
getLogFiles()
.map { FileInputStream(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import com.telefonica.androidlogger.io.executor.TaskCallback
import com.telefonica.androidlogger.ui.viewmodel.AppLoggerViewModel
import com.telefonica.androidlogger.ui.viewmodel.LogCategoryViewModel
import com.telefonica.androidlogger.ui.viewmodel.LogPriorityViewModel
import java.io.File
import java.security.InvalidParameterException

class AppLoggerActivity : AppCompatActivity() {
Expand Down Expand Up @@ -286,12 +287,19 @@ class AppLoggerActivity : AppCompatActivity() {
}

private fun shareVisibleLogs() {
val shareIntent = Intent.createChooser(Intent().apply {
action = Intent.ACTION_SEND
type = "text/plain"
putExtra(Intent.EXTRA_TEXT, adapter.getVisibleInfoAsString())
}, null)
startActivity(shareIntent)
val visibleInfoAsString: String = adapter.getVisibleInfoAsString()
shareAllLogsCallback = TaskCallback<Uri> { uri ->
mainThreadHandler.post {
if (uri != null) {
launchShareAllLogsIntent(uri)
} else {
Toast.makeText(this, R.string.share_visible_logs_failure, Toast.LENGTH_SHORT)
.show()
}
}
}.apply {
viewModel.onLogsThatShouldBeStoredIntoAFile(visibleInfoAsString, this)
}
}

private fun shareAllLogs() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.telefonica.androidlogger.domain.LogEntry
import com.telefonica.androidlogger.domain.getCategories
import com.telefonica.androidlogger.domain.getLogs
import com.telefonica.androidlogger.domain.getPersistedLogs
import com.telefonica.androidlogger.domain.storeVisibleLogs
import com.telefonica.androidlogger.io.executor.TaskCallback
import com.telefonica.androidlogger.ui.livedata.filter
import com.telefonica.androidlogger.ui.livedata.map
Expand Down Expand Up @@ -97,6 +98,10 @@ internal class AppLoggerViewModel : ViewModel() {
getPersistedLogs(callback)
}

fun onLogsThatShouldBeStoredIntoAFile(visibleLogs: String, callback: TaskCallback<Uri>) {
storeVisibleLogs(visibleLogs, callback)
}

private fun List<LogEntry>.filterByLogLevel() =
filter {
it.priority.toLogPriority() >= minLogLevel
Expand Down
1 change: 1 addition & 0 deletions library/src/enabled/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<string name="copy_clipboard_success">Log copied to clipboard!</string>

<string name="share_all_logs_failure">Something went wrong retrieving persisted logs</string>
<string name="share_visible_logs_failure">Something went wrong while sharing visible logs</string>

<string name="log_level_filter_message">Minimum Log Level</string>
<string name="categories_filter_message">Categories</string>
Expand Down
Loading