Created
November 5, 2025 09:00
-
-
Save rezazoom/88e6ed49641ccc5b8969aabb04a8ff0f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| ################################################ | |
| # Advanced ADB Logcat Monitor for android apps # | |
| # Author: Reza Esmaeili # | |
| ################################################ | |
| APP_NAME="avaair" | |
| PACKAGE_NAME="ir.avaair" # β Change if needed | |
| LOG_DIR="logs" | |
| mkdir -p "$LOG_DIR" | |
| # Colors for nice console output | |
| RED="\033[0;31m" | |
| GREEN="\033[0;32m" | |
| YELLOW="\033[1;33m" | |
| BLUE="\033[0;34m" | |
| NC="\033[0m" # No Color | |
| FILTER="${1:-*:V}" # Default to verbose logs if no argument passed | |
| START_TIME=$(date +%s) | |
| # --- Cleanup on exit --- | |
| cleanup() { | |
| echo -e "\n${YELLOW}π§Ή Cleaning up...${NC}" | |
| pkill -f "adb -s $DEVICE logcat --pid" 2>/dev/null | |
| echo -e "${GREEN}β Done.${NC}" | |
| exit 0 | |
| } | |
| trap cleanup SIGINT SIGTERM | |
| # --- Device selection --- | |
| echo -e "${BLUE}π Checking connected devices...${NC}" | |
| DEVICES=($(adb devices | grep -w "device" | awk '{print $1}')) | |
| if [ ${#DEVICES[@]} -eq 0 ]; then | |
| echo -e "${RED}β No devices found. Connect a device or ensure it's authorized.${NC}" | |
| exit 1 | |
| fi | |
| # Handle unauthorized/offline states | |
| if adb devices | grep -q "unauthorized"; then | |
| echo -e "${RED}β οΈ Device unauthorized. Accept RSA prompt on your device.${NC}" | |
| exit 1 | |
| fi | |
| if adb devices | grep -q "offline"; then | |
| echo -e "${RED}β οΈ Device offline. Reconnect or restart adb server.${NC}" | |
| exit 1 | |
| fi | |
| if [ ${#DEVICES[@]} -gt 1 ]; then | |
| echo -e "${YELLOW}π± Multiple devices found:${NC}" | |
| i=1 | |
| for d in "${DEVICES[@]}"; do | |
| echo " [$i] $d" | |
| ((i++)) | |
| done | |
| read -p "π Select device number: " choice | |
| DEVICE=${DEVICES[$((choice-1))]} | |
| else | |
| DEVICE=${DEVICES[0]} | |
| echo -e "${GREEN}π± Using single device:${NC} $DEVICE" | |
| fi | |
| # --- Helper: choose ps command dynamically --- | |
| detect_ps_cmd() { | |
| if adb -s "$DEVICE" shell ps -A >/dev/null 2>&1; then | |
| echo "ps -A" | |
| else | |
| echo "ps" | |
| fi | |
| } | |
| PS_CMD=$(detect_ps_cmd) | |
| # --- Get PID for app --- | |
| get_pid() { | |
| adb -s "$DEVICE" shell $PS_CMD | grep "$APP_NAME" | awk '{print $2}' | head -n 1 | |
| } | |
| # --- Start logcat session --- | |
| start_logcat() { | |
| local PID=$1 | |
| local LOG_FILE="$LOG_DIR/${APP_NAME}_$(date +'%Y-%m-%d_%H-%M-%S').log" | |
| echo -e "${GREEN}π Logging PID $PID ($APP_NAME) β ${LOG_FILE}${NC}" | |
| echo -e "${BLUE}π Filter:${NC} $FILTER" | |
| echo "----------------------------------------" | |
| adb -s "$DEVICE" logcat --pid=$PID $FILTER | tee -a "$LOG_FILE" | |
| } | |
| # --- Restart app helper (optional) --- | |
| restart_app() { | |
| if [ -n "$PACKAGE_NAME" ]; then | |
| echo -e "${YELLOW}π Restarting app $PACKAGE_NAME...${NC}" | |
| adb -s "$DEVICE" shell am force-stop "$PACKAGE_NAME" | |
| adb -s "$DEVICE" shell monkey -p "$PACKAGE_NAME" 1 >/dev/null 2>&1 | |
| fi | |
| } | |
| # --- Main loop --- | |
| LAST_PID="" | |
| while true; do | |
| PID=$(get_pid) | |
| if [ -z "$PID" ]; then | |
| echo -e "${YELLOW}β οΈ Waiting for process '$APP_NAME' to start...${NC}" | |
| sleep 3 | |
| continue | |
| fi | |
| if [ "$PID" != "$LAST_PID" ]; then | |
| if [ -n "$LAST_PID" ]; then | |
| echo -e "${BLUE}π Process restarted (old: $LAST_PID β new: $PID).${NC}" | |
| fi | |
| LAST_PID=$PID | |
| # Stop previous logcat if running | |
| pkill -f "adb -s $DEVICE logcat --pid=$LAST_PID" 2>/dev/null | |
| # Start new logcat in background | |
| start_logcat "$PID" & | |
| fi | |
| sleep 5 | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment