Skip to content

Commit

Permalink
Add date format to exported logs
Browse files Browse the repository at this point in the history
And make them same as in logs list
  • Loading branch information
KCeh committed Jul 7, 2024
1 parent a32fd89 commit 1eb3b48
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import com.infinum.sentinel.ui.logger.models.FlowBuffer
import com.infinum.sentinel.ui.logger.models.Level
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 +22,8 @@ internal class SentinelFileTree(
private val logFileResolver = LogFileResolver(context)

override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
val dateTimeFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss. SSSZ", Locale.getDefault())

MainScope().launch {
withContext(Dispatchers.IO) {
val entry = Entry(
Expand All @@ -34,7 +37,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 @@ -5,12 +5,16 @@ import android.view.ViewGroup
import androidx.recyclerview.widget.ListAdapter
import com.infinum.sentinel.SentinelFileTree
import com.infinum.sentinel.databinding.SentinelItemLogBinding
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("yyyy-MM-dd'T'HH:mm:ss. SSSZ", Locale.getDefault())

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LoggerViewHolder =
LoggerViewHolder(
SentinelItemLogBinding.inflate(
Expand All @@ -21,7 +25,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 @@ -3,17 +3,19 @@ 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 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("yyyy-MM-dd'T'HH:mm:ss. SSSZ", Locale.getDefault())

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LogsViewHolder =
LogsViewHolder(
SentinelItemLogFileBinding.inflate(
Expand All @@ -24,7 +26,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

0 comments on commit 1eb3b48

Please sign in to comment.