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() {
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 logDirectory = LoggerUtils.getLogDirectory()
val logDirectory = LoggerUtils.logDirectory
val logs = getLogFiles(logDirectory, amount)
val zipDir = context.getExternalFilesDir("exports")
val zipFile = File(zipDir, constructName())
@ -66,8 +66,7 @@ class MaintenancePlugin @Inject constructor(
//todo replace this with a call on startup of the application, specifically to remove
// unnecessary garbage from the log exports
fun deleteLogs() {
LoggerUtils.getLogDirectory()?.let { logDirectory ->
val logDir = File(logDirectory)
val logDir = File(LoggerUtils.logDirectory)
val files = logDir.listFiles { _: File?, name: String ->
(name.startsWith("AndroidAPS") && name.endsWith(".zip"))
}
@ -81,7 +80,7 @@ class MaintenancePlugin @Inject constructor(
file.delete()
}
}
val exportDir = File(logDirectory, "exports")
val exportDir = File(LoggerUtils.logDirectory, "exports")
if (exportDir.exists()) {
val expFiles = exportDir.listFiles()
for (file in expFiles) {
@ -90,7 +89,6 @@ class MaintenancePlugin @Inject constructor(
exportDir.delete()
}
}
}
/**
* returns a list of log files. The number of returned logs is given via the amount

View file

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