在 “设置” 应用中的开发者选项添加一个开关 保存Logcat和KMSG日志
添加te文件 由于SELinux的原因,需要在sepolicy下添加catlot.te
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 type catlog, domain;type catlog_exec, exec_type, file_type;allow init catlog_exec:file { execute getattr read open };allow init catlog:process { transition };allow init catlog:process { rlimitinh siginh noatsecure };allow catlog kernel:system { syslog_mod };allow catlog catlog:capability { dac_override sys_nice };allow catlog catlog:capability2 { syslog };allow catlog catlog_exec:file { execute entrypoint read open };allow catlog shell_exec:file { getattr read };allow catlog rootfs:lnk_file { getattr };allow catlog proc:file { write open read };allow catlog tmpfs:lnk_file { read };allow catlog storage_file:dir { search };allow catlog storage_file:lnk_file { read };allow catlog mnt_user_file:dir { search };allow catlog mnt_user_file:lnk_file { read };allow catlog fuse:dir { search getattr create write read open add_name rename remove_name };allow catlog fuse:file { getattr create write open rename append };allow catlog toolbox_exec:file { execute read open getattr execute_no_trans };allow catlog logdr_socket:sock_file { write };allow catlog logd:unix_stream_socket { connectto };allow catlog logcat_exec:file { execute read open execute_no_trans getattr };
添加cat_log.sh
脚本 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 #!/system/bin/sh function enable_log (){ LOG_FILE="/data/tool.log" exec 2>> $LOG_FILE exec 1>> $LOG_FILE echo "----------------------------------" echo "para: $*" } enable_log $* ; set -x echo 1 > /proc/sys/kernel/panicENABLE_LOG_FILE="/mnt/sdcard/.enable_logsave" LOG_DIR="/mnt/sdcard/LOGSAVE" LAST_LOG_DIR="/mnt/sdcard/LOGSAVE/last" SAVE_LOG_COUNT=5 if [ ! -d "$LOG_DIR " ];then mkdir $LOG_DIR fi if [ -d "$LAST_LOG_DIR .$SAVE_LOG_COUNT " ];then rm -r "$LAST_LOG_DIR .$SAVE_LOG_COUNT " fi i=$((SAVE_LOG_COUNT -1 )) while [ $i -ge 1 ]do if [ -d "$LAST_LOG_DIR .$i " ];then if [ "`ls -a $LAST_LOG_DIR .$i `" = "" ]; then echo "$LAST_LOG_DIR .$i is indeed empty" else echo "$LAST_LOG_DIR .$i is not empty" j=$(($i +1 )) mv "$LAST_LOG_DIR .$i " "$LAST_LOG_DIR .$j " fi fi i=$(($i -1 )) done mkdir $LAST_LOG_DIR ."1" mv $LOG_DIR /*.log $LAST_LOG_DIR ."1" mv $LOG_DIR /*.log * $LAST_LOG_DIR ."1" DATE=$(date +%Y%m%d%H%M) cat /sys/fs/pstore/console-ramoops"-0" > $LOG_DIR /"$DATE " _panic_kmsg.log echo "------start kmsg log------" cat /proc/kmsg > $LOG_DIR /"$DATE " _kmsg.log &echo "------start logcat log------" logcat -v time -n 1 -f $LOG_DIR /"$DATE " _logcat.log -r10240
拷贝脚本到system/bin
在mk文件添加
1 PRODUCT_COPY_FILES +=$(CUR_PATH)/cat_log.sh :system /bin/cat_log.sh
property_service
里赋予权限在property_service.cpp 的检查权限check_mac_perms函数中添加
1 2 3 4 if (strcmp ("persist.sys.cat_log" ,name) == 0 ) { return 1 ; }
init.*.rc
里声明服务1 2 3 4 5 6 7 8 9 10 11 service catlog /system/bin/cat_log.sh seclabel u:r:catlog:s0 disabled oneshot on property :persist.sys.cat_log= 1 start catlog on property :persist.sys.cat_log= 0 stop catlog
其中的seclabel就是SELinux要用到的标志
开发者选项中添加一个SwitchPreference组件 1 2 3 4 5 <SwitchPreference android:key ="logcat_enable" android:title ="@string/logcat_enable" android:summary ="@string/logcat_summary" android:fragment ="com.android.tv.settings.system.development.AdbDialog" />
添加开关控制逻辑 在DevelopmentFragment.java的onPreferenceTreeClick里添加相关逻辑
1 2 3 4 5 6 7 8 9 if (preference == mLogcatPreference){ if (mLogcatPreference.isChecked()){ writeLogcatEnableOptions (1 ); setLogcatEnable (getPreferenceManager().getContext (), 1 ); } else { writeLogcatEnableOptions (0 ); setLogcatEnable (getPreferenceManager().getContext (), 0 ); } }
开机时初始化状态 1 2 3 4 5 6 7 8 9 10 11 12 13 String action = intent.getAction(); if (action.equals (Intent.ACTION_BOOT_COMPLETED)) { SharedPreferences sharedPreferences = context .getSharedPreferences("TvSetting" , Context.MODE_PRIVATE); boolean enable_log_save = sharedPreferences.getBoolean( "enable_log_save" , false ); String persist_sys_cat_log = SystemProperties.get("persist.sys.cat_log" ); Log .i("BootReceiver" , "action==ACTION_BOOT_COMPLETED,BootReceiver is start" ); if (persist_sys_cat_log.equals ("1" )) { SystemProperties.set ("persist.sys.cat_log" , "1" ); } }
编译运行,打开开关,可以看到LOG保存到了sdcard/LOGSAVE目录下面。