Log Android7.1 logd 日志记录缓冲区大小 Android5.1 抓取系统日志脚本
Android logd日志原理 http://gityuan.com/2018/01/27/android-log/
一.Android7.1 logd 日志记录缓冲区大小先看上层
1.1. 布局 KEY
1 2 | 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()
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 | 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
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 | --- 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
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 | #!/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
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 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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
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 | #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 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】