From de20be8e9adc3e26a18f863fc205912e8e9e80ec Mon Sep 17 00:00:00 2001 From: Abdelilah El Aissaoui Date: Sat, 23 Mar 2024 16:58:29 +0100 Subject: [PATCH] Added logs section in Settings --- .../kotlin/com/jetpackduba/gitnuro/App.kt | 4 + .../kotlin/com/jetpackduba/gitnuro/Logging.kt | 130 +++++++++++------- .../kotlin/com/jetpackduba/gitnuro/main.kt | 1 - .../ui/dialogs/settings/SettingsDialog.kt | 13 ++ .../gitnuro/viewmodels/SettingsViewModel.kt | 16 +++ 5 files changed, 111 insertions(+), 53 deletions(-) diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/App.kt b/src/main/kotlin/com/jetpackduba/gitnuro/App.kt index af58124..4a91ff8 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/App.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/App.kt @@ -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) { tabsManager.appComponent = this.appComponent + logging.initLogging() initProxySettings() val windowPlacement = appSettings.windowPlacement.toWindowPlacement diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/Logging.kt b/src/main/kotlin/com/jetpackduba/gitnuro/Logging.kt index 0e46d76..9e79142 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/Logging.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/Logging.kt @@ -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" -} diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/main.kt b/src/main/kotlin/com/jetpackduba/gitnuro/main.kt index a3e4f8a..b4f4f6e 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/main.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/main.kt @@ -4,7 +4,6 @@ import com.jetpackduba.gitnuro.managers.ShellManager import com.jetpackduba.gitnuro.preferences.initPreferencesPath fun main(args: Array) { - initLogging() initPreferencesPath() val app = App() diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/ui/dialogs/settings/SettingsDialog.kt b/src/main/kotlin/com/jetpackduba/gitnuro/ui/dialogs/settings/SettingsDialog.kt index 2233f35..61cf7be 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/ui/dialogs/settings/SettingsDialog.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/ui/dialogs/settings/SettingsDialog.kt @@ -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) { diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/viewmodels/SettingsViewModel.kt b/src/main/kotlin/com/jetpackduba/gitnuro/viewmodels/SettingsViewModel.kt index 7171c96..2701139 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/viewmodels/SettingsViewModel.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/viewmodels/SettingsViewModel.kt @@ -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) + } + + } } \ No newline at end of file