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

Add date format to exported logs #71

Merged
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 @@ -4,9 +4,11 @@ import android.content.Context
import com.infinum.sentinel.ui.logger.models.BaseEntry
import com.infinum.sentinel.ui.logger.models.FlowBuffer
import com.infinum.sentinel.ui.logger.models.Level
import com.infinum.sentinel.ui.shared.Constants.LOG_DATE_TIME_FORMAT
import com.infinum.sentinel.ui.shared.LogFileResolver
import java.io.File
import java.util.Calendar
import java.text.SimpleDateFormat
import java.util.Locale
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
Expand All @@ -21,6 +23,8 @@ internal class SentinelFileTree(
private val logFileResolver = LogFileResolver(context)

override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
val dateTimeFormat = SimpleDateFormat(LOG_DATE_TIME_FORMAT, Locale.getDefault())

MainScope().launch {
withContext(Dispatchers.IO) {
val entry = Entry(
Expand All @@ -34,7 +38,7 @@ internal class SentinelFileTree(
buffer.enqueue(entry)

val file: File = logFileResolver.createOrOpenFile()
val line = entry.asLineString()
val line = entry.asLineString(dateTimeFormat)

file.appendText(line)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.infinum.sentinel
import android.content.Context
import androidx.startup.Initializer
import com.infinum.sentinel.ui.logger.models.FlowBuffer
import kotlinx.coroutines.MainScope
import timber.log.Timber

public class TimberInitializer : Initializer<Class<TimberInitializer>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import timber.log.Timber


public class LoggerActivity : AppCompatActivity() {

private companion object {
Expand Down Expand Up @@ -94,21 +93,21 @@ public class LoggerActivity : AppCompatActivity() {
with(binding) {
toolbar.setNavigationOnClickListener { finish() }
toolbar.subtitle = (
packageManager.getApplicationLabel(
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
packageManager.getApplicationInfo(
packageName,
PackageManager.ApplicationInfoFlags.of(PackageManager.GET_META_DATA.toLong())
)
} else {
@Suppress("DEPRECATION")
packageManager.getApplicationInfo(
packageName,
PackageManager.GET_META_DATA
)
}
) as? String
) ?: getString(R.string.sentinel_name)
packageManager.getApplicationLabel(
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
packageManager.getApplicationInfo(
packageName,
PackageManager.ApplicationInfoFlags.of(PackageManager.GET_META_DATA.toLong())
)
} else {
@Suppress("DEPRECATION")
packageManager.getApplicationInfo(
packageName,
PackageManager.GET_META_DATA
)
}
) as? String
) ?: getString(R.string.sentinel_name)
toolbar.setOnMenuItemClickListener {
when (it.itemId) {
R.id.search -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ import android.view.ViewGroup
import androidx.recyclerview.widget.ListAdapter
import com.infinum.sentinel.SentinelFileTree
import com.infinum.sentinel.databinding.SentinelItemLogBinding
import com.infinum.sentinel.ui.shared.Constants.LOG_DATE_TIME_FORMAT
import java.text.SimpleDateFormat
import java.util.Locale

internal class LoggerAdapter(
private val onListChanged: (Boolean) -> Unit,
private val onClick: (SentinelFileTree.Entry) -> Unit
) : ListAdapter<SentinelFileTree.Entry, LoggerViewHolder>(LoggerDiffUtil()) {

private val dateTimeFormat = SimpleDateFormat(LOG_DATE_TIME_FORMAT, Locale.getDefault())

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LoggerViewHolder =
LoggerViewHolder(
SentinelItemLogBinding.inflate(
Expand All @@ -21,7 +26,11 @@ internal class LoggerAdapter(
)

override fun onBindViewHolder(holder: LoggerViewHolder, position: Int) {
holder.bind(getItem(position), onClick)
holder.bind(
item = getItem(position),
dateTimeFormat = dateTimeFormat,
onClick = onClick
)
}

override fun onViewRecycled(holder: LoggerViewHolder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ internal class LoggerViewHolder(
private val binding: SentinelItemLogBinding
) : RecyclerView.ViewHolder(binding.root) {

fun bind(item: SentinelFileTree.Entry?, onClick: (SentinelFileTree.Entry) -> Unit) {
fun bind(
item: SentinelFileTree.Entry?,
dateTimeFormat: SimpleDateFormat,
onClick: (SentinelFileTree.Entry) -> Unit
) {
item?.let { entry ->
with(binding) {
levelView.setBackgroundColor(
Expand All @@ -31,7 +35,7 @@ internal class LoggerViewHolder(
}
)
)
timestampView.text = SimpleDateFormat.getDateTimeInstance().format(Date(entry.timestamp))
timestampView.text = dateTimeFormat.format(Date(entry.timestamp))
tagView.text = entry.tag
entry.stackTrace?.let {
stackTraceView.text = it
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.infinum.sentinel.ui.logger.models

import java.io.File
import java.text.SimpleDateFormat
import java.util.Date
import org.json.JSONObject

internal open class BaseEntry(
Expand All @@ -10,7 +11,6 @@ internal open class BaseEntry(
open val message: String? = null,
open val stackTrace: String? = null
) {

fun asJSONString(): String =
JSONObject()
.put("level", level)
Expand All @@ -20,9 +20,9 @@ internal open class BaseEntry(
.put("stackTrace", stackTrace)
.toString()

fun asLineString(): String =
fun asLineString(dateTimeFormat: SimpleDateFormat): String =
buildString {
append(timestamp)
append(dateTimeFormat.format(Date(timestamp)))
append(" LEVEL: ")
append(level)
append(" TAG: ")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ internal class FlowBuffer<T : BaseEntry> {
} else {
queue.reversed().filter {
it.tag?.lowercase()?.contains(query.lowercase()) == true ||
it.message?.lowercase()?.contains(query.lowercase()) == true ||
it.stackTrace?.lowercase()?.contains(query.lowercase()) == true
it.message?.lowercase()?.contains(query.lowercase()) == true ||
it.stackTrace?.lowercase()?.contains(query.lowercase()) == true
}
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext


public class LogsActivity : AppCompatActivity() {

private companion object {
Expand Down Expand Up @@ -79,21 +78,21 @@ public class LogsActivity : AppCompatActivity() {
with(binding) {
toolbar.setNavigationOnClickListener { finish() }
toolbar.subtitle = (
packageManager.getApplicationLabel(
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
packageManager.getApplicationInfo(
packageName,
PackageManager.ApplicationInfoFlags.of(PackageManager.GET_META_DATA.toLong())
)
} else {
@Suppress("DEPRECATION")
packageManager.getApplicationInfo(
packageName,
PackageManager.GET_META_DATA
)
}
) as? String
) ?: getString(R.string.sentinel_name)
packageManager.getApplicationLabel(
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
packageManager.getApplicationInfo(
packageName,
PackageManager.ApplicationInfoFlags.of(PackageManager.GET_META_DATA.toLong())
)
} else {
@Suppress("DEPRECATION")
packageManager.getApplicationInfo(
packageName,
PackageManager.GET_META_DATA
)
}
) as? String
) ?: getString(R.string.sentinel_name)

recyclerView.layoutManager = LinearLayoutManager(
recyclerView.context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ package com.infinum.sentinel.ui.logs
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.ListAdapter
import com.infinum.sentinel.SentinelFileTree
import com.infinum.sentinel.databinding.SentinelItemLogBinding
import com.infinum.sentinel.databinding.SentinelItemLogFileBinding
import com.infinum.sentinel.ui.shared.Constants.LOG_DATE_TIME_FORMAT
import java.io.File
import java.text.SimpleDateFormat
import java.util.Locale

internal class LogsAdapter(
private val onListChanged: (Boolean) -> Unit,
private val onDelete: (File) -> Unit,
private val onShare: (File) -> Unit
) : ListAdapter<File, LogsViewHolder>(LogsDiffUtil()) {

private val dateTimeFormat = SimpleDateFormat(LOG_DATE_TIME_FORMAT, Locale.getDefault())

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LogsViewHolder =
LogsViewHolder(
SentinelItemLogFileBinding.inflate(
Expand All @@ -24,7 +27,12 @@ internal class LogsAdapter(
)

override fun onBindViewHolder(holder: LogsViewHolder, position: Int) {
holder.bind(getItem(position), onDelete, onShare)
holder.bind(
item = getItem(position),
dateTimeFormat = dateTimeFormat,
onDelete = onDelete,
onShare = onShare
)
}

override fun onViewRecycled(holder: LogsViewHolder) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package com.infinum.sentinel.ui.logs

import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.infinum.sentinel.R
import com.infinum.sentinel.SentinelFileTree
import com.infinum.sentinel.databinding.SentinelItemLogFileBinding
import com.infinum.sentinel.ui.logger.models.Level
import java.io.File
import java.text.SimpleDateFormat
import java.util.Date
Expand All @@ -15,11 +10,11 @@ internal class LogsViewHolder(
private val binding: SentinelItemLogFileBinding
) : RecyclerView.ViewHolder(binding.root) {

fun bind(item: File?, onDelete: (File) -> Unit, onShare: (File) -> Unit) {
fun bind(item: File?, dateTimeFormat: SimpleDateFormat, onDelete: (File) -> Unit, onShare: (File) -> Unit) {
item?.let { entry ->
with(binding) {
messageView.text = entry.name
timestampView.text = SimpleDateFormat.getDateTimeInstance().format(Date(entry.lastModified()))
timestampView.text = dateTimeFormat.format(Date(entry.lastModified()))
deleteButton.setOnClickListener { onDelete(entry) }
shareButton.setOnClickListener { onShare(entry) }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.infinum.sentinel.ui.shared

internal object Constants {
const val LOG_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
}
Loading