mirror of
https://github.com/fankes/termux-app.git
synced 2025-09-07 03:05:18 +08:00
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:
@@ -1,7 +1,9 @@
|
||||
package com.termux.app;
|
||||
|
||||
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.settings.preferences.TermuxAppSharedPreferences;
|
||||
import com.termux.shared.logger.Logger;
|
||||
@@ -14,16 +16,19 @@ public class TermuxApplication extends Application {
|
||||
// Set crash handler for the app
|
||||
TermuxCrashUtils.setCrashHandler(this);
|
||||
|
||||
// Set log level for the app
|
||||
setLogLevel();
|
||||
}
|
||||
// Set log config for the app
|
||||
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");
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -6,9 +6,10 @@ import android.os.Looper;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.termux.shared.R;
|
||||
import com.termux.shared.data.DataUtils;
|
||||
import com.termux.shared.termux.TermuxConstants;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
@@ -19,7 +20,7 @@ import java.util.List;
|
||||
|
||||
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_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() {
|
||||
return CURRENT_LOG_LEVEL;
|
||||
}
|
||||
|
@@ -190,7 +190,7 @@ public class StreamGobbler extends Thread {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
String defaultLogTag = Logger.DEFAULT_LOG_TAG;
|
||||
String defaultLogTag = Logger.getDefaultLogTag();
|
||||
boolean loggingEnabled = Logger.shouldEnableLoggingForCustomLogLevel(mLogLevel);
|
||||
if (loggingEnabled)
|
||||
Logger.logVerbose(LOG_TAG, "Using custom log level: " + mLogLevel + ", current log level: " + Logger.getLogLevel());
|
||||
|
Reference in New Issue
Block a user