#!/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