Changed!: Remove TermuxConstants reference from Logger and set DEFAULT_LOG_TAG at application startup

Plugin apps must do the same
This commit is contained in:
agnostic-apollo
2021-10-26 08:00:45 +05:00
parent 8e506859a6
commit 6ff5572999
3 changed files with 28 additions and 8 deletions

View File

@@ -1,7 +1,9 @@
package com.termux.app; package com.termux.app;
import android.app.Application; import android.app.Application;
import android.content.Context;
import com.termux.shared.termux.TermuxConstants;
import com.termux.shared.termux.crash.TermuxCrashUtils; import com.termux.shared.termux.crash.TermuxCrashUtils;
import com.termux.shared.termux.settings.preferences.TermuxAppSharedPreferences; import com.termux.shared.termux.settings.preferences.TermuxAppSharedPreferences;
import com.termux.shared.logger.Logger; import com.termux.shared.logger.Logger;
@@ -14,16 +16,19 @@ public class TermuxApplication extends Application {
// Set crash handler for the app // Set crash handler for the app
TermuxCrashUtils.setCrashHandler(this); TermuxCrashUtils.setCrashHandler(this);
// Set log level for the app // Set log config for the app
setLogLevel(); setLogConfig(getApplicationContext());
}
private void setLogLevel() {
// Load the log level from shared preferences and set it to the {@link Logger.CURRENT_LOG_LEVEL}
TermuxAppSharedPreferences preferences = TermuxAppSharedPreferences.build(getApplicationContext());
if (preferences == null) return;
preferences.setLogLevel(null, preferences.getLogLevel());
Logger.logDebug("Starting Application"); Logger.logDebug("Starting Application");
} }
public static void setLogConfig(Context context) {
Logger.setDefaultLogTag(TermuxConstants.TERMUX_APP_NAME);
// Load the log level from shared preferences and set it to the {@link Logger.CURRENT_LOG_LEVEL}
TermuxAppSharedPreferences preferences = TermuxAppSharedPreferences.build(context);
if (preferences == null) return;
preferences.setLogLevel(null, preferences.getLogLevel());
}
} }

View File

@@ -6,9 +6,10 @@ import android.os.Looper;
import android.util.Log; import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.NonNull;
import com.termux.shared.R; import com.termux.shared.R;
import com.termux.shared.data.DataUtils; import com.termux.shared.data.DataUtils;
import com.termux.shared.termux.TermuxConstants;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
@@ -19,7 +20,7 @@ import java.util.List;
public class Logger { public class Logger {
public static final String DEFAULT_LOG_TAG = TermuxConstants.TERMUX_APP_NAME; private static String DEFAULT_LOG_TAG = "Logger";
public static final int LOG_LEVEL_OFF = 0; // log nothing public static final int LOG_LEVEL_OFF = 0; // log nothing
public static final int LOG_LEVEL_NORMAL = 1; // start logging error, warn and info messages and stacktraces public static final int LOG_LEVEL_NORMAL = 1; // start logging error, warn and info messages and stacktraces
@@ -419,6 +420,20 @@ public class Logger {
@NonNull
public static String getDefaultLogTag() {
return DEFAULT_LOG_TAG;
}
/**
* IllegalArgumentException will be thrown if tag.length() > 23 for Nougat (7.0) and prior releases.
* https://developer.android.com/reference/android/util/Log#isLoggable(java.lang.String,%20int) */
public static void setDefaultLogTag(@NonNull String defaultLogTag) {
DEFAULT_LOG_TAG = defaultLogTag.length() >= 23 ? defaultLogTag.substring(0, 22) : defaultLogTag;
}
public static int getLogLevel() { public static int getLogLevel() {
return CURRENT_LOG_LEVEL; return CURRENT_LOG_LEVEL;
} }

View File

@@ -190,7 +190,7 @@ public class StreamGobbler extends Thread {
@Override @Override
public void run() { public void run() {
String defaultLogTag = Logger.DEFAULT_LOG_TAG; String defaultLogTag = Logger.getDefaultLogTag();
boolean loggingEnabled = Logger.shouldEnableLoggingForCustomLogLevel(mLogLevel); boolean loggingEnabled = Logger.shouldEnableLoggingForCustomLogLevel(mLogLevel);
if (loggingEnabled) if (loggingEnabled)
Logger.logVerbose(LOG_TAG, "Using custom log level: " + mLogLevel + ", current log level: " + Logger.getLogLevel()); Logger.logVerbose(LOG_TAG, "Using custom log level: " + mLogLevel + ", current log level: " + Logger.getLogLevel());