diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java index f292133a..640ce2db 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java @@ -22,6 +22,8 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.Appender; import nodomain.freeyourgadget.gadgetbridge.database.ActivityDatabaseHandler; import nodomain.freeyourgadget.gadgetbridge.database.DBConstants; import nodomain.freeyourgadget.gadgetbridge.database.DBHandler; @@ -50,6 +52,7 @@ public class GBApplication extends Application { //if preferences have to be migrated, increment the following and add the migration logic in migratePrefs below; see http://stackoverflow.com/questions/16397848/how-can-i-migrate-android-preferences-with-a-new-version private static final int CURRENT_PREFS_VERSION = 2; private static LimitedQueue mIDSenderLookup = new LimitedQueue(16); + private static Appender fileLogger; public static final String ACTION_QUIT = "nodomain.freeyourgadget.gadgetbridge.gbapplication.action.quit"; @@ -86,7 +89,7 @@ public class GBApplication extends Application { // don't do anything here before we set up logging, otherwise // slf4j may be implicitly initialized before we properly configured it. - setupLogging(); + setupLogging(isFileLoggingEnabled()); if (getPrefsFileVersion() != CURRENT_PREFS_VERSION) { migratePrefs(getPrefsFileVersion()); @@ -122,32 +125,68 @@ public class GBApplication extends Application { return sharedPrefs.getBoolean("log_to_file", false); } - private void setupLogging() { - if (isFileLoggingEnabled()) { - try { + public static void setupLogging(boolean enable) { + try { + if (fileLogger == null) { File dir = FileUtils.getExternalFilesDir(); // used by assets/logback.xml since the location cannot be statically determined System.setProperty("GB_LOGFILES_DIR", dir.getAbsolutePath()); - getLogger().info("Gadgetbridge version: " + BuildConfig.VERSION_NAME); - } catch (IOException ex) { - Log.e("GBApplication", "External files dir not available, cannot log to file", ex); - removeFileLogger(); + rememberFileLogger(); } - } else { - removeFileLogger(); + if (enable) { + startFileLogger(); + } else { + stopFileLogger(); + } + getLogger().info("Gadgetbridge version: " + BuildConfig.VERSION_NAME); + } catch (IOException ex) { + Log.e("GBApplication", "External files dir not available, cannot log to file", ex); + stopFileLogger(); } } - private void removeFileLogger() { + private static void startFileLogger() { + if (fileLogger != null && !fileLogger.isStarted()) { + addFileLogger(fileLogger); + fileLogger.start(); + } + } + + private static void stopFileLogger() { + if (fileLogger != null && fileLogger.isStarted()) { + fileLogger.stop(); + removeFileLogger(fileLogger); + } + } + + private static void rememberFileLogger() { + ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); + fileLogger = root.getAppender("FILE"); + } + + private static void addFileLogger(Appender fileLogger) { try { ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); - root.detachAppender("FILE"); + if (!root.isAttached(fileLogger)) { + root.addAppender(fileLogger); + } } catch (Throwable ex) { Log.e("GBApplication", "Error removing logger FILE appender", ex); } } - private Logger getLogger() { + private static void removeFileLogger(Appender fileLogger) { + try { + ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); + if (root.isAttached(fileLogger)) { + root.detachAppender(fileLogger); + } + } catch (Throwable ex) { + Log.e("GBApplication", "Error removing logger FILE appender", ex); + } + } + + private static Logger getLogger() { return LoggerFactory.getLogger(GBApplication.class); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/SettingsActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/SettingsActivity.java index d541af9c..c8bfe835 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/SettingsActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/SettingsActivity.java @@ -12,6 +12,7 @@ import android.widget.Toast; import java.io.IOException; import java.util.List; +import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandPreferencesActivity; import nodomain.freeyourgadget.gadgetbridge.util.FileUtils; @@ -88,16 +89,18 @@ public class SettingsActivity extends AbstractSettingsActivity { pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newVal) { - if (Boolean.TRUE.equals(newVal)) { - try { + boolean doEnable = Boolean.TRUE.equals(newVal); + try { + if (doEnable) { FileUtils.getExternalFilesDir(); // ensures that it is created - } catch (IOException ex) { - GB.toast(getApplicationContext(), - getString(R.string.error_creating_directory_for_logfiles, ex.getLocalizedMessage()), - Toast.LENGTH_LONG, - GB.ERROR, - ex); } + GBApplication.setupLogging(doEnable); + } catch (IOException ex) { + GB.toast(getApplicationContext(), + getString(R.string.error_creating_directory_for_logfiles, ex.getLocalizedMessage()), + Toast.LENGTH_LONG, + GB.ERROR, + ex); } return true; } diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index f9665cef..56557e6d 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -108,7 +108,7 @@ Name/Alias Anzahl der Vibrationen Schlafmonitor - Log-Dateien schreiben (Neustart erforderlich) + Log-Dateien schreiben initialisiere Hole Aktivitätsdaten Von %1$s bis %2$s diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index f4e48054..6dc1e3cc 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -107,7 +107,7 @@ Nombre/Apodo Número de vibraciones Monitor de sueño - Guardar logs (requiere reiniciar) + Guardar logs iniciando Recuperando datos de actividad Desde %1$s a %2$s diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 0d578e24..5df0e5f7 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -108,7 +108,7 @@ Nom/Pseudo Nombre de vibrations Moniteur de sommeil - Écrire les fichiers journaux (redémarrage requis) + Écrire les fichiers journaux Initialisation Récupération des données d\'activité De %1$s à %2$s diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index fa3cd0d7..14f87afb 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -108,7 +108,7 @@ Nome / Soprannome Numero vibrazioni Monitoraggio del sonno - Salva il log su file (richiede riavvio) + Salva il log su file inizializzazione in corso Recupero dati attività Da %1$s a %2$s diff --git a/app/src/main/res/values-ja/strings.xml b/app/src/main/res/values-ja/strings.xml index 6cf3ba01..3dd91611 100644 --- a/app/src/main/res/values-ja/strings.xml +++ b/app/src/main/res/values-ja/strings.xml @@ -108,7 +108,7 @@ 名前/別名 バイブレーション回数 睡眠観測 - ログファイルを出力 (再起動が必要) + ログファイルを出力 初期化中 活動データを取得中 %1$sから%2$sまで diff --git a/app/src/main/res/values-ko/strings.xml b/app/src/main/res/values-ko/strings.xml index 407990cd..e8cf584d 100644 --- a/app/src/main/res/values-ko/strings.xml +++ b/app/src/main/res/values-ko/strings.xml @@ -105,7 +105,7 @@ 이름/별명 진동 횟수 수면 측정계 - 기록 파일 작성 (재시작 필요) + 기록 파일 작성 초기화 중 활동 데이터 가져오는 중 %1$s에서 %2$s(으)로 diff --git a/app/src/main/res/values-pl/strings.xml b/app/src/main/res/values-pl/strings.xml index 1047a982..f1748748 100644 --- a/app/src/main/res/values-pl/strings.xml +++ b/app/src/main/res/values-pl/strings.xml @@ -105,7 +105,7 @@ Nazwisko/Pseudonim Liczba wibracji Monitor snu - Zapisuj logi (wymaga restartu) + Zapisuj logi Uruchamianie Pobieranie danych aktywności Od %1$s do %2$s diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index 6b444aec..1531fe66 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -106,7 +106,7 @@ Имя/псевдоним Количество вибраций Анализ сна - Записывать файлы журнала (нужен перезапуск) + Записывать файлы журнала Инициализация Получение данных активности От %1$s до %2$s diff --git a/app/src/main/res/values-tr/strings.xml b/app/src/main/res/values-tr/strings.xml index 3e58a5bf..58421a74 100644 --- a/app/src/main/res/values-tr/strings.xml +++ b/app/src/main/res/values-tr/strings.xml @@ -102,7 +102,7 @@ Titreşim adedi Uyku Monitörü - Kütük dosyalarını yaz (yeniden başlatmak gerekir) + Kütük dosyalarını yaz başlatılıyor Aktivite verisi alınıyor diff --git a/app/src/main/res/values-uk/strings.xml b/app/src/main/res/values-uk/strings.xml index d0aac7a8..66d1f3f6 100644 --- a/app/src/main/res/values-uk/strings.xml +++ b/app/src/main/res/values-uk/strings.xml @@ -105,7 +105,7 @@ Ім\'я/нік Кількість вібрацій Аналіз сну - Записувати файли звіту (потрібен перезапуск) + Записувати файли звіту Ініціалізація… Отримання даних активності Від %1$s до %2$s diff --git a/app/src/main/res/values-vi/strings.xml b/app/src/main/res/values-vi/strings.xml index 9d059902..af3de63c 100644 --- a/app/src/main/res/values-vi/strings.xml +++ b/app/src/main/res/values-vi/strings.xml @@ -83,7 +83,7 @@ Ảnh thiết bị Tên/Bí danh Trình giám sát giấc ngủ - Ghi tập tin nhật ký (cần khởi động lại) + Ghi tập tin nhật ký đang khởi chạy Từ %1$s đến %2$s Đeo bên trái hay phải? diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5f43f764..1df50a7a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -126,7 +126,7 @@ Vibration Count Sleep Monitor - Write Log Files (needs restart) + Write Log Files initializing Fetching Activity Data From %1$s to %2$s