parent
fbec92bf24
commit
b76f75f3b0
69
src/main/kotlin/com/jetpackduba/gitnuro/Logging.kt
Normal file
69
src/main/kotlin/com/jetpackduba/gitnuro/Logging.kt
Normal file
@ -0,0 +1,69 @@
|
||||
package com.jetpackduba.gitnuro
|
||||
|
||||
import com.jetpackduba.gitnuro.extensions.openDirectory
|
||||
import com.jetpackduba.gitnuro.system.OS
|
||||
import com.jetpackduba.gitnuro.system.getCurrentOs
|
||||
import org.apache.log4j.ConsoleAppender
|
||||
import org.apache.log4j.LogManager
|
||||
import org.apache.log4j.PatternLayout
|
||||
import org.apache.log4j.RollingFileAppender
|
||||
import java.io.File
|
||||
|
||||
fun initLogging() {
|
||||
val layout = PatternLayout("%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n")
|
||||
|
||||
val filePath = when (getCurrentOs()) {
|
||||
OS.LINUX -> linuxLogsPathAppender()
|
||||
OS.WINDOWS -> windowsLogsPathAppender()
|
||||
OS.MAC -> macosLogsPath()
|
||||
OS.UNKNOWN -> defaultLogsPath()
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
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"
|
||||
}
|
@ -1,6 +1,13 @@
|
||||
package com.jetpackduba.gitnuro
|
||||
|
||||
import com.jetpackduba.gitnuro.preferences.initPreferencesPath
|
||||
|
||||
private const val TAG = "main"
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
initLogging()
|
||||
initPreferencesPath()
|
||||
|
||||
val app = App()
|
||||
app.start(args)
|
||||
}
|
@ -1,6 +1,8 @@
|
||||
package com.jetpackduba.gitnuro.preferences
|
||||
|
||||
import com.jetpackduba.gitnuro.extensions.defaultWindowPlacement
|
||||
import com.jetpackduba.gitnuro.system.OS
|
||||
import com.jetpackduba.gitnuro.system.getCurrentOs
|
||||
import com.jetpackduba.gitnuro.theme.ColorsScheme
|
||||
import com.jetpackduba.gitnuro.theme.Theme
|
||||
import com.jetpackduba.gitnuro.viewmodels.TextDiffType
|
||||
@ -196,3 +198,19 @@ class AppSettings @Inject constructor() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO migrate old prefs path to new one?
|
||||
fun initPreferencesPath() {
|
||||
if(getCurrentOs() == OS.LINUX) {
|
||||
val xdgConfigHome: String? = System.getenv("XDG_CONFIG_HOME")
|
||||
|
||||
val settingsPath = if(xdgConfigHome.isNullOrBlank()) {
|
||||
val home = System.getProperty("user.home").orEmpty()
|
||||
"$home/.config/gitnuro"
|
||||
} else {
|
||||
"$xdgConfigHome/gitnuro"
|
||||
}
|
||||
|
||||
System.setProperty("java.util.prefs.userRoot", settingsPath)
|
||||
}
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
# Root logger option
|
||||
log4j.rootLogger=INFO, file, stdout
|
||||
# Direct log messages to a log file
|
||||
log4j.appender.file=org.apache.log4j.RollingFileAppender
|
||||
log4j.appender.file.File=${user.home}/.gitnuro/gitnuro.log
|
||||
log4j.appender.file.MaxFileSize=10MB
|
||||
log4j.appender.file.MaxBackupIndex=10
|
||||
log4j.appender.file.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
|
||||
# Direct log messages to stdout
|
||||
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.stdout.Target=System.out
|
||||
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
|
Loading…
Reference in New Issue
Block a user