mirror of
https://github.com/fankes/termux-app.git
synced 2025-09-07 03:05:18 +08:00
Fixed: Stdin not being logged for background execution commands
This commit is contained in:
@@ -26,6 +26,7 @@ public class Logger {
|
|||||||
public static final int LOG_LEVEL_VERBOSE = 3; // start logging verbose messages
|
public static final int LOG_LEVEL_VERBOSE = 3; // start logging verbose messages
|
||||||
|
|
||||||
public static final int DEFAULT_LOG_LEVEL = LOG_LEVEL_NORMAL;
|
public static final int DEFAULT_LOG_LEVEL = LOG_LEVEL_NORMAL;
|
||||||
|
public static final int MAX_LOG_LEVEL = LOG_LEVEL_VERBOSE;
|
||||||
private static int CURRENT_LOG_LEVEL = DEFAULT_LOG_LEVEL;
|
private static int CURRENT_LOG_LEVEL = DEFAULT_LOG_LEVEL;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -413,7 +414,7 @@ public class Logger {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static int setLogLevel(Context context, int logLevel) {
|
public static int setLogLevel(Context context, int logLevel) {
|
||||||
if (logLevel >= LOG_LEVEL_OFF && logLevel <= LOG_LEVEL_VERBOSE)
|
if (isLogLevelValid(logLevel))
|
||||||
CURRENT_LOG_LEVEL = logLevel;
|
CURRENT_LOG_LEVEL = logLevel;
|
||||||
else
|
else
|
||||||
CURRENT_LOG_LEVEL = DEFAULT_LOG_LEVEL;
|
CURRENT_LOG_LEVEL = DEFAULT_LOG_LEVEL;
|
||||||
@@ -431,4 +432,8 @@ public class Logger {
|
|||||||
return DEFAULT_LOG_TAG + ":" + tag;
|
return DEFAULT_LOG_TAG + ":" + tag;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean isLogLevelValid(Integer logLevel) {
|
||||||
|
return (logLevel != null && logLevel >= LOG_LEVEL_OFF && logLevel <= MAX_LOG_LEVEL);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -240,7 +240,7 @@ public class ExecutionCommand {
|
|||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
if (!hasExecuted())
|
if (!hasExecuted())
|
||||||
return getExecutionInputLogString(this, true);
|
return getExecutionInputLogString(this, true, true);
|
||||||
else {
|
else {
|
||||||
return getExecutionOutputLogString(this, true, true);
|
return getExecutionOutputLogString(this, true, true);
|
||||||
}
|
}
|
||||||
@@ -251,9 +251,10 @@ public class ExecutionCommand {
|
|||||||
*
|
*
|
||||||
* @param executionCommand The {@link ExecutionCommand} to convert.
|
* @param executionCommand The {@link ExecutionCommand} to convert.
|
||||||
* @param ignoreNull Set to {@code true} if non-critical {@code null} values are to be ignored.
|
* @param ignoreNull Set to {@code true} if non-critical {@code null} values are to be ignored.
|
||||||
|
* @param logStdin Set to {@code true} if {@link #stdin} should be logged.
|
||||||
* @return Returns the log friendly {@link String}.
|
* @return Returns the log friendly {@link String}.
|
||||||
*/
|
*/
|
||||||
public static String getExecutionInputLogString(final ExecutionCommand executionCommand, boolean ignoreNull) {
|
public static String getExecutionInputLogString(final ExecutionCommand executionCommand, boolean ignoreNull, boolean logStdin) {
|
||||||
if (executionCommand == null) return "null";
|
if (executionCommand == null) return "null";
|
||||||
|
|
||||||
StringBuilder logString = new StringBuilder();
|
StringBuilder logString = new StringBuilder();
|
||||||
@@ -270,8 +271,13 @@ public class ExecutionCommand {
|
|||||||
logString.append("\n").append(executionCommand.getInBackgroundLogString());
|
logString.append("\n").append(executionCommand.getInBackgroundLogString());
|
||||||
logString.append("\n").append(executionCommand.getIsFailsafeLogString());
|
logString.append("\n").append(executionCommand.getIsFailsafeLogString());
|
||||||
|
|
||||||
if (executionCommand.inBackground && (!ignoreNull || executionCommand.backgroundCustomLogLevel != null))
|
if (executionCommand.inBackground) {
|
||||||
|
if (logStdin && (!ignoreNull || !DataUtils.isNullOrEmpty(executionCommand.stdin)))
|
||||||
|
logString.append("\n").append(executionCommand.getStdinLogString());
|
||||||
|
|
||||||
|
if (!ignoreNull || executionCommand.backgroundCustomLogLevel != null)
|
||||||
logString.append("\n").append(executionCommand.getBackgroundCustomLogLevelLogString());
|
logString.append("\n").append(executionCommand.getBackgroundCustomLogLevelLogString());
|
||||||
|
}
|
||||||
|
|
||||||
if (!ignoreNull || executionCommand.sessionAction != null)
|
if (!ignoreNull || executionCommand.sessionAction != null)
|
||||||
logString.append("\n").append(executionCommand.getSessionActionLogString());
|
logString.append("\n").append(executionCommand.getSessionActionLogString());
|
||||||
@@ -321,7 +327,7 @@ public class ExecutionCommand {
|
|||||||
|
|
||||||
StringBuilder logString = new StringBuilder();
|
StringBuilder logString = new StringBuilder();
|
||||||
|
|
||||||
logString.append(getExecutionInputLogString(executionCommand, false));
|
logString.append(getExecutionInputLogString(executionCommand, false, true));
|
||||||
logString.append(getExecutionOutputLogString(executionCommand, false, true));
|
logString.append(getExecutionOutputLogString(executionCommand, false, true));
|
||||||
|
|
||||||
logString.append("\n").append(executionCommand.getCommandDescriptionLogString());
|
logString.append("\n").append(executionCommand.getCommandDescriptionLogString());
|
||||||
@@ -356,8 +362,12 @@ public class ExecutionCommand {
|
|||||||
markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("inBackground", executionCommand.inBackground, "-"));
|
markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("inBackground", executionCommand.inBackground, "-"));
|
||||||
markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("isFailsafe", executionCommand.isFailsafe, "-"));
|
markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("isFailsafe", executionCommand.isFailsafe, "-"));
|
||||||
|
|
||||||
if (executionCommand.inBackground && executionCommand.backgroundCustomLogLevel != null)
|
if (executionCommand.inBackground) {
|
||||||
|
if (!DataUtils.isNullOrEmpty(executionCommand.stdin))
|
||||||
|
markdownString.append("\n").append(MarkdownUtils.getMultiLineMarkdownStringEntry("Stdin", executionCommand.stdin, "-"));
|
||||||
|
if (executionCommand.backgroundCustomLogLevel != null)
|
||||||
markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Background Custom Log Level", executionCommand.backgroundCustomLogLevel, "-"));
|
markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Background Custom Log Level", executionCommand.backgroundCustomLogLevel, "-"));
|
||||||
|
}
|
||||||
|
|
||||||
markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Session Action", executionCommand.sessionAction, "-"));
|
markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Session Action", executionCommand.sessionAction, "-"));
|
||||||
|
|
||||||
@@ -431,6 +441,13 @@ public class ExecutionCommand {
|
|||||||
return "isFailsafe: `" + isFailsafe + "`";
|
return "isFailsafe: `" + isFailsafe + "`";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getStdinLogString() {
|
||||||
|
if (DataUtils.isNullOrEmpty(stdin))
|
||||||
|
return "Stdin: -";
|
||||||
|
else
|
||||||
|
return Logger.getMultiLineLogStringEntry("Stdin", stdin, "-");
|
||||||
|
}
|
||||||
|
|
||||||
public String getBackgroundCustomLogLevelLogString() {
|
public String getBackgroundCustomLogLevelLogString() {
|
||||||
return "Background Custom Log Level: `" + backgroundCustomLogLevel + "`";
|
return "Background Custom Log Level: `" + backgroundCustomLogLevel + "`";
|
||||||
}
|
}
|
||||||
|
@@ -194,7 +194,7 @@ public class StreamGobbler extends Thread {
|
|||||||
int currentLogLevel = Logger.getLogLevel();
|
int currentLogLevel = Logger.getLogLevel();
|
||||||
|
|
||||||
int customLogLevel;
|
int customLogLevel;
|
||||||
if (mLlogLevel != null && mLlogLevel >= Logger.LOG_LEVEL_OFF) {
|
if (Logger.isLogLevelValid(mLlogLevel)) {
|
||||||
customLogLevel = mLlogLevel;
|
customLogLevel = mLlogLevel;
|
||||||
Logger.logVerbose(LOG_TAG, "Using custom log level: " + customLogLevel + ", current log level: " + currentLogLevel);
|
Logger.logVerbose(LOG_TAG, "Using custom log level: " + customLogLevel + ", current log level: " + currentLogLevel);
|
||||||
} else {
|
} else {
|
||||||
|
@@ -8,6 +8,7 @@ import android.system.OsConstants;
|
|||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
|
||||||
import com.termux.shared.R;
|
import com.termux.shared.R;
|
||||||
|
import com.termux.shared.data.DataUtils;
|
||||||
import com.termux.shared.models.ExecutionCommand;
|
import com.termux.shared.models.ExecutionCommand;
|
||||||
import com.termux.shared.models.ResultData;
|
import com.termux.shared.models.ResultData;
|
||||||
import com.termux.shared.models.errors.Errno;
|
import com.termux.shared.models.errors.Errno;
|
||||||
@@ -80,7 +81,9 @@ public final class TermuxTask {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger.logDebug(LOG_TAG, executionCommand.toString());
|
// No need to log stdin if logging is disabled, like for app internal scripts
|
||||||
|
int customLogLevel = Logger.isLogLevelValid(executionCommand.backgroundCustomLogLevel) ? executionCommand.backgroundCustomLogLevel: Logger.LOG_LEVEL_VERBOSE;
|
||||||
|
Logger.logDebug(LOG_TAG, ExecutionCommand.getExecutionInputLogString(executionCommand, true, customLogLevel >= Logger.getLogLevel()));
|
||||||
|
|
||||||
String taskName = ShellUtils.getExecutableBasename(executionCommand.executable);
|
String taskName = ShellUtils.getExecutableBasename(executionCommand.executable);
|
||||||
|
|
||||||
@@ -146,7 +149,7 @@ public final class TermuxTask {
|
|||||||
STDOUT.start();
|
STDOUT.start();
|
||||||
STDERR.start();
|
STDERR.start();
|
||||||
|
|
||||||
if (mExecutionCommand.stdin != null && !mExecutionCommand.stdin.isEmpty()) {
|
if (!DataUtils.isNullOrEmpty(mExecutionCommand.stdin)) {
|
||||||
try {
|
try {
|
||||||
STDIN.write((mExecutionCommand.stdin + "\n").getBytes(StandardCharsets.UTF_8));
|
STDIN.write((mExecutionCommand.stdin + "\n").getBytes(StandardCharsets.UTF_8));
|
||||||
STDIN.flush();
|
STDIN.flush();
|
||||||
|
Reference in New Issue
Block a user