From 1eb3b48b518cc32f3f2a1a4f7f34e895aa7dd1c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karlo=20=C4=8Ceh?= Date: Sun, 7 Jul 2024 16:31:45 +0200 Subject: [PATCH 1/2] Add date format to exported logs And make them same as in logs list --- .../kotlin/com/infinum/sentinel/SentinelFileTree.kt | 7 +++++-- .../com/infinum/sentinel/ui/logger/LoggerAdapter.kt | 10 +++++++++- .../infinum/sentinel/ui/logger/LoggerViewHolder.kt | 8 ++++++-- .../infinum/sentinel/ui/logger/models/BaseEntry.kt | 8 ++++---- .../com/infinum/sentinel/ui/logs/LogsAdapter.kt | 13 ++++++++++--- .../com/infinum/sentinel/ui/logs/LogsViewHolder.kt | 9 ++------- 6 files changed, 36 insertions(+), 19 deletions(-) diff --git a/tool-timber/src/main/kotlin/com/infinum/sentinel/SentinelFileTree.kt b/tool-timber/src/main/kotlin/com/infinum/sentinel/SentinelFileTree.kt index b09cb45..f507eb3 100644 --- a/tool-timber/src/main/kotlin/com/infinum/sentinel/SentinelFileTree.kt +++ b/tool-timber/src/main/kotlin/com/infinum/sentinel/SentinelFileTree.kt @@ -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 @@ -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( @@ -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) } diff --git a/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/logger/LoggerAdapter.kt b/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/logger/LoggerAdapter.kt index 46d750f..2ca2cd7 100644 --- a/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/logger/LoggerAdapter.kt +++ b/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/logger/LoggerAdapter.kt @@ -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(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( @@ -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) { diff --git a/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/logger/LoggerViewHolder.kt b/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/logger/LoggerViewHolder.kt index 5be2992..ce2361e 100644 --- a/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/logger/LoggerViewHolder.kt +++ b/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/logger/LoggerViewHolder.kt @@ -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( @@ -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 diff --git a/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/logger/models/BaseEntry.kt b/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/logger/models/BaseEntry.kt index 7ec2e2b..9feadbf 100644 --- a/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/logger/models/BaseEntry.kt +++ b/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/logger/models/BaseEntry.kt @@ -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( @@ -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) @@ -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: ") diff --git a/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/logs/LogsAdapter.kt b/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/logs/LogsAdapter.kt index 8913309..d2c0324 100644 --- a/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/logs/LogsAdapter.kt +++ b/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/logs/LogsAdapter.kt @@ -3,10 +3,10 @@ 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, @@ -14,6 +14,8 @@ internal class LogsAdapter( private val onShare: (File) -> Unit ) : ListAdapter(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( @@ -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) { diff --git a/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/logs/LogsViewHolder.kt b/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/logs/LogsViewHolder.kt index 61c2d60..da7df10 100644 --- a/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/logs/LogsViewHolder.kt +++ b/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/logs/LogsViewHolder.kt @@ -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 @@ -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) } } From f95f4db2a120d826be938494bff97d164a43183c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karlo=20=C4=8Ceh?= Date: Mon, 8 Jul 2024 07:43:33 +0200 Subject: [PATCH 2/2] Extract log date time format --- .../com/infinum/sentinel/SentinelFileTree.kt | 3 +- .../com/infinum/sentinel/TimberInitializer.kt | 1 - .../sentinel/ui/logger/LoggerActivity.kt | 31 +++++++++---------- .../sentinel/ui/logger/LoggerAdapter.kt | 3 +- .../sentinel/ui/logger/models/FlowBuffer.kt | 4 +-- .../infinum/sentinel/ui/logs/LogsActivity.kt | 31 +++++++++---------- .../infinum/sentinel/ui/logs/LogsAdapter.kt | 3 +- .../infinum/sentinel/ui/shared/Constants.kt | 5 +++ 8 files changed, 43 insertions(+), 38 deletions(-) create mode 100644 tool-timber/src/main/kotlin/com/infinum/sentinel/ui/shared/Constants.kt diff --git a/tool-timber/src/main/kotlin/com/infinum/sentinel/SentinelFileTree.kt b/tool-timber/src/main/kotlin/com/infinum/sentinel/SentinelFileTree.kt index f507eb3..81a6b52 100644 --- a/tool-timber/src/main/kotlin/com/infinum/sentinel/SentinelFileTree.kt +++ b/tool-timber/src/main/kotlin/com/infinum/sentinel/SentinelFileTree.kt @@ -4,6 +4,7 @@ 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.text.SimpleDateFormat @@ -22,7 +23,7 @@ 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()) + val dateTimeFormat = SimpleDateFormat(LOG_DATE_TIME_FORMAT, Locale.getDefault()) MainScope().launch { withContext(Dispatchers.IO) { diff --git a/tool-timber/src/main/kotlin/com/infinum/sentinel/TimberInitializer.kt b/tool-timber/src/main/kotlin/com/infinum/sentinel/TimberInitializer.kt index 8125b82..804a728 100644 --- a/tool-timber/src/main/kotlin/com/infinum/sentinel/TimberInitializer.kt +++ b/tool-timber/src/main/kotlin/com/infinum/sentinel/TimberInitializer.kt @@ -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> { diff --git a/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/logger/LoggerActivity.kt b/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/logger/LoggerActivity.kt index fc6a2c9..c0235f9 100644 --- a/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/logger/LoggerActivity.kt +++ b/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/logger/LoggerActivity.kt @@ -36,7 +36,6 @@ import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import timber.log.Timber - public class LoggerActivity : AppCompatActivity() { private companion object { @@ -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 -> { diff --git a/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/logger/LoggerAdapter.kt b/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/logger/LoggerAdapter.kt index 2ca2cd7..7393cc2 100644 --- a/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/logger/LoggerAdapter.kt +++ b/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/logger/LoggerAdapter.kt @@ -5,6 +5,7 @@ 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 @@ -13,7 +14,7 @@ internal class LoggerAdapter( private val onClick: (SentinelFileTree.Entry) -> Unit ) : ListAdapter(LoggerDiffUtil()) { - private val dateTimeFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss. SSSZ", Locale.getDefault()) + private val dateTimeFormat = SimpleDateFormat(LOG_DATE_TIME_FORMAT, Locale.getDefault()) override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LoggerViewHolder = LoggerViewHolder( diff --git a/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/logger/models/FlowBuffer.kt b/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/logger/models/FlowBuffer.kt index d51ee6d..19c1af5 100644 --- a/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/logger/models/FlowBuffer.kt +++ b/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/logger/models/FlowBuffer.kt @@ -28,8 +28,8 @@ internal class FlowBuffer { } 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 } } ) diff --git a/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/logs/LogsActivity.kt b/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/logs/LogsActivity.kt index 65a0da0..8f3e1e6 100644 --- a/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/logs/LogsActivity.kt +++ b/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/logs/LogsActivity.kt @@ -29,7 +29,6 @@ import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch import kotlinx.coroutines.withContext - public class LogsActivity : AppCompatActivity() { private companion object { @@ -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, diff --git a/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/logs/LogsAdapter.kt b/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/logs/LogsAdapter.kt index d2c0324..589e520 100644 --- a/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/logs/LogsAdapter.kt +++ b/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/logs/LogsAdapter.kt @@ -4,6 +4,7 @@ import android.view.LayoutInflater import android.view.ViewGroup import androidx.recyclerview.widget.ListAdapter 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 @@ -14,7 +15,7 @@ internal class LogsAdapter( private val onShare: (File) -> Unit ) : ListAdapter(LogsDiffUtil()) { - private val dateTimeFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss. SSSZ", Locale.getDefault()) + private val dateTimeFormat = SimpleDateFormat(LOG_DATE_TIME_FORMAT, Locale.getDefault()) override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LogsViewHolder = LogsViewHolder( diff --git a/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/shared/Constants.kt b/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/shared/Constants.kt new file mode 100644 index 0000000..68127b6 --- /dev/null +++ b/tool-timber/src/main/kotlin/com/infinum/sentinel/ui/shared/Constants.kt @@ -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" +}