mirror of
https://github.com/fankes/termux-app.git
synced 2025-09-08 11:34:07 +08:00
When `Logger.CURRENT_LOG_LEVEL` set by user is `Logger.LOG_VERBOSE`, then background (not foreground sessions) command output was being logged to logcat, however, if command outputted too much data to logcat, then logcat clients like in Android Studio would crash. Also if a logcat dump is being taken inside termux, then duplicate lines would occur, first one due to of original entry, and second one due to StreamGobbler logging output at verbose level for logcat command. This would be a concern for plugins as well like `RUN_COMMAND` intent or Termux:Tasker, etc if they ran commands with lot of data and user had set log level to verbose. For plugins, TermuxService now supports `com.termux.execute.background_custom_log_level` `String` extra for custom log level. Termux:Tasker, etc will have to be updated with support. For `RUN_COMMAND` intent, the `com.termux.RUN_COMMAND_BACKGROUND_CUSTOM_LOG_LEVEL` `String` extra is now provided to set custom log level for only the command output. Check `TermuxConstants`. So one can pass a custom log level that is `>=` to the log level set it termux settings where (OFF=0, NORMAL=1, DEBUG=2, VERBOSE=3). If you pass `0`, it will completely disable logging. If you pass `1`, logging will only be enabled if log level in termux settings is `NORMAL` or higher. If custom log level is not passed, then old behaviour will remain and log level in termux settings must be `VERBOSE` or higher for logging to be enabled. Note that the log entries will still be logged with priority `Log.VERBOSE` regardless of log level, i.e `logcat` will have `V/`. The entries logcat component has now changed from `StreamGobbler` to `TermuxCommand`. For output at `stdout`, the entry format is `[<pid>-stdout] ...` and for the output at `stderr`, the entry format is `[<pid>-stderr] ...`. The `<pid>` will be process id as an integer that was started by termux. For example: `V/TermuxCommand: [66666-stdout] ...`. While doing this I realize that instead of using `am` command to send messages back to tasker, you can use tasker `Logcat Entry` profile event to listen to messages from termux at both `stdout` and `stderr`. This might be faster than `am` command intent systems or at least possibly more convenient in some use cases. So setup a profile with the `Component` value set to `TermuxCommand` and `Filter` value set to `-E 'TermuxCommand: \[[0-9]+-((stdout)|(stderr))\] message_tag: .*'` and enable the `Grep Filter` toggle so that entry matching is done in native code. Check https://github.com/joaomgcd/TaskerDocumentation/blob/master/en/help/logcat%20info.md for details. Also enable `Enforce Task Order` in profile settings and set collision handling to `Run Both Together` so that if two or more entries are sent quickly, entry task is run for all. Tasker currently (v5.13.16) is not maintaining order of entry tasks despite the setting. Then you can send an intent from tasker via `Run Shell` action with `root` (since `am` command won't work without it on android >=8) or normally in termux from a script, you should be able to receive the entries as `@lc_text` in entry task of tasker `Logcat Entry` profile. The following just passes two `echo` commands to `bash` as a script via `stdin`. If you don't have root, then you can call a wrapper script with `TermuxCommand` function in `Tasker Function` action that sends another `RUN_COMMAND` intent with termux provide `am` command which will work without root. ``` am startservice --user 0 -n com.termux/com.termux.app.RunCommandService -a com.termux.RUN_COMMAND --es com.termux.RUN_COMMAND_PATH '/data/data/com.termux/files/usr/bin/bash' --es com.termux.RUN_COMMAND_STDIN 'echo "message_tag: Sending message from tasker to termux"' --ez com.termux.RUN_COMMAND_BACKGROUND true --es com.termux.RUN_COMMAND_BACKGROUND_CUSTOM_LOG_LEVEL '1' ```
176 lines
5.6 KiB
Java
176 lines
5.6 KiB
Java
package com.termux.shared.data;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
public class DataUtils {
|
|
|
|
public static final int TRANSACTION_SIZE_LIMIT_IN_BYTES = 100 * 1024; // 100KB
|
|
|
|
private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
|
|
|
|
public static String getTruncatedCommandOutput(String text, int maxLength, boolean fromEnd, boolean onNewline, boolean addPrefix) {
|
|
if (text == null) return null;
|
|
|
|
String prefix = "(truncated) ";
|
|
|
|
if (addPrefix)
|
|
maxLength = maxLength - prefix.length();
|
|
|
|
if (maxLength < 0 || text.length() < maxLength) return text;
|
|
|
|
if (fromEnd) {
|
|
text = text.substring(0, maxLength);
|
|
} else {
|
|
int cutOffIndex = text.length() - maxLength;
|
|
|
|
if (onNewline) {
|
|
int nextNewlineIndex = text.indexOf('\n', cutOffIndex);
|
|
if (nextNewlineIndex != -1 && nextNewlineIndex != text.length() - 1) {
|
|
cutOffIndex = nextNewlineIndex + 1;
|
|
}
|
|
}
|
|
text = text.substring(cutOffIndex);
|
|
}
|
|
|
|
if (addPrefix)
|
|
text = prefix + text;
|
|
|
|
return text;
|
|
}
|
|
|
|
/**
|
|
* Replace a sub string in each item of a {@link String[]}.
|
|
*
|
|
* @param array The {@link String[]} to replace in.
|
|
* @param find The sub string to replace.
|
|
* @param replace The sub string to replace with.
|
|
*/
|
|
public static void replaceSubStringsInStringArrayItems(String[] array, String find, String replace) {
|
|
if(array == null || array.length == 0) return;
|
|
|
|
for (int i = 0; i < array.length; i++) {
|
|
array[i] = array[i].replace(find, replace);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get the {@code float} from a {@link String}.
|
|
*
|
|
* @param value The {@link String} value.
|
|
* @param def The default value if failed to read a valid value.
|
|
* @return Returns the {@code float} value after parsing the {@link String} value, otherwise
|
|
* returns default if failed to read a valid value, like in case of an exception.
|
|
*/
|
|
public static float getFloatFromString(String value, float def) {
|
|
if (value == null) return def;
|
|
|
|
try {
|
|
return Float.parseFloat(value);
|
|
}
|
|
catch (Exception e) {
|
|
return def;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get the {@code int} from a {@link String}.
|
|
*
|
|
* @param value The {@link String} value.
|
|
* @param def The default value if failed to read a valid value.
|
|
* @return Returns the {@code int} value after parsing the {@link String} value, otherwise
|
|
* returns default if failed to read a valid value, like in case of an exception.
|
|
*/
|
|
public static int getIntFromString(String value, int def) {
|
|
if (value == null) return def;
|
|
|
|
try {
|
|
return Integer.parseInt(value);
|
|
}
|
|
catch (Exception e) {
|
|
return def;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get the {@code String} from an {@link Integer}.
|
|
*
|
|
* @param value The {@link Integer} value.
|
|
* @param def The default {@link String} value.
|
|
* @return Returns {@code value} if it is not {@code null}, otherwise returns {@code def}.
|
|
*/
|
|
public static String getStringFromInteger(Integer value, String def) {
|
|
return (value == null) ? def : String.valueOf((int) value);
|
|
}
|
|
|
|
/**
|
|
* Get the {@code hex string} from a {@link byte[]}.
|
|
*
|
|
* @param bytes The {@link byte[]} value.
|
|
* @return Returns the {@code hex string} value.
|
|
*/
|
|
public static String bytesToHex(byte[] bytes) {
|
|
char[] hexChars = new char[bytes.length * 2];
|
|
for (int j = 0; j < bytes.length; j++) {
|
|
int v = bytes[j] & 0xFF;
|
|
hexChars[j * 2] = HEX_ARRAY[v >>> 4];
|
|
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
|
|
}
|
|
return new String(hexChars);
|
|
}
|
|
|
|
/**
|
|
* Get an {@code int} from {@link Bundle} that is stored as a {@link String}.
|
|
*
|
|
* @param bundle The {@link Bundle} to get the value from.
|
|
* @param key The key for the value.
|
|
* @param def The default value if failed to read a valid value.
|
|
* @return Returns the {@code int} value after parsing the {@link String} value stored in
|
|
* {@link Bundle}, otherwise returns default if failed to read a valid value,
|
|
* like in case of an exception.
|
|
*/
|
|
public static int getIntStoredAsStringFromBundle(Bundle bundle, String key, int def) {
|
|
if (bundle == null) return def;
|
|
return getIntFromString(bundle.getString(key, Integer.toString(def)), def);
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* If value is not in the range [min, max], set it to either min or max.
|
|
*/
|
|
public static int clamp(int value, int min, int max) {
|
|
return Math.min(Math.max(value, min), max);
|
|
}
|
|
|
|
/**
|
|
* If value is not in the range [min, max], set it to default.
|
|
*/
|
|
public static float rangedOrDefault(float value, float def, float min, float max) {
|
|
if (value < min || value > max)
|
|
return def;
|
|
else
|
|
return value;
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Get the object itself if it is not {@code null}, otherwise default.
|
|
*
|
|
* @param object The {@link Object} to check.
|
|
* @param def The default {@link Object}.
|
|
* @return Returns {@code object} if it is not {@code null}, otherwise returns {@code def}.
|
|
*/
|
|
public static <T> T getDefaultIfNull(@Nullable T object, @Nullable T def) {
|
|
return (object == null) ? def : object;
|
|
}
|
|
|
|
/** Check if a string is null or empty. */
|
|
public static boolean isNullOrEmpty(String string) {
|
|
return string == null || string.isEmpty();
|
|
}
|
|
|
|
}
|