这是indexloc提供的服务,不要输入任何密码
Skip to content

Commit 486faf7

Browse files
Fixed: Stdin not being logged for background execution commands
1 parent 6409019 commit 486faf7

File tree

4 files changed

+36
-11
lines changed

4 files changed

+36
-11
lines changed

termux-shared/src/main/java/com/termux/shared/logger/Logger.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class Logger {
2626
public static final int LOG_LEVEL_VERBOSE = 3; // start logging verbose messages
2727

2828
public static final int DEFAULT_LOG_LEVEL = LOG_LEVEL_NORMAL;
29+
public static final int MAX_LOG_LEVEL = LOG_LEVEL_VERBOSE;
2930
private static int CURRENT_LOG_LEVEL = DEFAULT_LOG_LEVEL;
3031

3132
/**
@@ -413,7 +414,7 @@ public static int getLogLevel() {
413414
}
414415

415416
public static int setLogLevel(Context context, int logLevel) {
416-
if (logLevel >= LOG_LEVEL_OFF && logLevel <= LOG_LEVEL_VERBOSE)
417+
if (isLogLevelValid(logLevel))
417418
CURRENT_LOG_LEVEL = logLevel;
418419
else
419420
CURRENT_LOG_LEVEL = DEFAULT_LOG_LEVEL;
@@ -431,4 +432,8 @@ public static String getFullTag(String tag) {
431432
return DEFAULT_LOG_TAG + ":" + tag;
432433
}
433434

435+
public static boolean isLogLevelValid(Integer logLevel) {
436+
return (logLevel != null && logLevel >= LOG_LEVEL_OFF && logLevel <= MAX_LOG_LEVEL);
437+
}
438+
434439
}

termux-shared/src/main/java/com/termux/shared/models/ExecutionCommand.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public synchronized boolean isStateFailed() {
240240
@Override
241241
public String toString() {
242242
if (!hasExecuted())
243-
return getExecutionInputLogString(this, true);
243+
return getExecutionInputLogString(this, true, true);
244244
else {
245245
return getExecutionOutputLogString(this, true, true);
246246
}
@@ -251,9 +251,10 @@ public String toString() {
251251
*
252252
* @param executionCommand The {@link ExecutionCommand} to convert.
253253
* @param ignoreNull Set to {@code true} if non-critical {@code null} values are to be ignored.
254+
* @param logStdin Set to {@code true} if {@link #stdin} should be logged.
254255
* @return Returns the log friendly {@link String}.
255256
*/
256-
public static String getExecutionInputLogString(final ExecutionCommand executionCommand, boolean ignoreNull) {
257+
public static String getExecutionInputLogString(final ExecutionCommand executionCommand, boolean ignoreNull, boolean logStdin) {
257258
if (executionCommand == null) return "null";
258259

259260
StringBuilder logString = new StringBuilder();
@@ -270,8 +271,13 @@ public static String getExecutionInputLogString(final ExecutionCommand execution
270271
logString.append("\n").append(executionCommand.getInBackgroundLogString());
271272
logString.append("\n").append(executionCommand.getIsFailsafeLogString());
272273

273-
if (executionCommand.inBackground && (!ignoreNull || executionCommand.backgroundCustomLogLevel != null))
274-
logString.append("\n").append(executionCommand.getBackgroundCustomLogLevelLogString());
274+
if (executionCommand.inBackground) {
275+
if (logStdin && (!ignoreNull || !DataUtils.isNullOrEmpty(executionCommand.stdin)))
276+
logString.append("\n").append(executionCommand.getStdinLogString());
277+
278+
if (!ignoreNull || executionCommand.backgroundCustomLogLevel != null)
279+
logString.append("\n").append(executionCommand.getBackgroundCustomLogLevelLogString());
280+
}
275281

276282
if (!ignoreNull || executionCommand.sessionAction != null)
277283
logString.append("\n").append(executionCommand.getSessionActionLogString());
@@ -321,7 +327,7 @@ public static String getDetailedLogString(final ExecutionCommand executionComman
321327

322328
StringBuilder logString = new StringBuilder();
323329

324-
logString.append(getExecutionInputLogString(executionCommand, false));
330+
logString.append(getExecutionInputLogString(executionCommand, false, true));
325331
logString.append(getExecutionOutputLogString(executionCommand, false, true));
326332

327333
logString.append("\n").append(executionCommand.getCommandDescriptionLogString());
@@ -356,8 +362,12 @@ public static String getExecutionCommandMarkdownString(final ExecutionCommand ex
356362
markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("inBackground", executionCommand.inBackground, "-"));
357363
markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("isFailsafe", executionCommand.isFailsafe, "-"));
358364

359-
if (executionCommand.inBackground && executionCommand.backgroundCustomLogLevel != null)
360-
markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Background Custom Log Level", executionCommand.backgroundCustomLogLevel, "-"));
365+
if (executionCommand.inBackground) {
366+
if (!DataUtils.isNullOrEmpty(executionCommand.stdin))
367+
markdownString.append("\n").append(MarkdownUtils.getMultiLineMarkdownStringEntry("Stdin", executionCommand.stdin, "-"));
368+
if (executionCommand.backgroundCustomLogLevel != null)
369+
markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Background Custom Log Level", executionCommand.backgroundCustomLogLevel, "-"));
370+
}
361371

362372
markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Session Action", executionCommand.sessionAction, "-"));
363373

@@ -431,6 +441,13 @@ public String getIsFailsafeLogString() {
431441
return "isFailsafe: `" + isFailsafe + "`";
432442
}
433443

444+
public String getStdinLogString() {
445+
if (DataUtils.isNullOrEmpty(stdin))
446+
return "Stdin: -";
447+
else
448+
return Logger.getMultiLineLogStringEntry("Stdin", stdin, "-");
449+
}
450+
434451
public String getBackgroundCustomLogLevelLogString() {
435452
return "Background Custom Log Level: `" + backgroundCustomLogLevel + "`";
436453
}

termux-shared/src/main/java/com/termux/shared/shell/StreamGobbler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public void run() {
194194
int currentLogLevel = Logger.getLogLevel();
195195

196196
int customLogLevel;
197-
if (mLlogLevel != null && mLlogLevel >= Logger.LOG_LEVEL_OFF) {
197+
if (Logger.isLogLevelValid(mLlogLevel)) {
198198
customLogLevel = mLlogLevel;
199199
Logger.logVerbose(LOG_TAG, "Using custom log level: " + customLogLevel + ", current log level: " + currentLogLevel);
200200
} else {

termux-shared/src/main/java/com/termux/shared/shell/TermuxTask.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import androidx.annotation.NonNull;
99

1010
import com.termux.shared.R;
11+
import com.termux.shared.data.DataUtils;
1112
import com.termux.shared.models.ExecutionCommand;
1213
import com.termux.shared.models.ResultData;
1314
import com.termux.shared.models.errors.Errno;
@@ -80,7 +81,9 @@ public static TermuxTask execute(@NonNull final Context context, @NonNull Execut
8081
return null;
8182
}
8283

83-
Logger.logDebug(LOG_TAG, executionCommand.toString());
84+
// No need to log stdin if logging is disabled, like for app internal scripts
85+
int customLogLevel = Logger.isLogLevelValid(executionCommand.backgroundCustomLogLevel) ? executionCommand.backgroundCustomLogLevel: Logger.LOG_LEVEL_VERBOSE;
86+
Logger.logDebug(LOG_TAG, ExecutionCommand.getExecutionInputLogString(executionCommand, true, customLogLevel >= Logger.getLogLevel()));
8487

8588
String taskName = ShellUtils.getExecutableBasename(executionCommand.executable);
8689

@@ -146,7 +149,7 @@ private void executeInner(@NonNull final Context context) throws IllegalThreadSt
146149
STDOUT.start();
147150
STDERR.start();
148151

149-
if (mExecutionCommand.stdin != null && !mExecutionCommand.stdin.isEmpty()) {
152+
if (!DataUtils.isNullOrEmpty(mExecutionCommand.stdin)) {
150153
try {
151154
STDIN.write((mExecutionCommand.stdin + "\n").getBytes(StandardCharsets.UTF_8));
152155
STDIN.flush();

0 commit comments

Comments
 (0)