Improved logging

This commit is contained in:
Abdelilah El Aissaoui 2022-06-11 12:58:55 +02:00
parent e92a3a5baa
commit d9c4e6f282
4 changed files with 32 additions and 15 deletions

View File

@ -1,6 +1,7 @@
package app.git package app.git
import app.extensions.systemSeparator import app.extensions.systemSeparator
import app.logging.printLog
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow import kotlinx.coroutines.flow.SharedFlow
@ -13,6 +14,8 @@ import java.nio.file.StandardWatchEventKinds.*
import java.nio.file.attribute.BasicFileAttributes import java.nio.file.attribute.BasicFileAttributes
import javax.inject.Inject import javax.inject.Inject
private const val TAG = "FileChangesWatcher"
class FileChangesWatcher @Inject constructor() { class FileChangesWatcher @Inject constructor() {
private val _changesNotifier = MutableSharedFlow<Boolean>() private val _changesNotifier = MutableSharedFlow<Boolean>()
@ -61,11 +64,13 @@ class FileChangesWatcher @Inject constructor() {
val fullPathOfFileChanged = "$pathStr$systemSeparator.git$systemSeparator$fileChanged" val fullPathOfFileChanged = "$pathStr$systemSeparator.git$systemSeparator$fileChanged"
// Ignore COMMIT_EDITMSG changes // Ignore COMMIT_EDITMSG changes
if(isGitMessageFile(pathStr, fullPathOfFileChanged)) if(isGitMessageFile(pathStr, fullPathOfFileChanged)) {
printLog(TAG, "Ignored changes in $fullPathOfFileChanged")
return@withContext return@withContext
} }
}
println("Has git dir changed: $hasGitDirectoryChanged") printLog(TAG, "Has git dir changed: $hasGitDirectoryChanged")
_changesNotifier.emit(hasGitDirectoryChanged) _changesNotifier.emit(hasGitDirectoryChanged)
@ -78,7 +83,7 @@ class FileChangesWatcher @Inject constructor() {
if (eventFile.isDirectory) { if (eventFile.isDirectory) {
val eventPath = eventFile.toPath() val eventPath = eventFile.toPath()
println("New directory $eventFile detected, adding it to watchService") printLog(TAG, "New directory $eventFile detected, adding it to watchService")
val watchKey = val watchKey =
eventPath.register(watchService, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY) eventPath.register(watchService, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY)
keys[watchKey] = eventPath keys[watchKey] = eventPath

View File

@ -2,6 +2,7 @@ package app.git
import app.git.graph.GraphCommitList import app.git.graph.GraphCommitList
import app.git.graph.GraphWalk import app.git.graph.GraphWalk
import app.logging.printLog
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ensureActive import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
@ -12,12 +13,15 @@ import org.eclipse.jgit.lib.Ref
import org.eclipse.jgit.revwalk.RevCommit import org.eclipse.jgit.revwalk.RevCommit
import javax.inject.Inject import javax.inject.Inject
private const val TAG = "LogManager"
class LogManager @Inject constructor() { class LogManager @Inject constructor() {
suspend fun loadLog(git: Git, currentBranch: Ref?, hasUncommitedChanges: Boolean, commitsLimit: Int) = withContext(Dispatchers.IO) { suspend fun loadLog(git: Git, currentBranch: Ref?, hasUncommitedChanges: Boolean, commitsLimit: Int) = withContext(Dispatchers.IO) {
val commitList = GraphCommitList() val commitList = GraphCommitList()
val repositoryState = git.repository.repositoryState val repositoryState = git.repository.repositoryState
println("Repository state ${repositoryState.description}")
printLog(TAG, "Repository state ${repositoryState.description}")
if (currentBranch != null || repositoryState.isRebasing) { // Current branch is null when there is no log (new repo) or rebasing if (currentBranch != null || repositoryState.isRebasing) { // Current branch is null when there is no log (new repo) or rebasing
val logList = git.log().setMaxCount(1).call().toList() val logList = git.log().setMaxCount(1).call().toList()

View File

@ -0,0 +1,5 @@
package app.logging
fun printLog(tag: String, message: String) {
println("$tag - $message")
}

View File

@ -5,6 +5,7 @@ import app.ErrorsManager
import app.credentials.CredentialsState import app.credentials.CredentialsState
import app.credentials.CredentialsStateManager import app.credentials.CredentialsStateManager
import app.git.* import app.git.*
import app.logging.printLog
import app.newErrorNow import app.newErrorNow
import app.ui.SelectedItem import app.ui.SelectedItem
import app.updates.Update import app.updates.Update
@ -24,6 +25,8 @@ import javax.inject.Provider
private const val MIN_TIME_IN_MS_BETWEEN_REFRESHES = 1000L private const val MIN_TIME_IN_MS_BETWEEN_REFRESHES = 1000L
private const val TAG = "TabViewModel"
/** /**
* Contains all the information related to a tab and its subcomponents (smaller composables like the log, branches, * Contains all the information related to a tab and its subcomponents (smaller composables like the log, branches,
* commit changes, etc.). It holds a reference to every view model because this class lives as long as the tab is open (survives * commit changes, etc.). It holds a reference to every view model because this class lives as long as the tab is open (survives
@ -93,7 +96,7 @@ class TabViewModel @Inject constructor(
launch { launch {
tabState.refreshData.collect { refreshType -> tabState.refreshData.collect { refreshType ->
when (refreshType) { when (refreshType) {
RefreshType.NONE -> println("Not refreshing...") RefreshType.NONE -> printLog(TAG, "Not refreshing...")
RefreshType.ALL_DATA -> refreshRepositoryInfo() RefreshType.ALL_DATA -> refreshRepositoryInfo()
RefreshType.REPO_STATE -> refreshRepositoryState() RefreshType.REPO_STATE -> refreshRepositoryState()
RefreshType.ONLY_LOG -> refreshLog() RefreshType.ONLY_LOG -> refreshLog()
@ -148,7 +151,7 @@ class TabViewModel @Inject constructor(
} }
fun openRepository(directory: File) = tabState.safeProcessingWihoutGit { fun openRepository(directory: File) = tabState.safeProcessingWihoutGit {
println("Trying to open repository ${directory.absoluteFile}") printLog(TAG, "Trying to open repository ${directory.absoluteFile}")
_repositorySelectionStatus.value = RepositorySelectionStatus.Opening(directory.absolutePath) _repositorySelectionStatus.value = RepositorySelectionStatus.Opening(directory.absolutePath)
@ -174,7 +177,7 @@ class TabViewModel @Inject constructor(
private suspend fun loadRepositoryState(git: Git) = withContext(Dispatchers.IO) { private suspend fun loadRepositoryState(git: Git) = withContext(Dispatchers.IO) {
val newRepoState = repositoryManager.getRepositoryState(git) val newRepoState = repositoryManager.getRepositoryState(git)
println("Refreshing repository state $newRepoState") printLog(TAG, "Refreshing repository state $newRepoState")
_repositoryState.value = newRepoState _repositoryState.value = newRepoState
onRepositoryStateChanged(newRepoState) onRepositoryStateChanged(newRepoState)
@ -196,7 +199,7 @@ class TabViewModel @Inject constructor(
launch { launch {
fileChangesWatcher.changesNotifier.collect { latestUpdateChangedGitDir -> fileChangesWatcher.changesNotifier.collect { latestUpdateChangedGitDir ->
if (!tabState.operationRunning) { // Only update if there isn't any process running if (!tabState.operationRunning) { // Only update if there isn't any process running
println("Detected changes in the repository's directory") printLog(TAG, "Detected changes in the repository's directory")
if (latestUpdateChangedGitDir) { if (latestUpdateChangedGitDir) {
hasGitDirChanged = true hasGitDirChanged = true
@ -214,11 +217,11 @@ class TabViewModel @Inject constructor(
// operation may be running // operation may be running
if (diffTime > MIN_TIME_IN_MS_BETWEEN_REFRESHES && !hasGitDirChanged) { if (diffTime > MIN_TIME_IN_MS_BETWEEN_REFRESHES && !hasGitDirChanged) {
updateApp(false) updateApp(false)
println("Sync emit with diff time $diffTime") printLog(TAG, "Sync emit with diff time $diffTime")
} else { } else {
asyncJob = async { asyncJob = async {
delay(MIN_TIME_IN_MS_BETWEEN_REFRESHES) delay(MIN_TIME_IN_MS_BETWEEN_REFRESHES)
println("Async emit") printLog(TAG, "Async emit")
if (isActive) if (isActive)
updateApp(hasGitDirChanged) updateApp(hasGitDirChanged)
@ -228,7 +231,7 @@ class TabViewModel @Inject constructor(
lastNotify = currentTimeMillis lastNotify = currentTimeMillis
} else { } else {
println("Ignoring changed occurred during operation running...") printLog(TAG, "Ignoring changed occurred during operation running...")
} }
} }
} }
@ -240,11 +243,11 @@ class TabViewModel @Inject constructor(
suspend fun updateApp(hasGitDirChanged: Boolean) { suspend fun updateApp(hasGitDirChanged: Boolean) {
if (hasGitDirChanged) { if (hasGitDirChanged) {
println("Changes detected in git directory, full refresh") printLog(TAG, "Changes detected in git directory, full refresh")
refreshRepositoryInfo() refreshRepositoryInfo()
} else { } else {
println("Changes detected, partial refresh") printLog(TAG, "Changes detected, partial refresh")
checkUncommitedChanges() checkUncommitedChanges()
} }
@ -255,7 +258,7 @@ class TabViewModel @Inject constructor(
) { git -> ) { git ->
val uncommitedChangesStateChanged = statusViewModel.updateHasUncommitedChanges(git) val uncommitedChangesStateChanged = statusViewModel.updateHasUncommitedChanges(git)
println("Has uncommitedChangesStateChanged $uncommitedChangesStateChanged") printLog(TAG, "Has uncommitedChangesStateChanged $uncommitedChangesStateChanged")
// Update the log only if the uncommitedChanges status has changed or requested // Update the log only if the uncommitedChanges status has changed or requested
if (uncommitedChangesStateChanged || fullUpdateLog) if (uncommitedChangesStateChanged || fullUpdateLog)
@ -303,7 +306,7 @@ class TabViewModel @Inject constructor(
private fun updateDiffEntry() { private fun updateDiffEntry() {
val diffSelected = diffSelected.value val diffSelected = diffSelected.value
println("Update diff entry $diffSelected") printLog(TAG, "Update diff entry $diffSelected")
if (diffSelected != null) { if (diffSelected != null) {
diffViewModel.updateDiff(diffSelected) diffViewModel.updateDiff(diffSelected)