mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-11-29 05:16:51 +01:00
Correctly disable file logging if initialization fails
If logging initialization failed, the file logger would not be removed correctly, and it would log to a GB_LOGFILES_DIR_IS_UNDEFINED directory. Remove the file logger from the root appender, regardless of it being null or not. The issue can be easily reproduced before the fix by throwing an exception in FileUtils#getExternalFilesDir. Before the fix, it would still log to the aforementioned directory. Aditionally, update the Settings screen to reflect that writing log files is not available in such cases.
This commit is contained in:
parent
7892b8be6a
commit
efdc4c434a
@ -76,13 +76,17 @@ public abstract class Logging {
|
|||||||
protected abstract String createLogDirectory() throws IOException;
|
protected abstract String createLogDirectory() throws IOException;
|
||||||
|
|
||||||
protected void init() throws IOException {
|
protected void init() throws IOException {
|
||||||
|
rememberFileLogger();
|
||||||
String dir = createLogDirectory();
|
String dir = createLogDirectory();
|
||||||
if (dir == null) {
|
if (dir == null) {
|
||||||
throw new IllegalArgumentException("log directory must not be null");
|
throw new IllegalArgumentException("log directory must not be null");
|
||||||
}
|
}
|
||||||
// used by assets/logback.xml since the location cannot be statically determined
|
// used by assets/logback.xml since the location cannot be statically determined
|
||||||
System.setProperty(PROP_LOGFILES_DIR, dir);
|
System.setProperty(PROP_LOGFILES_DIR, dir);
|
||||||
rememberFileLogger();
|
}
|
||||||
|
|
||||||
|
public boolean isInitialized() {
|
||||||
|
return System.getProperty(PROP_LOGFILES_DIR) != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Logger getLogger() {
|
private Logger getLogger() {
|
||||||
@ -100,13 +104,18 @@ public abstract class Logging {
|
|||||||
private void stopFileLogger() {
|
private void stopFileLogger() {
|
||||||
if (fileLogger != null && fileLogger.isStarted()) {
|
if (fileLogger != null && fileLogger.isStarted()) {
|
||||||
fileLogger.stop();
|
fileLogger.stop();
|
||||||
removeFileLogger(fileLogger);
|
|
||||||
}
|
}
|
||||||
|
// We still need to remove the logger from the root appender, otherwise slf4j will log to
|
||||||
|
// a GB_LOGFILES_DIR_IS_UNDEFINED directory (especially if something failed before we got
|
||||||
|
// the fileLogger
|
||||||
|
removeFileLogger();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void rememberFileLogger() {
|
private void rememberFileLogger() {
|
||||||
ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
|
if (fileLogger == null) {
|
||||||
fileLogger = (FileAppender<ILoggingEvent>) root.getAppender("FILE");
|
ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
|
||||||
|
fileLogger = (FileAppender<ILoggingEvent>) root.getAppender("FILE");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addFileLogger(Appender<ILoggingEvent> fileLogger) {
|
private void addFileLogger(Appender<ILoggingEvent> fileLogger) {
|
||||||
@ -120,10 +129,10 @@ public abstract class Logging {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void removeFileLogger(Appender<ILoggingEvent> fileLogger) {
|
private void removeFileLogger() {
|
||||||
try {
|
try {
|
||||||
ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
|
ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
|
||||||
if (root.isAttached(fileLogger)) {
|
if (fileLogger != null && root.isAttached(fileLogger)) {
|
||||||
root.detachAppender(fileLogger);
|
root.detachAppender(fileLogger);
|
||||||
}
|
}
|
||||||
} catch (Throwable ex) {
|
} catch (Throwable ex) {
|
||||||
|
@ -184,7 +184,11 @@ public class SettingsActivity extends AbstractSettingsActivity {
|
|||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// If we didn't manage to initialize file logging, disable the preference
|
||||||
|
if (!GBApplication.getLogging().isInitialized()) {
|
||||||
|
pref.setEnabled(false);
|
||||||
|
pref.setSummary(R.string.pref_write_logfiles_not_available);
|
||||||
|
}
|
||||||
|
|
||||||
pref = findPreference("language");
|
pref = findPreference("language");
|
||||||
pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
|
pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
|
||||||
|
@ -480,6 +480,7 @@
|
|||||||
<string name="watch9_pairing_tap_hint">When your watch vibrates, shake the device or press its button.</string>
|
<string name="watch9_pairing_tap_hint">When your watch vibrates, shake the device or press its button.</string>
|
||||||
<string name="title_activity_sleepmonitor">Sleep monitor</string>
|
<string name="title_activity_sleepmonitor">Sleep monitor</string>
|
||||||
<string name="pref_write_logfiles">Write log files</string>
|
<string name="pref_write_logfiles">Write log files</string>
|
||||||
|
<string name="pref_write_logfiles_not_available">File logging initialization failed, writing log files is not available</string>
|
||||||
<string name="initializing">Initializing</string>
|
<string name="initializing">Initializing</string>
|
||||||
<string name="busy_task_fetch_activity_data">Fetching activity data</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>
|
<string name="sleep_activity_date_range">From %1$s to %2$s</string>
|
||||||
|
Loading…
Reference in New Issue
Block a user