[转载]起动service保存android系统log( logcat服务)

时间:2022-06-23 21:01:48

原文链接:http://www.myexception.cn/android/1904013.html

启动service保存android系统log

作为android开发工程师,出现了BUG是否苦于没有log而苦恼万分呢,以下敝人提供一套自动保存log的方法,供大家借鉴学习:

首先,在产品目录的init.XXX.rc文件中,添加相应的service,

# start log service
start logd
on property:service.logcat.enable=
start logcat_service on property:service.logcat.enable=
stop logcat_service # log services
service logcat_service /system/bin/logcat -b system -b events -b main -b radio -k -n -v threadtime -r5000 -f /data/Logs/Log./logcat.log
user root
group log system
class main
disabled service logd /system/bin/sh /system/bin/logd.sh
user system
group log
oneshot

然后,在目标平台的system/bin下添加脚本文件logd.sh,处理存储的log日志,以及设置属性,开启logcat_service,

#!/system/bin/sh

#
# Global folder & cmd params
# OUTPUT_DIR=/data
LOG=Logs
index= LOG_DIR[]=$OUTPUT_DIR/$LOG/Log.
LOG_DIR[]=$OUTPUT_DIR/$LOG/Log.
LOG_DIR[]=$OUTPUT_DIR/$LOG/Log. RM=rm
MV="mv"
MKDIR=mkdir
UMASK=umask # set default permission
$UMASK # Init the three folders i=
while [ "$i" -le "$index" ]
do
$MKDIR -p ${LOG_DIR[$i]}
i=$(($i+))
done # Transfer the three folders
((i=$index-))
$RM -r ${LOG_DIR[$index]}/*
while [ "$i" -ge "0" ]
do
$MV ${LOG_DIR[$i]}/* ${LOG_DIR[$i+1]}
i=$(($i-1))
done
$RM -r ${LOG_DIR[0]}/* # start logcat service
setprop service.logcat.enable 1 mkdir /data/www
cp -R /system/var/www/ /data/
ln -s /storage/external/ /data/www/sdcard