Skip to content

Commit

Permalink
Changed: Use multi-process SharedPrefernces for log level of plugin apps
Browse files Browse the repository at this point in the history
Since termux-app runs in a separate process from other apps, if a user sets log level in termux settings, then it would require exiting the `termux-app` completely since android caches `SharedPrefernces` in memory and only writes to the file on app exit. Now updated value will be instantly written to the file so that plugins can directly read at startup. If plugins are already running, they would need to be restarted since usually log levels are loaded at startup.
  • Loading branch information
agnostic-apollo committed Sep 2, 2021
1 parent d55c100 commit 9f1203f
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private void configureLoggingPreferences(@NonNull Context context) {
if (preferences == null) return;

com.termux.app.fragments.settings.termux.DebuggingPreferencesFragment.
setLogLevelListPreferenceData(logLevelListPreference, context, preferences.getLogLevel());
setLogLevelListPreferenceData(logLevelListPreference, context, preferences.getLogLevel(true));
loggingCategory.addPreference(logLevelListPreference);
}
}
Expand Down Expand Up @@ -76,7 +76,7 @@ public String getString(String key, @Nullable String defValue) {

switch (key) {
case "log_level":
return String.valueOf(mPreferences.getLogLevel());
return String.valueOf(mPreferences.getLogLevel(true));
default:
return null;
}
Expand All @@ -90,7 +90,7 @@ public void putString(String key, @Nullable String value) {
switch (key) {
case "log_level":
if (value != null) {
mPreferences.setLogLevel(mContext, Integer.parseInt(value));
mPreferences.setLogLevel(mContext, Integer.parseInt(value), true);
}
break;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ public class TermuxAPIAppSharedPreferences {

private final Context mContext;
private final SharedPreferences mSharedPreferences;
private final SharedPreferences mMultiProcessSharedPreferences;


private static final String LOG_TAG = "TermuxAPIAppSharedPreferences";

private TermuxAPIAppSharedPreferences(@Nonnull Context context) {
mContext = context;
mSharedPreferences = getPrivateSharedPreferences(mContext);
mMultiProcessSharedPreferences = getPrivateAndMultiProcessSharedPreferences(mContext);
}

/**
Expand Down Expand Up @@ -65,15 +67,23 @@ private static SharedPreferences getPrivateSharedPreferences(Context context) {
return SharedPreferenceUtils.getPrivateSharedPreferences(context, TermuxConstants.TERMUX_API_DEFAULT_PREFERENCES_FILE_BASENAME_WITHOUT_EXTENSION);
}

private static SharedPreferences getPrivateAndMultiProcessSharedPreferences(Context context) {
if (context == null) return null;
return SharedPreferenceUtils.getPrivateAndMultiProcessSharedPreferences(context, TermuxConstants.TERMUX_API_DEFAULT_PREFERENCES_FILE_BASENAME_WITHOUT_EXTENSION);
}


public int getLogLevel() {
return SharedPreferenceUtils.getInt(mSharedPreferences, TERMUX_API_APP.KEY_LOG_LEVEL, Logger.DEFAULT_LOG_LEVEL);

public int getLogLevel(boolean readFromFile) {
if (readFromFile)
return SharedPreferenceUtils.getInt(mMultiProcessSharedPreferences, TERMUX_API_APP.KEY_LOG_LEVEL, Logger.DEFAULT_LOG_LEVEL);
else
return SharedPreferenceUtils.getInt(mSharedPreferences, TERMUX_API_APP.KEY_LOG_LEVEL, Logger.DEFAULT_LOG_LEVEL);
}

public void setLogLevel(Context context, int logLevel) {
public void setLogLevel(Context context, int logLevel, boolean commitToFile) {
logLevel = Logger.setLogLevel(context, logLevel);
SharedPreferenceUtils.setInt(mSharedPreferences, TERMUX_API_APP.KEY_LOG_LEVEL, logLevel, false);
SharedPreferenceUtils.setInt(mSharedPreferences, TERMUX_API_APP.KEY_LOG_LEVEL, logLevel, commitToFile);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ public class TermuxBootAppSharedPreferences {

private final Context mContext;
private final SharedPreferences mSharedPreferences;
private final SharedPreferences mMultiProcessSharedPreferences;


private static final String LOG_TAG = "TermuxBootAppSharedPreferences";

private TermuxBootAppSharedPreferences(@Nonnull Context context) {
mContext = context;
mSharedPreferences = getPrivateSharedPreferences(mContext);
mMultiProcessSharedPreferences = getPrivateAndMultiProcessSharedPreferences(mContext);
}

/**
Expand Down Expand Up @@ -65,15 +67,23 @@ private static SharedPreferences getPrivateSharedPreferences(Context context) {
return SharedPreferenceUtils.getPrivateSharedPreferences(context, TermuxConstants.TERMUX_BOOT_DEFAULT_PREFERENCES_FILE_BASENAME_WITHOUT_EXTENSION);
}

private static SharedPreferences getPrivateAndMultiProcessSharedPreferences(Context context) {
if (context == null) return null;
return SharedPreferenceUtils.getPrivateAndMultiProcessSharedPreferences(context, TermuxConstants.TERMUX_BOOT_DEFAULT_PREFERENCES_FILE_BASENAME_WITHOUT_EXTENSION);
}


public int getLogLevel() {
return SharedPreferenceUtils.getInt(mSharedPreferences, TERMUX_BOOT_APP.KEY_LOG_LEVEL, Logger.DEFAULT_LOG_LEVEL);

public int getLogLevel(boolean readFromFile) {
if (readFromFile)
return SharedPreferenceUtils.getInt(mMultiProcessSharedPreferences, TERMUX_BOOT_APP.KEY_LOG_LEVEL, Logger.DEFAULT_LOG_LEVEL);
else
return SharedPreferenceUtils.getInt(mSharedPreferences, TERMUX_BOOT_APP.KEY_LOG_LEVEL, Logger.DEFAULT_LOG_LEVEL);
}

public void setLogLevel(Context context, int logLevel) {
public void setLogLevel(Context context, int logLevel, boolean commitToFile) {
logLevel = Logger.setLogLevel(context, logLevel);
SharedPreferenceUtils.setInt(mSharedPreferences, TERMUX_BOOT_APP.KEY_LOG_LEVEL, logLevel, false);
SharedPreferenceUtils.setInt(mSharedPreferences, TERMUX_BOOT_APP.KEY_LOG_LEVEL, logLevel, commitToFile);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ public class TermuxStylingAppSharedPreferences {

private final Context mContext;
private final SharedPreferences mSharedPreferences;
private final SharedPreferences mMultiProcessSharedPreferences;


private static final String LOG_TAG = "TermuxStylingAppSharedPreferences";

private TermuxStylingAppSharedPreferences(@Nonnull Context context) {
mContext = context;
mSharedPreferences = getPrivateSharedPreferences(mContext);
mMultiProcessSharedPreferences = getPrivateAndMultiProcessSharedPreferences(mContext);
}

/**
Expand Down Expand Up @@ -65,15 +67,23 @@ private static SharedPreferences getPrivateSharedPreferences(Context context) {
return SharedPreferenceUtils.getPrivateSharedPreferences(context, TermuxConstants.TERMUX_STYLING_DEFAULT_PREFERENCES_FILE_BASENAME_WITHOUT_EXTENSION);
}

private static SharedPreferences getPrivateAndMultiProcessSharedPreferences(Context context) {
if (context == null) return null;
return SharedPreferenceUtils.getPrivateAndMultiProcessSharedPreferences(context, TermuxConstants.TERMUX_STYLING_DEFAULT_PREFERENCES_FILE_BASENAME_WITHOUT_EXTENSION);
}


public int getLogLevel() {
return SharedPreferenceUtils.getInt(mSharedPreferences, TERMUX_STYLING_APP.KEY_LOG_LEVEL, Logger.DEFAULT_LOG_LEVEL);

public int getLogLevel(boolean readFromFile) {
if (readFromFile)
return SharedPreferenceUtils.getInt(mMultiProcessSharedPreferences, TERMUX_STYLING_APP.KEY_LOG_LEVEL, Logger.DEFAULT_LOG_LEVEL);
else
return SharedPreferenceUtils.getInt(mSharedPreferences, TERMUX_STYLING_APP.KEY_LOG_LEVEL, Logger.DEFAULT_LOG_LEVEL);
}

public void setLogLevel(Context context, int logLevel) {
public void setLogLevel(Context context, int logLevel, boolean commitToFile) {
logLevel = Logger.setLogLevel(context, logLevel);
SharedPreferenceUtils.setInt(mSharedPreferences, TERMUX_STYLING_APP.KEY_LOG_LEVEL, logLevel, false);
SharedPreferenceUtils.setInt(mSharedPreferences, TERMUX_STYLING_APP.KEY_LOG_LEVEL, logLevel, commitToFile);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ public class TermuxWidgetAppSharedPreferences {

private final Context mContext;
private final SharedPreferences mSharedPreferences;
private final SharedPreferences mMultiProcessSharedPreferences;


private static final String LOG_TAG = "TermuxWidgetAppSharedPreferences";

private TermuxWidgetAppSharedPreferences(@Nonnull Context context) {
mContext = context;
mSharedPreferences = getPrivateSharedPreferences(mContext);
mMultiProcessSharedPreferences = getPrivateAndMultiProcessSharedPreferences(mContext);
}

/**
Expand Down Expand Up @@ -65,15 +67,23 @@ private static SharedPreferences getPrivateSharedPreferences(Context context) {
return SharedPreferenceUtils.getPrivateSharedPreferences(context, TermuxConstants.TERMUX_WIDGET_DEFAULT_PREFERENCES_FILE_BASENAME_WITHOUT_EXTENSION);
}

private static SharedPreferences getPrivateAndMultiProcessSharedPreferences(Context context) {
if (context == null) return null;
return SharedPreferenceUtils.getPrivateAndMultiProcessSharedPreferences(context, TermuxConstants.TERMUX_WIDGET_DEFAULT_PREFERENCES_FILE_BASENAME_WITHOUT_EXTENSION);
}


public int getLogLevel() {
return SharedPreferenceUtils.getInt(mSharedPreferences, TERMUX_WIDGET_APP.KEY_LOG_LEVEL, Logger.DEFAULT_LOG_LEVEL);

public int getLogLevel(boolean readFromFile) {
if (readFromFile)
return SharedPreferenceUtils.getInt(mMultiProcessSharedPreferences, TERMUX_WIDGET_APP.KEY_LOG_LEVEL, Logger.DEFAULT_LOG_LEVEL);
else
return SharedPreferenceUtils.getInt(mSharedPreferences, TERMUX_WIDGET_APP.KEY_LOG_LEVEL, Logger.DEFAULT_LOG_LEVEL);
}

public void setLogLevel(Context context, int logLevel) {
public void setLogLevel(Context context, int logLevel, boolean commitToFile) {
logLevel = Logger.setLogLevel(context, logLevel);
SharedPreferenceUtils.setInt(mSharedPreferences, TERMUX_WIDGET_APP.KEY_LOG_LEVEL, logLevel, false);
SharedPreferenceUtils.setInt(mSharedPreferences, TERMUX_WIDGET_APP.KEY_LOG_LEVEL, logLevel, commitToFile);
}

}

0 comments on commit 9f1203f

Please sign in to comment.