Log Android7.1 logd 日志记录缓冲区大小 Android5.1 抓取系统日志脚本

Android logd日志原理   http://gityuan.com/2018/01/27/android-log/ 

一.Android7.1  logd 日志记录缓冲区大小先看上层

1.1.  布局 KEY

    private static final String SELECT_LOGD_SIZE_KEY = "select_logd_size";
    private static final String SELECT_LOGD_SIZE_PROPERTY = "persist.logd.size";

1.2. writeLogdSizeOption()

           updateLogpersistValues()

    private void writeLogdSizeOption(Object newValue) {
        boolean disable = (newValue != null) &&
            (newValue.toString().equals(SELECT_LOGD_OFF_SIZE_MARKER_VALUE));
        String currentTag = SystemProperties.get(SELECT_LOGD_TAG_PROPERTY);
        if (currentTag == null) {
            currentTag = "";
        }
        // filter clean and unstack all references to our setting
        String newTag = currentTag.replaceAll(
                ",+" + SELECT_LOGD_TAG_SILENCE, "").replaceFirst(
                "^" + SELECT_LOGD_TAG_SILENCE + ",*", "").replaceAll(
                ",+", ",").replaceFirst(
                ",+$", "");
        if (disable) {
            newValue = SELECT_LOGD_MINIMUM_SIZE_VALUE;
            // Make sure snet_event_log get through first, but do not override
            String snetValue = SystemProperties.get(SELECT_LOGD_SNET_TAG_PROPERTY);
            if ((snetValue == null) || (snetValue.length() == 0)) {
                snetValue = SystemProperties.get(SELECT_LOGD_RUNTIME_SNET_TAG_PROPERTY);
                if ((snetValue == null) || (snetValue.length() == 0)) {
                    SystemProperties.set(SELECT_LOGD_SNET_TAG_PROPERTY, "I");
                }
            }
            // Silence all log sources, security logs notwithstanding
            if (newTag.length() != 0) {
                newTag = "," + newTag;
            }
            // Stack settings, stack to help preserve original value
            newTag = SELECT_LOGD_TAG_SILENCE + newTag;
        }
        if (!newTag.equals(currentTag)) {
            SystemProperties.set(SELECT_LOGD_TAG_PROPERTY, newTag);
        }
        String defaultValue = defaultLogdSizeValue();
        final String size = ((newValue != null) && (newValue.toString().length() != 0)) ?
            newValue.toString() : defaultValue;
        SystemProperties.set(SELECT_LOGD_SIZE_PROPERTY, defaultValue.equals(size) ? "" : size);
        SystemProperties.set("ctl.start", "logd-reinit");
        pokeSystemProperties();
        updateLogdSizeValues();
    }


    private void updateLogdSizeValues() {
        if (mLogdSize != null) {
            String currentTag = SystemProperties.get(SELECT_LOGD_TAG_PROPERTY);
            String currentValue = SystemProperties.get(SELECT_LOGD_SIZE_PROPERTY);
            if ((currentTag != null) && currentTag.startsWith(SELECT_LOGD_TAG_SILENCE)) {
                currentValue = SELECT_LOGD_OFF_SIZE_MARKER_VALUE;
            }
            if (mLogpersist != null) {
                String currentLogpersistEnable
                    = SystemProperties.get(ACTUAL_LOGPERSIST_PROPERTY_ENABLE);
                if ((currentLogpersistEnable == null)
                        || !currentLogpersistEnable.equals("true")
                        || currentValue.equals(SELECT_LOGD_OFF_SIZE_MARKER_VALUE)) {
                    writeLogpersistOption(null, true);
                    mLogpersist.setEnabled(false);
                } else if (mLastEnabledState) {
                    mLogpersist.setEnabled(true);
                }
            }
            if ((currentValue == null) || (currentValue.length() == 0)) {
                currentValue = defaultLogdSizeValue();
            }
			//gatsby 加载res 资源
            String[] values = getResources().getStringArray(R.array.select_logd_size_values);
            String[] titles = getResources().getStringArray(R.array.select_logd_size_titles);
            int index = 2; // punt to second entry if not found
            if (SystemProperties.get("ro.config.low_ram").equals("true")) {
                mLogdSize.setEntries(R.array.select_logd_size_lowram_titles);
                titles = getResources().getStringArray(R.array.select_logd_size_lowram_titles);
                index = 1;
            }
            String[] summaries = getResources().getStringArray(R.array.select_logd_size_summaries);
            for (int i = 0; i < titles.length; i++) {
                if (currentValue.equals(values[i])
                        || currentValue.equals(titles[i])) {
                    index = i;
                    break;
                }
            }
            mLogdSize.setValue(values[index]);
            mLogdSize.setSummary(summaries[index]);
            mLogdSize.setOnPreferenceChangeListener(this);
        }
    }

 二.LogBuffer.cpp 

persist.logd.size
persist.logd.size
ro.logd.size
LOG_BUFFER_SIZE, 即256k
LOG_BUFFER_MIN_SIZE, 即64k

三.默认512K

--- a/frameworks/base/packages/SettingsLib/res/values-zh-rCN/arrays.xml
+++ b/frameworks/base/packages/SettingsLib/res/values-zh-rCN/arrays.xml
@@ -62,6 +62,7 @@
     <item msgid="8665206199209698501">"关闭"</item>
     <item msgid="1593289376502312923">"64K"</item>
     <item msgid="487545340236145324">"256K"</item>
+    <item msgid="487545340236145324">"512K"</item>
     <item msgid="2423528675294333831">"1M"</item>
     <item msgid="180883774509476541">"4M"</item>
     <item msgid="2803199102589126938">"16M"</item>
@@ -76,6 +77,7 @@
     <item msgid="6921048829791179331">"关闭"</item>
     <item msgid="2969458029344750262">"每个日志缓冲区 64K"</item>
     <item msgid="1342285115665698168">"每个日志缓冲区 256K"</item>
+    <item>"每个日志缓冲区 512K"</item>
     <item msgid="1314234299552254621">"每个日志缓冲区 1M"</item>
     <item msgid="3606047780792894151">"每个日志缓冲区 4M"</item>
     <item msgid="5431354956856655120">"每个日志缓冲区 16M"</item>
diff --git a/frameworks/base/packages/SettingsLib/res/values/arrays.xml b/frameworks/base/packages/SettingsLib/res/values/arrays.xml
old mode 100644
new mode 100755
index 920e061..b57115b
--- a/frameworks/base/packages/SettingsLib/res/values/arrays.xml
+++ b/frameworks/base/packages/SettingsLib/res/values/arrays.xml
@@ -122,6 +122,7 @@
     <string-array name="select_logd_size_values" translatable="false" >
         <item>32768</item>
         <item>65536</item>
+	<item>524288</item>
         <item>262144</item>
         <item>1048576</item>
         <item>4194304</item>
diff --git a/system/core/logd/LogBuffer.cpp b/system/core/logd/LogBuffer.cpp
old mode 100644
new mode 100755
index 0497a89..5a2a597
--- a/system/core/logd/LogBuffer.cpp
+++ b/system/core/logd/LogBuffer.cpp
@@ -97,14 +97,20 @@ void LogBuffer::init() {
     static const char global_tuneable[] = "persist.logd.size"; // Settings App
     static const char global_default[] = "ro.logd.size";       // BoardConfig.mk
 
+	SLOGE("gatsby LogBuffer init");
+
     unsigned long default_size = property_get_size(global_tuneable);
+	SLOGE("000 gatsby default_size ->  %lu .",default_size);	
     if (!default_size) {
+		SLOGE("000 gatsby !!!default_size aaa ->  %lu .",default_size);
         default_size = property_get_size(global_default);
+		SLOGE("000 gatsby !!!default_size bbb ->  %lu .",default_size);
         if (!default_size) {
             default_size = property_get_bool("ro.config.low_ram",
                                              BOOL_DEFAULT_FALSE)
                 ? LOG_BUFFER_MIN_SIZE // 64K
                 : LOG_BUFFER_SIZE;    // 256K
+			SLOGE("000 gatsby !!!!!default_size ccc ->  %lu .",default_size);
         }
     }
 
@@ -116,24 +122,32 @@ void LogBuffer::init() {
 
         snprintf(key, sizeof(key), "%s.%s",
                  global_tuneable, android_log_id_to_name(i));
+		//getprop persist.logd.size		 
         unsigned long property_size = property_get_size(key);
 
         if (!property_size) {
             snprintf(key, sizeof(key), "%s.%s",
                      global_default, android_log_id_to_name(i));
             property_size = property_get_size(key);
+			SLOGE("111 gatsby global_tuneable ->  %s . android_log_id_to_name(i) -> %s .",global_tuneable,android_log_id_to_name(i));
+			SLOGE("111 gatsby property_size ->  %lu .",property_size);
         }
 
         if (!property_size) {
             property_size = default_size;
+			SLOGE("2222 gatsby property_size ->  %lu .",property_size);
         }
 
         if (!property_size) {
+			//256k
             property_size = LOG_BUFFER_SIZE;
+			SLOGE("333 gatsby property_size ->  %lu .",property_size);
         }
 
         if (setSize(i, property_size)) {
+			//64k
             setSize(i, LOG_BUFFER_MIN_SIZE);
+			SLOGE("444 gatsby property_size ->  %lu .",property_size);
         }
     }
     bool lastMonotonic = monotonic;

 四.Android5.1 抓取系统日志脚本 

4.1.logger.sh

#!/system/bin/sh

FILE_PATH=/mnt/internal_sd/logger

FILE_PREFIX_KERNLOG=kernlog
FILE_PREFIX_MAINLOG=mainlog
FILE_PREFIX_RADIOLOG=radiolog
FILE_PREFIX_PPPDLOG=pppdlog
#sleep 30
if [ ! -d $FILE_PATH ]
then
mkdir -p $FILE_PATH
fi

rename_logfile()
{
	FILE_PREFIX=$1
	LAST_LAST_LAST_FILE=`busybox find $FILE_PATH/ -name "$FILE_PREFIX-*.log.last.last"`
	LAST_LAST_FILE=`busybox find $FILE_PATH/ -name "$FILE_PREFIX-*.log.last"`
	LAST_FILE=`busybox find $FILE_PATH/ -name "$FILE_PREFIX-*.log"`
	#change log files name
	echo "$LAST_LAST_LAST_FILE $LAST_LAST_FILE $LAST_FILE"
	rm $LAST_LAST_FILE
	mv $LAST_LAST_FILE $LAST_LAST_FILE.last
	mv $LAST_FILE $LAST_FILE.last
}

logger_kmsg()
{
	echo "logger $FILE_PREFIX_KERNLOG ..."
	rename_logfile $FILE_PREFIX_KERNLOG
	busybox cat /proc/kmsg > $FILE_PATH/$FILE_PREFIX_KERNLOG-`date +%Y%m%d-%H%M%S`.log 
}

logger_logcat()
{
	echo "logger $FILE_PREFIX_MAINLOG ..."
	rename_logfile $FILE_PREFIX_MAINLOG
	logcat -b main -b radio -b system  -v time -f $FILE_PATH/$FILE_PREFIX_MAINLOG-`date +%Y%m%d-%H%M%S`.log 
}

logger_pppd()
{
	echo "logger $FILE_PREFIX_PPPDLOG ..."
	#rename_logfile $FILE_PREFIX_PPPDLOG
	logcat -v time -s pppd > $FILE_PATH/$FILE_PREFIX_PPPDLOG-`date +%Y%m%d-%H%M%S`.log 
}

logger_radio()
{
	echo "logger $FILE_PREFIX_RADIOLOG ..."
#	rename_logfile $FILE_PREFIX_RADIOLOG
	logcat -v time -b radio -f $FILE_PATH/$FILE_PREFIX_RADIOLOG-`date +%Y%m%d-%H%M%S`.log 
}



case "$1" in
kernel)
	logger_kmsg;;
android)
	logger_logcat;;
pppd)
	logger_pppd;;
radio)
	logger_radio;;
*)
	print "error argument, only for kernel, android, radio,pppd\n"
	;;
esac

4.2.device\rockchip\common\init.rk30board.rc

service logger_kernel /system/bin/logger.sh kernel
        user root
        group root
        disabled
        oneshot

service logger_android /system/bin/logger.sh android
        user root
        group root
        disabled
        oneshot
       
service logger_radio /system/bin/logger.sh radio
        user root
        group root
        disabled
        oneshot

service logger_pppd /system/bin/logger.sh pppd
        user root
        group root
        disabled
        oneshot                

on property:persist.sys.logger=1
    start logger_kernel
    start logger_android
    start logger_radio
    start logger_pppd

4.3.  a.最大保存5份文件  b.满5份之后删除最旧的一份

        c.Notepad++ 下编辑-文档格式转换-转换为UNIX格式 

 test.sh

count=5
for i in `seq $(($count+1))`
do
       echo "i is $i"
       if [ ! -f "log/kmsg_$i.log" ]; then
		   echo "i is break!"
           break
       fi
done

if [ $i -eq $(($count+1)) ]; then 
       rm -rf log/kmsg_1.log
       i=$count
       for j in `seq $(($count-1))`
       do
               echo "log/kmsg_$(($j+1)).log log/kmsg_$j.log"
               mv log/kmsg_$(($j+1)).log log/kmsg_$j.log
       done
fi

touch  log/kmsg_$i.log
echo `date +%Y.%m.%d.%H.%M.%S` > log/kmsg_$i.log

kmsg.sh

#Save five files as max.
count=5
for i in `seq $(($count+1))`
do
       #echo "i is $i"
       if [ ! -f "/data/log/kmsg_$i.log" ]; then
           break
       fi
done

if [ $i -eq $(($count+1)) ]; then 
       rm /data/log/kmsg_1.log
       i=$count
       for j in `seq $(($count-1))`
       do
               #echo "/data/log/kmsg_$(($j+1)).log /data/log/kmsg_$j.log"
               mv /data/log/kmsg_$(($j+1)).log /data/log/kmsg_$j.log
       done
fi


cat /proc/kmsg > /data/log/kmsg_$i.log


#cat /proc/kmsg > /data/log/kmsg_`date +%Y%m%d%H%M%S`.log

  

  

posted @ 2021-04-30 13:37  CrushGirl  阅读(1125)  评论(0编辑  收藏  举报