Skip to content

Commit 7eded38

Browse files
committed
Fix logs
1 parent ce6c6a5 commit 7eded38

9 files changed

Lines changed: 33 additions & 15 deletions

File tree

app/src/main/java/com/ismartcoding/plain/CrashHandler.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import java.time.format.DateTimeFormatter
99

1010
object CrashHandler {
1111
private const val CRASH_FILE_NAME = "crash_report.txt"
12+
private const val CRASH_LOG_NAME = "crash_log.txt"
1213
private val dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
1314

1415
fun install(context: Context) {
@@ -52,6 +53,10 @@ object CrashHandler {
5253
append(sw.toString())
5354
}
5455
File(context.filesDir, CRASH_FILE_NAME).writeText(report)
56+
// Append to the permanent crash history so it ships with the
57+
// user's app-log zip even if the pending report has been
58+
// consumed by the crash dialog already.
59+
File(context.filesDir, CRASH_LOG_NAME).appendText(report + "\n\n")
5560
} catch (_: Exception) {}
5661
}
5762
}

app/src/main/java/com/ismartcoding/plain/MainApp.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,12 @@ class MainApp : Application() {
8383
newImageLoader(context)
8484
}
8585

86-
LogCat.addLogAdapter(DiskLogAdapter(DiskLogFormatStrategy.getInstance(this)))
86+
LogCat.addLogAdapter(
87+
DiskLogAdapter(
88+
DiskLogFormatStrategy.getInstance(this),
89+
minPriority = if (BuildConfig.DEBUG) LogCat.VERBOSE else LogCat.WARN,
90+
),
91+
)
8792

8893
AppEvents.register()
8994
HttpServerManager.warmUp()

app/src/main/java/com/ismartcoding/plain/helpers/AppLogHelper.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,9 @@ object AppLogHelper {
8787
val logFolder = DiskLogFormatStrategy.getLogFolder(context)
8888
val logFolderFile = File(logFolder)
8989
val crashReportFile = File(context.filesDir, "crash_report.txt")
90+
val crashLogFile = File(context.filesDir, "crash_log.txt")
9091

91-
if (!logFolderFile.exists() && !crashReportFile.exists()) {
92+
if (!logFolderFile.exists() && !crashReportFile.exists() && !crashLogFile.exists()) {
9293
DialogHelper.showMessage(Res.string.no_logs_error)
9394
return@coMain
9495
}
@@ -98,6 +99,7 @@ object AppLogHelper {
9899
val sourcePaths = buildList {
99100
if (logFolderFile.exists()) add(logFolder)
100101
if (crashReportFile.exists()) add(crashReportFile.absolutePath)
102+
if (crashLogFile.exists()) add(crashLogFile.absolutePath)
101103
}
102104
val success = withIO {
103105
ZipHelper.zip(sourcePaths, zipFile.absolutePath)

app/src/main/java/com/ismartcoding/plain/ui/models/ChatViewModel.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import com.ismartcoding.plain.chat.data.ChatTargetType
1515
import com.ismartcoding.plain.db.AppDatabase
1616
import com.ismartcoding.plain.db.DChat
1717
import com.ismartcoding.plain.events.ChannelUpdatedEvent
18+
import com.ismartcoding.plain.events.PeerOnlineStatusChangedEvent
1819
import com.ismartcoding.plain.events.PeerUpdatedEvent
1920
import kotlinx.coroutines.Dispatchers
2021
import kotlinx.coroutines.flow.MutableStateFlow
@@ -26,7 +27,6 @@ import kotlinx.coroutines.withContext
2627
data class ChatState(
2728
val target: ChatTarget = ChatTarget("", ChatTargetType.LOCAL),
2829
val toName: String = "",
29-
val onlinePeerIds: Set<String> = emptySet(),
3030
)
3131

3232
class ChatViewModel : ISelectableViewModel<VChat>, ViewModel() {
@@ -39,10 +39,19 @@ class ChatViewModel : ISelectableViewModel<VChat>, ViewModel() {
3939
private val _chatState = MutableStateFlow(ChatState())
4040
val chatState: StateFlow<ChatState> get() = _chatState
4141

42+
private val _onlinePeerIds = MutableStateFlow<Set<String>>(emptySet())
43+
val onlinePeerIds: StateFlow<Set<String>> get() = _onlinePeerIds
44+
4245
init {
4346
viewModelScope.launch {
4447
Channel.sharedFlow.collect { event ->
4548
when (event) {
49+
is PeerOnlineStatusChangedEvent -> {
50+
_onlinePeerIds.update { current ->
51+
if (event.online) current + event.peerId
52+
else current - event.peerId
53+
}
54+
}
4655
is PeerUpdatedEvent -> {
4756
ChatCacheManager.updatePeer(event.peer)
4857
if (_chatState.value.target.type == ChatTargetType.PEER && _chatState.value.target.toId == event.peer.id) {

app/src/main/java/com/ismartcoding/plain/ui/models/ChatViewModelForward.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fun ChatViewModel.resendMessage(messageId: String) {
2727
val state = chatState.value
2828
ChatDbHelper.updateChatItemStatus(item, "pending")
2929
update(item)
30-
ChatSender.resend(item, state.onlinePeerIds)
30+
ChatSender.resend(item, onlinePeerIds.value)
3131
}
3232
}
3333

@@ -48,7 +48,7 @@ fun ChatViewModel.forwardMessage(messageId: String, target: ChatTarget, onResult
4848
val item = ChatDbHelper.getChatItem(messageId) ?: return@launch
4949
val newItem = ChatSender.createChatItem(target, item.content)
5050
val state = chatState.value
51-
ChatSender.send(newItem, target, state.onlinePeerIds)
51+
ChatSender.send(newItem, target, onlinePeerIds.value)
5252
update(newItem)
5353
onResult(newItem.status == "sent")
5454
}

app/src/main/java/com/ismartcoding/plain/ui/models/ChatViewModelMessages.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ fun ChatViewModel.sendMessage(content: DMessageContent, onResult: (Boolean) -> U
3535
addAll(listOf(item))
3636

3737
if (state.target.type != ChatTargetType.LOCAL) {
38-
ChatSender.send(item, state.target, state.onlinePeerIds)
38+
ChatSender.send(item, state.target, onlinePeerIds.value)
3939
update(item)
4040
onResult(item.status == "sent")
4141
} else {
@@ -82,7 +82,7 @@ fun ChatViewModel.updateFilesMessage(messageId: String, files: List<DMessageFile
8282
ChatDbHelper.updateChatItemStatus(item, "sent")
8383
} else {
8484
ChatDbHelper.updateChatItemStatus(item, "pending")
85-
ChatSender.send(item, state.target, state.onlinePeerIds)
85+
ChatSender.send(item, state.target, onlinePeerIds.value)
8686
}
8787
sendEvent(HMessageUpdatedEvent(item.id))
8888
update(item)

lib/src/main/java/com/ismartcoding/lib/logcat/DiskLogAdapter.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package com.ismartcoding.lib.logcat
22

3-
class DiskLogAdapter(private val formatStrategy: FormatStrategy) : LogAdapter {
3+
class DiskLogAdapter(
4+
private val formatStrategy: FormatStrategy,
5+
private val minPriority: Int = LogCat.VERBOSE,
6+
) : LogAdapter {
47
override fun isLoggable(
58
priority: Int,
69
tag: String?,
710
): Boolean {
8-
return true
11+
return priority >= minPriority
912
}
1013

1114
override fun log(

lib/src/main/java/com/ismartcoding/lib/readability4j/Readability4J.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.ismartcoding.lib.readability4j
22

3-
import com.ismartcoding.lib.logcat.LogCat
43
import com.ismartcoding.lib.readability4j.processor.ArticleGrabber
54
import com.ismartcoding.lib.readability4j.processor.MetadataParser
65
import com.ismartcoding.lib.readability4j.processor.Postprocessor
@@ -32,7 +31,6 @@ object Readability4J {
3231
val metadata = metadataParser.getArticleMetadata(document)
3332

3433
val articleContent = articleGrabber.grabArticle(document, metadata)
35-
LogCat.d("Grabbed: $articleContent")
3634

3735
articleContent?.let {
3836
postprocessor.postProcessContent(document, articleContent, uri)

lib/src/main/java/com/ismartcoding/lib/readability4j/processor/ArticleGrabber.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,8 @@ internal class ArticleGrabber(val options: ReadabilityOptions) {
9292
var articleContent = createArticleContent(doc, topCandidate, isPaging)
9393

9494

95-
LogCat.d("Article content pre-prep: ${articleContent.html()}")
9695
// So we have all of the content that we need. Now we clean it up for presentation.
9796
prepArticle(articleContent, options, metadata)
98-
LogCat.d("Article content post-prep: ${articleContent.html()}")
9997

10098
if (neededToCreateTopCandidate) {
10199
// We already created a fake div thing, and there wouldn't have been any siblings left
@@ -117,8 +115,6 @@ internal class ArticleGrabber(val options: ReadabilityOptions) {
117115
articleContent.appendChild(div)
118116
}
119117

120-
LogCat.d("Article content after paging: ${articleContent.html()}")
121-
122118
var parseSuccessful = true
123119
val attempts = ArrayList<Pair<Element, Int>>()
124120

0 commit comments

Comments
 (0)