Skip to content

Instantly share code, notes, and snippets.

@scue
Created May 10, 2015 06:39
Show Gist options
  • Save scue/fbbfdf9e09fe805ff041 to your computer and use it in GitHub Desktop.
Save scue/fbbfdf9e09fe805ff041 to your computer and use it in GitHub Desktop.
Cat all android logs
#!/system/bin/sh
#===============================================================================
#
# FILE: logcat_all.sh
#
# USAGE: ./logcat_all.sh
#
# DESCRIPTION: DEBUG版时,打印所有日志(包括时间)到指定文件,方便后期排查问题
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: lwq (28120), scue@vip.qq.com
# ORGANIZATION:
# CREATED: 04/08/2015 04:57:14 PM CST
# REVISION: ---
#===============================================================================
debuggable=$(getprop ro.debuggable)
if [[ $debuggable != 1 ]]; then
log -t logcat_all "system not debuggable, not start logcat on startup"
exit
fi
timestamp=$(/system/bin/date +%F_%H-%M-%S)
logcat_pdir=/sdcard/.logcat
logcat_dir=/sdcard/.logcat/$timestamp
logcat_file=$logcat_dir/logcat_${timestamp}.log
kmsg_logfile=$logcat_dir/kmsg_${timestamp}.log
top_logfile=$logcat_dir/top_${timestamp}.log
vmstat_logfile=$logcat_dir/vmstat_${timestamp}.log
mkdir -p $logcat_dir
# 仅保留10个日志文件
busybox test -d $logcat_dir && {
busybox rm -rf $(busybox ls -1t $logcat_pdir | busybox tail -n +11)
busybox rm -rf $(busybox ls -1t $logcat_pdir | busybox tail -n +11)
}
# for logcat
{
log -t logcat_all "start logcat to file: $logcat_file"
/system/bin/logcat -v threadtime -f $logcat_file
} &
# for kmsg
{
log -t logcat_all "start kmsg to file: $kmsg_logfile"
cat /proc/kmsg >$kmsg_logfile
} &
# for top
{
while busybox true; do
top_newtime=$(/system/bin/date +%F_%H-%M-%S)
top_logfile=$logcat_dir/top_${top_newtime}.log && > $top_logfile
log -t logcat_all "start top to file: $top_logfile"
# 仅保留5个top日志文件
# 5*100*3=25分钟内的Top信息,避免Top日志文件过大
busybox rm -f $(busybox ls -1t ${logcat_dir}/top_* | busybox tail -n +6)
for n in $(busybox seq 1 100) ; do
# 每3秒打印一次top信息,并加入时间显示
top -m 5 -d 3 -t -n 1 | busybox awk '{now=strftime("%Y-%M-%d %T "); print now $0}' >>$top_logfile
echo >>$top_logfile
done
done
} &
# for vmstat
{
while busybox true; do
log -t logcat_all "start vmstat_ to file: $vmstat_logfile"
vmstat_newtime=$(/system/bin/date +%F_%H-%M-%S)
vmstat_logfile=$logcat_dir/vmstat_${vmstat_newtime}.log && > $vmstat_logfile
busybox rm -f $(busybox ls -1t ${logcat_dir}/vmstat_* | busybox tail -n +6)
/system/bin/vmstat -d 3 -n 100 | busybox awk '{now=strftime("%Y-%M-%d %T "); print now $0}' >$vmstat_logfile 2>&1
done
} &
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment