Added logs section in Settings

This commit is contained in:
Abdelilah El Aissaoui 2024-03-23 16:58:29 +01:00
parent 88bee8dfcd
commit de20be8e9a
No known key found for this signature in database
GPG Key ID: 7587FC860F594869
5 changed files with 111 additions and 53 deletions

View File

@ -75,6 +75,9 @@ class App {
@Inject @Inject
lateinit var tempFilesManager: TempFilesManager lateinit var tempFilesManager: TempFilesManager
@Inject
lateinit var logging: Logging
init { init {
appComponent.inject(this) appComponent.inject(this)
} }
@ -83,6 +86,7 @@ class App {
fun start(args: Array<String>) { fun start(args: Array<String>) {
tabsManager.appComponent = this.appComponent tabsManager.appComponent = this.appComponent
logging.initLogging()
initProxySettings() initProxySettings()
val windowPlacement = appSettings.windowPlacement.toWindowPlacement val windowPlacement = appSettings.windowPlacement.toWindowPlacement

View File

@ -5,16 +5,15 @@ import com.jetpackduba.gitnuro.system.OS
import com.jetpackduba.gitnuro.system.currentOs import com.jetpackduba.gitnuro.system.currentOs
import org.apache.log4j.* import org.apache.log4j.*
import java.io.File import java.io.File
import javax.inject.Inject
import javax.inject.Singleton
fun initLogging() { @Singleton
class Logging @Inject constructor() {
fun initLogging() {
val layout = PatternLayout("%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n") val layout = PatternLayout("%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n")
val filePath = when (currentOs) { val filePath = logsFile()
OS.LINUX -> linuxLogsPathAppender()
OS.WINDOWS -> windowsLogsPathAppender()
OS.MAC -> macosLogsPath()
OS.UNKNOWN -> defaultLogsPath()
}
val fileAppender = RollingFileAppender(layout, filePath, true) val fileAppender = RollingFileAppender(layout, filePath, true)
fileAppender.maximumFileSize = 10 * 1024 * 1024 // 10MB fileAppender.maximumFileSize = 10 * 1024 * 1024 // 10MB
@ -27,30 +26,31 @@ fun initLogging() {
addAppender(consoleAppender) addAppender(consoleAppender)
level = Level.INFO level = Level.INFO
} }
} }
private fun defaultLogsPath(): String { private fun defaultLogsPath(): File {
val homePath = System.getProperty("user.home").orEmpty() val homePath = System.getProperty("user.home").orEmpty()
return "$homePath/gitnuro/gitnuro.logs" return File("$homePath/gitnuro/")
} }
private fun macosLogsPath(): String { private fun macLogsDirectory(): File {
val logsDir = File(System.getProperty("user.home") + "/Library/Logs/") val logsDir = File(System.getProperty("user.home") + "/Library/Logs/")
.openDirectory("com.jetpackduba.Gitnuro") .openDirectory("com.jetpackduba.Gitnuro")
return "${logsDir.absolutePath}/gitnuro.log" return logsDir
} }
private fun windowsLogsPathAppender(): String { private fun windowsLogsDirectory(): File {
val localAppData = System.getenv("LOCALAPPDATA") val localAppData = System.getenv("LOCALAPPDATA")
val gitnuroDir = File(localAppData).openDirectory("Gitnuro") val gitnuroDir = File(localAppData).openDirectory("Gitnuro")
val logsDir = gitnuroDir.openDirectory("logs") val logsDir = gitnuroDir.openDirectory("logs")
return "${logsDir.absolutePath}/gitnuro.log"
}
private fun linuxLogsPathAppender(): String { return logsDir
}
private fun linuxLogsDirectory(): File {
// Based on this https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html // Based on this https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
val homePath = System.getProperty("user.home") val homePath = System.getProperty("user.home")
val xdgStateHome = System.getenv("XDG_STATE_HOME") val xdgStateHome = System.getenv("XDG_STATE_HOME")
@ -63,5 +63,31 @@ private fun linuxLogsPathAppender(): String {
val gitnuroDir = File(safeXdgStateHome).openDirectory("gitnuro") val gitnuroDir = File(safeXdgStateHome).openDirectory("gitnuro")
val logsDir = gitnuroDir.openDirectory("logs") val logsDir = gitnuroDir.openDirectory("logs")
return "$logsDir/gitnuro.log" return logsDir
}
val logsDirectory by lazy {
val directory = when (currentOs) {
OS.LINUX -> linuxLogsDirectory()
OS.WINDOWS -> windowsLogsDirectory()
OS.MAC -> macLogsDirectory()
OS.UNKNOWN -> defaultLogsPath()
}
if (!directory.exists()) {
if(directory.isFile) {
directory.delete()
}
directory.mkdirs()
}
directory
}
fun logsFile(): String {
val file = File(logsDirectory, "gitnuro.log")
return file.absolutePath
}
} }

View File

@ -4,7 +4,6 @@ import com.jetpackduba.gitnuro.managers.ShellManager
import com.jetpackduba.gitnuro.preferences.initPreferencesPath import com.jetpackduba.gitnuro.preferences.initPreferencesPath
fun main(args: Array<String>) { fun main(args: Array<String>) {
initLogging()
initPreferencesPath() initPreferencesPath()
val app = App() val app = App()

View File

@ -52,6 +52,7 @@ val settings = listOf(
SettingsEntry.Section("Tools"), SettingsEntry.Section("Tools"),
SettingsEntry.Entry(AppIcons.TERMINAL, "Terminal") { Terminal(it) }, SettingsEntry.Entry(AppIcons.TERMINAL, "Terminal") { Terminal(it) },
SettingsEntry.Entry(AppIcons.INFO, "Logs") { Logs(it) },
) )
@Composable @Composable
@ -384,6 +385,18 @@ fun Terminal(settingsViewModel: SettingsViewModel) {
) )
} }
@Composable
fun Logs(settingsViewModel: SettingsViewModel) {
SettingButton(
title = "Logs",
subtitle = "View the logs folder",
buttonText = "Open folder",
onClick = {
settingsViewModel.openLogsFolderInFileExplorer()
}
)
}
@Composable @Composable
private fun Branches(settingsViewModel: SettingsViewModel) { private fun Branches(settingsViewModel: SettingsViewModel) {

View File

@ -1,6 +1,9 @@
package com.jetpackduba.gitnuro.viewmodels package com.jetpackduba.gitnuro.viewmodels
import com.jetpackduba.gitnuro.Logging
import com.jetpackduba.gitnuro.di.qualifiers.AppCoroutineScope import com.jetpackduba.gitnuro.di.qualifiers.AppCoroutineScope
import com.jetpackduba.gitnuro.git.RefreshType
import com.jetpackduba.gitnuro.logging.printError
import com.jetpackduba.gitnuro.managers.Error import com.jetpackduba.gitnuro.managers.Error
import com.jetpackduba.gitnuro.managers.newErrorNow import com.jetpackduba.gitnuro.managers.newErrorNow
import com.jetpackduba.gitnuro.preferences.AppSettings import com.jetpackduba.gitnuro.preferences.AppSettings
@ -10,13 +13,17 @@ import com.jetpackduba.gitnuro.theme.Theme
import com.jetpackduba.gitnuro.ui.dialogs.settings.ProxyType import com.jetpackduba.gitnuro.ui.dialogs.settings.ProxyType
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import java.awt.Desktop
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
private const val TAG = "SettingsViewModel"
@Singleton @Singleton
class SettingsViewModel @Inject constructor( class SettingsViewModel @Inject constructor(
private val appSettings: AppSettings, private val appSettings: AppSettings,
private val openFilePickerUseCase: OpenFilePickerUseCase, private val openFilePickerUseCase: OpenFilePickerUseCase,
private val logging: Logging,
@AppCoroutineScope private val appScope: CoroutineScope, @AppCoroutineScope private val appScope: CoroutineScope,
) { ) {
// Temporary values to detect changed variables // Temporary values to detect changed variables
@ -163,4 +170,13 @@ class SettingsViewModel @Inject constructor(
fun openFileDialog(): String? { fun openFileDialog(): String? {
return openFilePickerUseCase(PickerType.FILES, null) return openFilePickerUseCase(PickerType.FILES, null)
} }
fun openLogsFolderInFileExplorer() {
try {
Desktop.getDesktop().open(logging.logsDirectory)
} catch (ex: Exception) {
printError(TAG, ex.message.orEmpty(), ex)
}
}
} }