Dynamically enable/disable logging #288
This commit is contained in:
parent
98d7237ec3
commit
0427294227
|
@ -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<ILoggingEvent> 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<ILoggingEvent> 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<ILoggingEvent> 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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -108,7 +108,7 @@
|
|||
<string name="miband_prefs_alias">Name/Alias</string>
|
||||
<string name="pref_header_vibration_count">Anzahl der Vibrationen</string>
|
||||
<string name="title_activity_sleepmonitor">Schlafmonitor</string>
|
||||
<string name="pref_write_logfiles">Log-Dateien schreiben (Neustart erforderlich)</string>
|
||||
<string name="pref_write_logfiles">Log-Dateien schreiben</string>
|
||||
<string name="initializing">initialisiere</string>
|
||||
<string name="busy_task_fetch_activity_data">Hole Aktivitätsdaten</string>
|
||||
<string name="sleep_activity_date_range">Von %1$s bis %2$s</string>
|
||||
|
|
|
@ -107,7 +107,7 @@
|
|||
<string name="miband_prefs_alias">Nombre/Apodo</string>
|
||||
<string name="pref_header_vibration_count">Número de vibraciones</string>
|
||||
<string name="title_activity_sleepmonitor">Monitor de sueño</string>
|
||||
<string name="pref_write_logfiles">Guardar logs (requiere reiniciar)</string>
|
||||
<string name="pref_write_logfiles">Guardar logs</string>
|
||||
<string name="initializing">iniciando</string>
|
||||
<string name="busy_task_fetch_activity_data">Recuperando datos de actividad</string>
|
||||
<string name="sleep_activity_date_range">Desde %1$s a %2$s</string>
|
||||
|
|
|
@ -108,7 +108,7 @@
|
|||
<string name="miband_prefs_alias">Nom/Pseudo</string>
|
||||
<string name="pref_header_vibration_count">Nombre de vibrations</string>
|
||||
<string name="title_activity_sleepmonitor">Moniteur de sommeil</string>
|
||||
<string name="pref_write_logfiles">Écrire les fichiers journaux (redémarrage requis)</string>
|
||||
<string name="pref_write_logfiles">Écrire les fichiers journaux</string>
|
||||
<string name="initializing">Initialisation</string>
|
||||
<string name="busy_task_fetch_activity_data">Récupération des données d\'activité</string>
|
||||
<string name="sleep_activity_date_range">De %1$s à %2$s</string>
|
||||
|
|
|
@ -108,7 +108,7 @@
|
|||
<string name="miband_prefs_alias">Nome / Soprannome</string>
|
||||
<string name="pref_header_vibration_count">Numero vibrazioni</string>
|
||||
<string name="title_activity_sleepmonitor">Monitoraggio del sonno</string>
|
||||
<string name="pref_write_logfiles">Salva il log su file (richiede riavvio)</string>
|
||||
<string name="pref_write_logfiles">Salva il log su file</string>
|
||||
<string name="initializing">inizializzazione in corso</string>
|
||||
<string name="busy_task_fetch_activity_data">Recupero dati attività</string>
|
||||
<string name="sleep_activity_date_range">Da %1$s a %2$s</string>
|
||||
|
|
|
@ -108,7 +108,7 @@
|
|||
<string name="miband_prefs_alias">名前/別名</string>
|
||||
<string name="pref_header_vibration_count">バイブレーション回数</string>
|
||||
<string name="title_activity_sleepmonitor">睡眠観測</string>
|
||||
<string name="pref_write_logfiles">ログファイルを出力 (再起動が必要)</string>
|
||||
<string name="pref_write_logfiles">ログファイルを出力</string>
|
||||
<string name="initializing">初期化中</string>
|
||||
<string name="busy_task_fetch_activity_data">活動データを取得中</string>
|
||||
<string name="sleep_activity_date_range">%1$sから%2$sまで</string>
|
||||
|
|
|
@ -105,7 +105,7 @@
|
|||
<string name="miband_prefs_alias">이름/별명</string>
|
||||
<string name="pref_header_vibration_count">진동 횟수</string>
|
||||
<string name="title_activity_sleepmonitor">수면 측정계</string>
|
||||
<string name="pref_write_logfiles">기록 파일 작성 (재시작 필요)</string>
|
||||
<string name="pref_write_logfiles">기록 파일 작성</string>
|
||||
<string name="initializing">초기화 중</string>
|
||||
<string name="busy_task_fetch_activity_data">활동 데이터 가져오는 중</string>
|
||||
<string name="sleep_activity_date_range">%1$s에서 %2$s(으)로</string>
|
||||
|
|
|
@ -105,7 +105,7 @@
|
|||
<string name="miband_prefs_alias">Nazwisko/Pseudonim</string>
|
||||
<string name="pref_header_vibration_count">Liczba wibracji</string>
|
||||
<string name="title_activity_sleepmonitor">Monitor snu</string>
|
||||
<string name="pref_write_logfiles">Zapisuj logi (wymaga restartu)</string>
|
||||
<string name="pref_write_logfiles">Zapisuj logi</string>
|
||||
<string name="initializing">Uruchamianie</string>
|
||||
<string name="busy_task_fetch_activity_data">Pobieranie danych aktywności</string>
|
||||
<string name="sleep_activity_date_range">Od %1$s do %2$s</string>
|
||||
|
|
|
@ -106,7 +106,7 @@
|
|||
<string name="miband_prefs_alias">Имя/псевдоним</string>
|
||||
<string name="pref_header_vibration_count">Количество вибраций</string>
|
||||
<string name="title_activity_sleepmonitor">Анализ сна</string>
|
||||
<string name="pref_write_logfiles">Записывать файлы журнала (нужен перезапуск)</string>
|
||||
<string name="pref_write_logfiles">Записывать файлы журнала</string>
|
||||
<string name="initializing">Инициализация</string>
|
||||
<string name="busy_task_fetch_activity_data">Получение данных активности</string>
|
||||
<string name="sleep_activity_date_range">От %1$s до %2$s</string>
|
||||
|
|
|
@ -102,7 +102,7 @@
|
|||
<string name="pref_header_vibration_count">Titreşim adedi</string>
|
||||
|
||||
<string name="title_activity_sleepmonitor">Uyku Monitörü</string>
|
||||
<string name="pref_write_logfiles">Kütük dosyalarını yaz (yeniden başlatmak gerekir)</string>
|
||||
<string name="pref_write_logfiles">Kütük dosyalarını yaz</string>
|
||||
<string name="initializing">başlatılıyor</string>
|
||||
<string name="busy_task_fetch_activity_data">Aktivite verisi alınıyor</string>
|
||||
|
||||
|
|
|
@ -105,7 +105,7 @@
|
|||
<string name="miband_prefs_alias">Ім\'я/нік</string>
|
||||
<string name="pref_header_vibration_count">Кількість вібрацій</string>
|
||||
<string name="title_activity_sleepmonitor">Аналіз сну</string>
|
||||
<string name="pref_write_logfiles">Записувати файли звіту (потрібен перезапуск)</string>
|
||||
<string name="pref_write_logfiles">Записувати файли звіту</string>
|
||||
<string name="initializing">Ініціалізація…</string>
|
||||
<string name="busy_task_fetch_activity_data">Отримання даних активності</string>
|
||||
<string name="sleep_activity_date_range">Від %1$s до %2$s</string>
|
||||
|
|
|
@ -83,7 +83,7 @@
|
|||
<string name="candidate_item_device_image">Ảnh thiết bị</string>
|
||||
<string name="miband_prefs_alias">Tên/Bí danh</string>
|
||||
<string name="title_activity_sleepmonitor">Trình giám sát giấc ngủ</string>
|
||||
<string name="pref_write_logfiles">Ghi tập tin nhật ký (cần khởi động lại)</string>
|
||||
<string name="pref_write_logfiles">Ghi tập tin nhật ký</string>
|
||||
<string name="initializing">đang khởi chạy</string>
|
||||
<string name="sleep_activity_date_range">Từ %1$s đến %2$s</string>
|
||||
<string name="miband_prefs_wearside">Đeo bên trái hay phải?</string>
|
||||
|
|
|
@ -126,7 +126,7 @@
|
|||
<string name="pref_header_vibration_count">Vibration Count</string>
|
||||
|
||||
<string name="title_activity_sleepmonitor">Sleep Monitor</string>
|
||||
<string name="pref_write_logfiles">Write Log Files (needs restart)</string>
|
||||
<string name="pref_write_logfiles">Write Log Files</string>
|
||||
<string name="initializing">initializing</string>
|
||||
<string name="busy_task_fetch_activity_data">Fetching Activity Data</string>
|
||||
<string name="sleep_activity_date_range">From %1$s to %2$s</string>
|
||||
|
|
Loading…
Reference in New Issue