LoggerUtils -> kt

This commit is contained in:
Milos Kozak 2021-02-15 22:20:25 +01:00
parent aa7de3b8c4
commit e1c7963c98
4 changed files with 45 additions and 48 deletions

View file

@ -1,25 +0,0 @@
package info.nightscout.androidaps.plugins.general.maintenance;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.LoggerContext;
/**
* This class provides serveral methods for log-handling (eg. sending logs as emails).
*/
public class LoggerUtils {
public static String SUFFIX = ".log.zip";
/**
* Returns the directory, in which the logs are stored on the system. This is configured in the
* logback.xml file.
*
* @return
*/
public static String getLogDirectory() {
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
return lc.getProperty("EXT_FILES_DIR");
}
}

View file

@ -0,0 +1,24 @@
package info.nightscout.androidaps.plugins.general.maintenance
import ch.qos.logback.classic.LoggerContext
import org.slf4j.LoggerFactory
/**
* This class provides several methods for log-handling (eg. sending logs as emails).
*/
object LoggerUtils {
var SUFFIX = ".log.zip"
/**
* Returns the directory, in which the logs are stored on the system. This is configured in the
* logback.xml file.
*
* @return
*/
val logDirectory: String
get() {
val lc = LoggerFactory.getILoggerFactory() as LoggerContext
return lc.getProperty("EXT_FILES_DIR")
}
}

View file

@ -51,7 +51,7 @@ class MaintenancePlugin @Inject constructor(
fun sendLogs() { fun sendLogs() {
val recipient = sp.getString(R.string.key_maintenance_logs_email, "logs@androidaps.org") val recipient = sp.getString(R.string.key_maintenance_logs_email, "logs@androidaps.org")
val amount = sp.getInt(R.string.key_maintenance_logs_amount, 2) val amount = sp.getInt(R.string.key_maintenance_logs_amount, 2)
val logDirectory = LoggerUtils.getLogDirectory() val logDirectory = LoggerUtils.logDirectory
val logs = getLogFiles(logDirectory, amount) val logs = getLogFiles(logDirectory, amount)
val zipDir = context.getExternalFilesDir("exports") val zipDir = context.getExternalFilesDir("exports")
val zipFile = File(zipDir, constructName()) val zipFile = File(zipDir, constructName())
@ -66,29 +66,27 @@ class MaintenancePlugin @Inject constructor(
//todo replace this with a call on startup of the application, specifically to remove //todo replace this with a call on startup of the application, specifically to remove
// unnecessary garbage from the log exports // unnecessary garbage from the log exports
fun deleteLogs() { fun deleteLogs() {
LoggerUtils.getLogDirectory()?.let { logDirectory -> val logDir = File(LoggerUtils.logDirectory)
val logDir = File(logDirectory) val files = logDir.listFiles { _: File?, name: String ->
val files = logDir.listFiles { _: File?, name: String -> (name.startsWith("AndroidAPS") && name.endsWith(".zip"))
(name.startsWith("AndroidAPS") && name.endsWith(".zip")) }
Arrays.sort(files) { f1: File, f2: File -> f1.name.compareTo(f2.name) }
var delFiles = listOf(*files)
val amount = sp.getInt(R.string.key_logshipper_amount, 2)
val keepIndex = amount - 1
if (keepIndex < delFiles.size) {
delFiles = delFiles.subList(keepIndex, delFiles.size)
for (file in delFiles) {
file.delete()
} }
Arrays.sort(files) { f1: File, f2: File -> f1.name.compareTo(f2.name) } }
var delFiles = listOf(*files) val exportDir = File(LoggerUtils.logDirectory, "exports")
val amount = sp.getInt(R.string.key_logshipper_amount, 2) if (exportDir.exists()) {
val keepIndex = amount - 1 val expFiles = exportDir.listFiles()
if (keepIndex < delFiles.size) { for (file in expFiles) {
delFiles = delFiles.subList(keepIndex, delFiles.size) file.delete()
for (file in delFiles) {
file.delete()
}
}
val exportDir = File(logDirectory, "exports")
if (exportDir.exists()) {
val expFiles = exportDir.listFiles()
for (file in expFiles) {
file.delete()
}
exportDir.delete()
} }
exportDir.delete()
} }
} }

View file

@ -14,7 +14,7 @@ class BuildHelper @Inject constructor(private val config: Config) {
private var engineeringMode = false private var engineeringMode = false
init { init {
val extFilesDir = LoggerUtils.getLogDirectory() val extFilesDir = LoggerUtils.logDirectory
val engineeringModeSemaphore = File(extFilesDir, "engineering__mode") val engineeringModeSemaphore = File(extFilesDir, "engineering__mode")
engineeringMode = engineeringModeSemaphore.exists() && engineeringModeSemaphore.isFile engineeringMode = engineeringModeSemaphore.exists() && engineeringModeSemaphore.isFile