Initial commit
This commit is contained in:
129
log_avaair.sh
Executable file
129
log_avaair.sh
Executable file
@@ -0,0 +1,129 @@
|
||||
#!/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
|
||||
Reference in New Issue
Block a user