android7.1日志系统功能开发

前言

    每个公司都会要求将系统所有日志保存到指定目录,方便分析问题,我们公司也不例外,下面记录我这边做功能的具体实现。

实现

   1.创建打印日志的脚本init.corelog.sh

      目录:/device/qcom/msm8953_64/init.corelog.sh

#!/system/bin/sh
#LOGPATH=/sdcard/corelog
DATE=$(date +%Y%m%d%H%M)
ROOTPATH=/data/corelog
LOGPATH=$ROOTPATH/$DATE/
LOGPATH_LAST=$ROOTPATH/last

LOGCAT=$LOGPATH/logcat_"$DATE".txt
LOGCAT_RADIO=$LOGPATH/logcat_radio_"$DATE".txt

LOGKMSG=$LOGPATH/kmsg_"$DATE".txt
LOGCAT_EVENT=$LOGPATH/logcat_event_"$DATE".txt
CUR_PWD=$PWD
if [ -d $LOGPATH_LAST  ];then
 cd $ROOTPATH
 #find . -regex "./[0-9]+" | xargs -i mv {} $LOGPATH_LAST/
 find  ./ -maxdepth 1 -type d  -name "*[0-9]*" -exec mv {} $LOGPATH_LAST/ \;
 cd $CUR_PWD
fi

#ensure $LOGPATH is exist
if [ ! -d $LOGPATH ]; then
mkdir -p $LOGPATH
fi

# remove old backup log files
#rm -r $LOGPATH_LAST

# re-creat backup directory
if [ ! -d $LOGPATH_LAST ]; then
mkdir $LOGPATH_LAST
fi

#copy last files to backup directory
#mv $LOGPATH/*.* $LOGPATH_LAST
#m~_0m~I0m~W[0m~Wm~[[0m~U0m~K0m~E0m~G2天m~Z~D以".log"0m~S尾m~Z~Dm~W[0m~Wm~V~G件并m~H| m~Ym
find $LOGPATH_LAST/ -type d -name "*[0-9]*" -mtime +2 | xargs rm -rf
# set core_pattern to let kernel generate core dump during process crash
# core dump file(core-PID-EXE_NAME-TIME.dump) will be generated in /data 
#echo $LOGPATH/core-%p-%e-%t.dump > /proc/sys/kernel/core_pattern


# set RLIMIT_CORE to set core dump file size(multiply of 512)
#ulimit -c 2048
#/system/bin/sh ulimit -c 2048
#use init buildin command setrlimit
#setrlimit 4  40960000 40960000
echo 0 > /sys/class/gpio/gpio8/value
echo "8" > /proc/sys/kernel/printk
#cat /proc/kmsg > $LOGKMSG &
/system/bin/logcat -r50000 -n 20 -b kernel -v time -f $LOGKMSG &

/system/bin/logcat -r50000 -n 20 -v time -f $LOGCAT &
/system/bin/logcat -r50000 -n 20 -b radio -v time -f $LOGCAT_RADIO &

/system/bin/logcat -r50000 -n 20 -b events -v time -f $LOGCAT_EVENT &

2.设置创建日志服务

   目录:/device/qcom/msm8953_64/init.target.rc

service corelog /system/bin/sh /etc/init.corelog.sh
    disabled
    user root
    oneshot

3.日志需要按日期切换目录去保存,下面需要实现一个定时服务,来开关日志服务,以达到切换目录保存的目的;还需要一个清除老的日志的服务,达到手动删除老的日志的目的:

    目录:(1)/device/qcom/msm8953_64/init.alarmclock.sh(定时切换目录保存日志文件)

             

#!/bin/sh

#Section configuration(m~E~M置m~C0m~H~F) 
#Task Time ,ex:203000(Time 20:30:00);190000(Time 19:00:00);
startTime=235959
#Script File
#scriptfile=./init.corelog.sh
scriptfile=/etc/init.corelog.sh

#Section promgram (0m~K0m~Om~I[0m~Lm~C0m~H~F) 
perDate=$(date "+%Y%m%d")
isNewDay=1
isFirstTime=1

echo 'Task schedule Time: ('$startTime') scriptfile: ('$scriptfile') Waiting...'

while true ; do
        curTime=$(date "+%H%M%S")
        curDate=$(date "+%Y%m%d")
        echo 'New Day curDate: ('$curDate') curTime: ('$curTime') isNewDay: ('$isNewDay') perDate ('$perDate') isFirstTime ('$isFirstTime') Waiting...'
        #check and run script
        if  [ "$isNewDay" -eq "1" ];then
                if [ "$curTime" -ge "$startTime" ];then
                        if [ "$isFirstTime" -eq "0" ];then
                                sleep 1
                                echo 'Shell Script ('$scriptfile') Running...'
                                /system/bin/sh $scriptfile
                                echo 'Shell Script ('$scriptfile') Stopped...'
                        fi
                        isNewDay=0
                        echo 'while 0 isFirstTime ('$isFirstTime')'
                else
                        if [ "$isFirstTime" -eq "1" ];then
                                echo 'New Day: ('$curDate') Task schedule Time: ('$startTime') Waiting...'
                                isFirstTime=0
                        fi
                        echo 'while 1 isFirstTime ('$isFirstTime')'

                fi
        else
                #new day start
                if [ "$curDate" -gt "$perDate" ];then
                        echo 'New Day: ('$curDate') Task schedule Time: ('$startTime') Waiting...'
                        isNewDay=1
                        perDate=$curDate
                fi
                echo 'while 2 perDate ('$perDate')'
        fi
        sleep 1
done

       (2) /device/qcom/msm8953_64/clear.corelog.sh(可以手动删除老的日志,在adb中设置setprop persist.clear_corelog 1即可删除last目录下面的老的日志)

#!/system/bin/sh
ROOTPATH=/data/corelog
LOGPATH=$ROOTPATH/$DATE/
LOGPATH_LAST=$ROOTPATH/last
if [ -d $LOGPATH_LAST  ];then
 echo 'clear corelog in the last, Waiting...'
 rm -rf /data/corelog/last/*
fi

 4在init.target.rc中创建相关服务以及添加开启和关闭服务的开关


service corelog /system/bin/sh /etc/init.corelog.sh
    disabled
    user root
    oneshot

service alarmclock /system/bin/sh /etc/init.alarmclock.sh
    disabled
    user root
    oneshot
service clearlog /system/bin/sh /etc/clear.corelog.sh
    disabled
    user root
    oneshot

# and it is set by init
on property:persist.corelog_enable=1
    start corelog
    start alarmclock
on property:persist.corelog_enable=0
    stop corelog
    stop alarmclock
on property:persist.clear_corelog=1
   start clearlog
on property:persist.clear_corelog=0
   stop clearlog

5.开机时启动日志服务

diff --git a/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java b/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
index d31b48c..fe276bd 100755
--- a/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
+++ b/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
@@ -7056,6 +7056,9 @@ public final class ActivityManagerService extends ActivityManagerNative
                 mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY);
                 // Tell anyone interested that we are done booting!
                 SystemProperties.set("sys.boot_completed", "1");
+               if ("1".equals(SystemProperties.get("persist.corelog_enable", "1"))) {
+                 SystemProperties.set("persist.corelog_enable", "1");
+               }
 
                 // And trigger dev.bootcomplete if we are not showing encryption progress
                 if (!"trigger_restart_min_framework".equals(SystemProperties.get("vold.decrypt"))

6.将脚本编译进系统:

   目录: (1)device/qcom/msm8953_64/AndroidBoard.mk

           

include $(CLEAR_VARS)
LOCAL_MODULE       := init.corelog.sh
LOCAL_MODULE_TAGS  := optional eng
LOCAL_MODULE_CLASS := ETC
LOCAL_SRC_FILES    := init.corelog.sh
include $(BUILD_PREBUILT)

include $(CLEAR_VARS)
LOCAL_MODULE       := init.alarmclock.sh
LOCAL_MODULE_TAGS  := optional eng
LOCAL_MODULE_CLASS := ETC
LOCAL_SRC_FILES    := init.alarmclock.sh
include $(BUILD_PREBUILT)

include $(CLEAR_VARS)
LOCAL_MODULE       := clear.corelog.sh
LOCAL_MODULE_TAGS  := optional eng
LOCAL_MODULE_CLASS := ETC
LOCAL_SRC_FILES    := clear.corelog.sh
include $(BUILD_PREBUILT)

               (2)device/qcom/msm8953_64/msm8953_64.mk

PRODUCT_PACKAGES += init.corelog.sh
PRODUCT_PACKAGES += init.alarmclock.sh
PRODUCT_PACKAGES += clear.corelog.sh

            

 

 

   

    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值