Skip to content

Commit e889d84

Browse files
Changed!: Changes introduced to disable/change logging in 60f37bd now also apply to stdin and plugin command results
1 parent 956e20e commit e889d84

File tree

7 files changed

+63
-44
lines changed

7 files changed

+63
-44
lines changed

app/src/main/java/com/termux/app/utils/PluginUtils.java

+15-8
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,12 @@ public static void processPluginExecutionCommandResult(final Context context, St
6565
}
6666

6767
boolean isPluginExecutionCommandWithPendingResult = executionCommand.isPluginExecutionCommandWithPendingResult();
68+
boolean isExecutionCommandLoggingEnabled = Logger.shouldEnableLoggingForCustomLogLevel(executionCommand.backgroundCustomLogLevel);
6869

6970
// Log the output. ResultData should not be logged if pending result since ResultSender will do it
70-
Logger.logDebugExtended(logTag, ExecutionCommand.getExecutionOutputLogString(executionCommand, true, !isPluginExecutionCommandWithPendingResult));
71+
// or if logging is disabled
72+
Logger.logDebugExtended(logTag, ExecutionCommand.getExecutionOutputLogString(executionCommand, true,
73+
!isPluginExecutionCommandWithPendingResult, isExecutionCommandLoggingEnabled));
7174

7275
// If execution command was started by a plugin which expects the result back
7376
if (isPluginExecutionCommandWithPendingResult) {
@@ -78,11 +81,12 @@ public static void processPluginExecutionCommandResult(final Context context, St
7881
setPluginResultDirectoryVariables(executionCommand);
7982

8083
// Send result to caller
81-
error = ResultSender.sendCommandResultData(context, logTag, executionCommand.getCommandIdAndLabelLogString(), executionCommand.resultConfig, executionCommand.resultData);
84+
error = ResultSender.sendCommandResultData(context, logTag, executionCommand.getCommandIdAndLabelLogString(),
85+
executionCommand.resultConfig, executionCommand.resultData, isExecutionCommandLoggingEnabled);
8286
if (error != null) {
8387
// error will be added to existing Errors
8488
resultData.setStateFailed(error);
85-
Logger.logDebugExtended(logTag, ExecutionCommand.getExecutionOutputLogString(executionCommand, true, true));
89+
Logger.logDebugExtended(logTag, ExecutionCommand.getExecutionOutputLogString(executionCommand, true, true, isExecutionCommandLoggingEnabled));
8690

8791
// Flash and send notification for the error
8892
Logger.showToast(context, ResultData.getErrorsListMinimalString(resultData), true);
@@ -133,9 +137,11 @@ public static void processPluginExecutionCommandError(final Context context, Str
133137
}
134138

135139
boolean isPluginExecutionCommandWithPendingResult = executionCommand.isPluginExecutionCommandWithPendingResult();
140+
boolean isExecutionCommandLoggingEnabled = Logger.shouldEnableLoggingForCustomLogLevel(executionCommand.backgroundCustomLogLevel);
136141

137142
// Log the error and any exception. ResultData should not be logged if pending result since ResultSender will do it
138-
Logger.logErrorExtended(logTag, ExecutionCommand.getExecutionOutputLogString(executionCommand, true, !isPluginExecutionCommandWithPendingResult));
143+
Logger.logErrorExtended(logTag, ExecutionCommand.getExecutionOutputLogString(executionCommand, true,
144+
!isPluginExecutionCommandWithPendingResult, isExecutionCommandLoggingEnabled));
139145

140146
// If execution command was started by a plugin which expects the result back
141147
if (isPluginExecutionCommandWithPendingResult) {
@@ -146,11 +152,12 @@ public static void processPluginExecutionCommandError(final Context context, Str
146152
setPluginResultDirectoryVariables(executionCommand);
147153

148154
// Send result to caller
149-
error = ResultSender.sendCommandResultData(context, logTag, executionCommand.getCommandIdAndLabelLogString(), executionCommand.resultConfig, executionCommand.resultData);
155+
error = ResultSender.sendCommandResultData(context, logTag, executionCommand.getCommandIdAndLabelLogString(),
156+
executionCommand.resultConfig, executionCommand.resultData, isExecutionCommandLoggingEnabled);
150157
if (error != null) {
151158
// error will be added to existing Errors
152159
resultData.setStateFailed(error);
153-
Logger.logErrorExtended(logTag, ExecutionCommand.getExecutionOutputLogString(executionCommand, true, true));
160+
Logger.logErrorExtended(logTag, ExecutionCommand.getExecutionOutputLogString(executionCommand, true, true, isExecutionCommandLoggingEnabled));
154161
forceNotification = true;
155162
}
156163

@@ -171,7 +178,7 @@ public static void processPluginExecutionCommandError(final Context context, Str
171178

172179
}
173180

174-
/** Set variables which will be used by {@link ResultSender#sendCommandResultData(Context, String, String, ResultConfig, ResultData)}
181+
/** Set variables which will be used by {@link ResultSender#sendCommandResultData(Context, String, String, ResultConfig, ResultData, boolean)}
175182
* to send back the result via {@link ResultConfig#resultPendingIntent}. */
176183
public static void setPluginResultPendingIntentVariables(ExecutionCommand executionCommand) {
177184
ResultConfig resultConfig = executionCommand.resultConfig;
@@ -186,7 +193,7 @@ public static void setPluginResultPendingIntentVariables(ExecutionCommand execut
186193
resultConfig.resultErrmsgKey = TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE_ERRMSG;
187194
}
188195

189-
/** Set variables which will be used by {@link ResultSender#sendCommandResultData(Context, String, String, ResultConfig, ResultData)}
196+
/** Set variables which will be used by {@link ResultSender#sendCommandResultData(Context, String, String, ResultConfig, ResultData, boolean)}
190197
* to send back the result by writing it to files in {@link ResultConfig#resultDirectoryPath}. */
191198
public static void setPluginResultDirectoryVariables(ExecutionCommand executionCommand) {
192199
ResultConfig resultConfig = executionCommand.resultConfig;

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

+7
Original file line numberDiff line numberDiff line change
@@ -436,4 +436,11 @@ public static boolean isLogLevelValid(Integer logLevel) {
436436
return (logLevel != null && logLevel >= LOG_LEVEL_OFF && logLevel <= MAX_LOG_LEVEL);
437437
}
438438

439+
/** Check if custom log level is valid and >= {@link #CURRENT_LOG_LEVEL}. If custom log level is
440+
* not valid then {@link #LOG_LEVEL_VERBOSE} must be >= {@link #CURRENT_LOG_LEVEL}. */
441+
public static boolean shouldEnableLoggingForCustomLogLevel(Integer customLogLevel) {
442+
customLogLevel = Logger.isLogLevelValid(customLogLevel) ? customLogLevel: Logger.LOG_LEVEL_VERBOSE;
443+
return (customLogLevel >= CURRENT_LOG_LEVEL);
444+
}
445+
439446
}

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

+9-6
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,10 @@ public int getValue() {
8585

8686
/**
8787
* The {@link ExecutionCommand} custom log level for background {@link com.termux.shared.shell.TermuxTask}
88-
* commands. By default, @link com.termux.shared.shell.StreamGobbler} only logs if {@link Logger}
89-
* `CURRENT_LOG_LEVEL` is >= {@link Logger#LOG_LEVEL_VERBOSE}.
88+
* commands. By default, @link com.termux.shared.shell.StreamGobbler} only logs stdout and
89+
* stderr if {@link Logger} `CURRENT_LOG_LEVEL` is >= {@link Logger#LOG_LEVEL_VERBOSE} and
90+
* {@link com.termux.shared.shell.TermuxTask} only logs stdin if `CURRENT_LOG_LEVEL` is >=
91+
* {@link Logger#LOG_LEVEL_DEBUG}.
9092
*/
9193
public Integer backgroundCustomLogLevel;
9294

@@ -242,7 +244,7 @@ public String toString() {
242244
if (!hasExecuted())
243245
return getExecutionInputLogString(this, true, true);
244246
else {
245-
return getExecutionOutputLogString(this, true, true);
247+
return getExecutionOutputLogString(this, true, true, true);
246248
}
247249
}
248250

@@ -298,9 +300,10 @@ public static String getExecutionInputLogString(final ExecutionCommand execution
298300
* @param executionCommand The {@link ExecutionCommand} to convert.
299301
* @param ignoreNull Set to {@code true} if non-critical {@code null} values are to be ignored.
300302
* @param logResultData Set to {@code true} if {@link #resultData} should be logged.
303+
* @param logStdoutAndStderr Set to {@code true} if {@link ResultData#stdout} and {@link ResultData#stderr} should be logged.
301304
* @return Returns the log friendly {@link String}.
302305
*/
303-
public static String getExecutionOutputLogString(final ExecutionCommand executionCommand, boolean ignoreNull, boolean logResultData) {
306+
public static String getExecutionOutputLogString(final ExecutionCommand executionCommand, boolean ignoreNull, boolean logResultData, boolean logStdoutAndStderr) {
304307
if (executionCommand == null) return "null";
305308

306309
StringBuilder logString = new StringBuilder();
@@ -311,7 +314,7 @@ public static String getExecutionOutputLogString(final ExecutionCommand executio
311314
logString.append("\n").append(executionCommand.getCurrentStateLogString());
312315

313316
if (logResultData)
314-
logString.append("\n").append(ResultData.getResultDataLogString(executionCommand.resultData, ignoreNull));
317+
logString.append("\n").append(ResultData.getResultDataLogString(executionCommand.resultData, logStdoutAndStderr));
315318

316319
return logString.toString();
317320
}
@@ -328,7 +331,7 @@ public static String getDetailedLogString(final ExecutionCommand executionComman
328331
StringBuilder logString = new StringBuilder();
329332

330333
logString.append(getExecutionInputLogString(executionCommand, false, true));
331-
logString.append(getExecutionOutputLogString(executionCommand, false, true));
334+
logString.append(getExecutionOutputLogString(executionCommand, false, true, true));
332335

333336
logString.append("\n").append(executionCommand.getCommandDescriptionLogString());
334337
logString.append("\n").append(executionCommand.getCommandHelpLogString());

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,18 @@ public String toString() {
133133
* Get a log friendly {@link String} for {@link ResultData} parameters.
134134
*
135135
* @param resultData The {@link ResultData} to convert.
136-
* @param ignoreNull Set to {@code true} if non-critical {@code null} values are to be ignored.
136+
* @param logStdoutAndStderr Set to {@code true} if {@link #stdout} and {@link #stderr} should be logged.
137137
* @return Returns the log friendly {@link String}.
138138
*/
139-
public static String getResultDataLogString(final ResultData resultData, boolean ignoreNull) {
139+
public static String getResultDataLogString(final ResultData resultData, boolean logStdoutAndStderr) {
140140
if (resultData == null) return "null";
141141

142142
StringBuilder logString = new StringBuilder();
143143

144-
logString.append("\n").append(resultData.getStdoutLogString());
145-
logString.append("\n").append(resultData.getStderrLogString());
144+
if (logStdoutAndStderr) {
145+
logString.append("\n").append(resultData.getStdoutLogString());
146+
logString.append("\n").append(resultData.getStderrLogString());
147+
}
146148
logString.append("\n").append(resultData.getExitCodeLogString());
147149

148150
logString.append("\n\n").append(getErrorsListLogString(resultData));

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

+13-7
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,24 @@ public class ResultSender {
3434
* @param label The label for the command.
3535
* @param resultConfig The {@link ResultConfig} object containing information on how to send the result.
3636
* @param resultData The {@link ResultData} object containing result data.
37+
* @param logStdoutAndStderr Set to {@code true} if {@link ResultData#stdout} and {@link ResultData#stderr}
38+
* should be logged.
3739
* @return Returns the {@link Error} if failed to send the result, otherwise {@code null}.
3840
*/
39-
public static Error sendCommandResultData(Context context, String logTag, String label, ResultConfig resultConfig, ResultData resultData) {
41+
public static Error sendCommandResultData(Context context, String logTag, String label, ResultConfig resultConfig, ResultData resultData, boolean logStdoutAndStderr) {
4042
if (context == null || resultConfig == null || resultData == null)
4143
return FunctionErrno.ERRNO_NULL_OR_EMPTY_PARAMETERS.getError("context, resultConfig or resultData", "sendCommandResultData");
4244

4345
Error error;
4446

4547
if (resultConfig.resultPendingIntent != null) {
46-
error = sendCommandResultDataWithPendingIntent(context, logTag, label, resultConfig, resultData);
48+
error = sendCommandResultDataWithPendingIntent(context, logTag, label, resultConfig, resultData, logStdoutAndStderr);
4749
if (error != null || resultConfig.resultDirectoryPath == null)
4850
return error;
4951
}
5052

5153
if (resultConfig.resultDirectoryPath != null) {
52-
return sendCommandResultDataToDirectory(context, logTag, label, resultConfig, resultData);
54+
return sendCommandResultDataToDirectory(context, logTag, label, resultConfig, resultData, logStdoutAndStderr);
5355
} else {
5456
return FunctionErrno.ERRNO_UNSET_PARAMETERS.getError("resultConfig.resultPendingIntent or resultConfig.resultDirectoryPath", "sendCommandResultData");
5557
}
@@ -63,15 +65,17 @@ public static Error sendCommandResultData(Context context, String logTag, String
6365
* @param label The label for the command.
6466
* @param resultConfig The {@link ResultConfig} object containing information on how to send the result.
6567
* @param resultData The {@link ResultData} object containing result data.
68+
* @param logStdoutAndStderr Set to {@code true} if {@link ResultData#stdout} and {@link ResultData#stderr}
69+
* should be logged.
6670
* @return Returns the {@link Error} if failed to send the result, otherwise {@code null}.
6771
*/
68-
public static Error sendCommandResultDataWithPendingIntent(Context context, String logTag, String label, ResultConfig resultConfig, ResultData resultData) {
72+
public static Error sendCommandResultDataWithPendingIntent(Context context, String logTag, String label, ResultConfig resultConfig, ResultData resultData, boolean logStdoutAndStderr) {
6973
if (context == null || resultConfig == null || resultData == null || resultConfig.resultPendingIntent == null || resultConfig.resultBundleKey == null)
7074
return FunctionErrno.ERRNO_NULL_OR_EMPTY_PARAMETER.getError("context, resultConfig, resultData, resultConfig.resultPendingIntent or resultConfig.resultBundleKey", "sendCommandResultDataWithPendingIntent");
7175

7276
logTag = DataUtils.getDefaultIfNull(logTag, LOG_TAG);
7377

74-
Logger.logDebugExtended(logTag, "Sending result for command \"" + label + "\":\n" + resultConfig.toString() + "\n" + resultData.toString());
78+
Logger.logDebugExtended(logTag, "Sending result for command \"" + label + "\":\n" + resultConfig.toString() + "\n" + ResultData.getResultDataLogString(resultData, logStdoutAndStderr));
7579

7680
String resultDataStdout = resultData.stdout.toString();
7781
String resultDataStderr = resultData.stderr.toString();
@@ -152,9 +156,11 @@ public static Error sendCommandResultDataWithPendingIntent(Context context, Stri
152156
* @param label The label for the command.
153157
* @param resultConfig The {@link ResultConfig} object containing information on how to send the result.
154158
* @param resultData The {@link ResultData} object containing result data.
159+
* @param logStdoutAndStderr Set to {@code true} if {@link ResultData#stdout} and {@link ResultData#stderr}
160+
* should be logged.
155161
* @return Returns the {@link Error} if failed to send the result, otherwise {@code null}.
156162
*/
157-
public static Error sendCommandResultDataToDirectory(Context context, String logTag, String label, ResultConfig resultConfig, ResultData resultData) {
163+
public static Error sendCommandResultDataToDirectory(Context context, String logTag, String label, ResultConfig resultConfig, ResultData resultData, boolean logStdoutAndStderr) {
158164
if (context == null || resultConfig == null || resultData == null || DataUtils.isNullOrEmpty(resultConfig.resultDirectoryPath))
159165
return FunctionErrno.ERRNO_NULL_OR_EMPTY_PARAMETER.getError("context, resultConfig, resultData or resultConfig.resultDirectoryPath", "sendCommandResultDataToDirectory");
160166

@@ -177,7 +183,7 @@ public static Error sendCommandResultDataToDirectory(Context context, String log
177183

178184
resultConfig.resultDirectoryPath = FileUtils.getCanonicalPath(resultConfig.resultDirectoryPath, null);
179185

180-
Logger.logDebugExtended(logTag, "Writing result for command \"" + label + "\":\n" + resultConfig.toString() + "\n" + resultData.toString());
186+
Logger.logDebugExtended(logTag, "Writing result for command \"" + label + "\":\n" + resultConfig.toString() + "\n" + ResultData.getResultDataLogString(resultData, logStdoutAndStderr));
181187

182188
// If resultDirectoryPath is not a directory, or is not readable or writable, then just return
183189
// Creation of missing directory and setting of read, write and execute permissions are

0 commit comments

Comments
 (0)