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

View File

@ -5,63 +5,89 @@ import com.jetpackduba.gitnuro.system.OS
import com.jetpackduba.gitnuro.system.currentOs
import org.apache.log4j.*
import java.io.File
import javax.inject.Inject
import javax.inject.Singleton
fun initLogging() {
val layout = PatternLayout("%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n")
@Singleton
class Logging @Inject constructor() {
fun initLogging() {
val layout = PatternLayout("%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n")
val filePath = when (currentOs) {
OS.LINUX -> linuxLogsPathAppender()
OS.WINDOWS -> windowsLogsPathAppender()
OS.MAC -> macosLogsPath()
OS.UNKNOWN -> defaultLogsPath()
val filePath = logsFile()
val fileAppender = RollingFileAppender(layout, filePath, true)
fileAppender.maximumFileSize = 10 * 1024 * 1024 // 10MB
fileAppender.maxBackupIndex = 5
val consoleAppender = ConsoleAppender(layout)
LogManager.getRootLogger().apply {
addAppender(fileAppender)
addAppender(consoleAppender)
level = Level.INFO
}
}
val fileAppender = RollingFileAppender(layout, filePath, true)
fileAppender.maximumFileSize = 10 * 1024 * 1024 // 10MB
fileAppender.maxBackupIndex = 5
private fun defaultLogsPath(): File {
val homePath = System.getProperty("user.home").orEmpty()
val consoleAppender = ConsoleAppender(layout)
return File("$homePath/gitnuro/")
}
LogManager.getRootLogger().apply {
addAppender(fileAppender)
addAppender(consoleAppender)
level = Level.INFO
private fun macLogsDirectory(): File {
val logsDir = File(System.getProperty("user.home") + "/Library/Logs/")
.openDirectory("com.jetpackduba.Gitnuro")
return logsDir
}
private fun windowsLogsDirectory(): File {
val localAppData = System.getenv("LOCALAPPDATA")
val gitnuroDir = File(localAppData).openDirectory("Gitnuro")
val logsDir = gitnuroDir.openDirectory("logs")
return logsDir
}
private fun linuxLogsDirectory(): File {
// Based on this https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
val homePath = System.getProperty("user.home")
val xdgStateHome = System.getenv("XDG_STATE_HOME")
val safeXdgStateHome = if (xdgStateHome.isNullOrBlank())
"$homePath/.local/state"
else
xdgStateHome
val gitnuroDir = File(safeXdgStateHome).openDirectory("gitnuro")
val logsDir = gitnuroDir.openDirectory("logs")
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
}
}
private fun defaultLogsPath(): String {
val homePath = System.getProperty("user.home").orEmpty()
return "$homePath/gitnuro/gitnuro.logs"
}
private fun macosLogsPath(): String {
val logsDir = File(System.getProperty("user.home") + "/Library/Logs/")
.openDirectory("com.jetpackduba.Gitnuro")
return "${logsDir.absolutePath}/gitnuro.log"
}
private fun windowsLogsPathAppender(): String {
val localAppData = System.getenv("LOCALAPPDATA")
val gitnuroDir = File(localAppData).openDirectory("Gitnuro")
val logsDir = gitnuroDir.openDirectory("logs")
return "${logsDir.absolutePath}/gitnuro.log"
}
private fun linuxLogsPathAppender(): String {
// Based on this https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
val homePath = System.getProperty("user.home")
val xdgStateHome = System.getenv("XDG_STATE_HOME")
val safeXdgStateHome = if (xdgStateHome.isNullOrBlank())
"$homePath/.local/state"
else
xdgStateHome
val gitnuroDir = File(safeXdgStateHome).openDirectory("gitnuro")
val logsDir = gitnuroDir.openDirectory("logs")
return "$logsDir/gitnuro.log"
}

View File

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

View File

@ -52,6 +52,7 @@ val settings = listOf(
SettingsEntry.Section("Tools"),
SettingsEntry.Entry(AppIcons.TERMINAL, "Terminal") { Terminal(it) },
SettingsEntry.Entry(AppIcons.INFO, "Logs") { Logs(it) },
)
@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
private fun Branches(settingsViewModel: SettingsViewModel) {

View File

@ -1,6 +1,9 @@
package com.jetpackduba.gitnuro.viewmodels
import com.jetpackduba.gitnuro.Logging
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.newErrorNow
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 kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import java.awt.Desktop
import javax.inject.Inject
import javax.inject.Singleton
private const val TAG = "SettingsViewModel"
@Singleton
class SettingsViewModel @Inject constructor(
private val appSettings: AppSettings,
private val openFilePickerUseCase: OpenFilePickerUseCase,
private val logging: Logging,
@AppCoroutineScope private val appScope: CoroutineScope,
) {
// Temporary values to detect changed variables
@ -163,4 +170,13 @@ class SettingsViewModel @Inject constructor(
fun openFileDialog(): String? {
return openFilePickerUseCase(PickerType.FILES, null)
}
fun openLogsFolderInFileExplorer() {
try {
Desktop.getDesktop().open(logging.logsDirectory)
} catch (ex: Exception) {
printError(TAG, ex.message.orEmpty(), ex)
}
}
}