在做android开发过程中,我们经常会用到logcat,通过logcat可以更直接的看出程序执行的顺序以及开发产生的日志信息,但是,我们在开发过程中也经常看到logcat输出read: Unexpected EOF!异常,然后日志就不在打印了。今天我们就分析一下为什么logcat会出现read: Unexpected EOF!异常,以及出现read: Unexpected EOF!异常后日志无法正常输出的原因。

分析

查阅先关资料,仔细分析后你会发现,出现这个日志,是因为最终的logcat进程退出,而退出的的原因是log buffer size设置过小导致,而默认size为256KB,如果你的程序长时间运行,并且产生了大量的日志,最终日志缓存的大小肯定是超过了默认的256kb。

分析大致过程如下:

1.在应用或者服务等进程 往logd中写入log量过大时(大于buffer size设置的2倍),logd会调用kickMe函数,这里面会去判断stats size即系统中实际需要占用的大小,当大于2倍我们在init函数中设定的默认buffer size(64KB)时,Logd认为reader读取数据的速度过慢,会主动release_Locked函数尝试断开连接,断开连接后会导致logd.reader.per线程while循环break退出,Logd.cpp -> kickMe函数部分代码:

void LogBuffer::kickMe(LogTimeEntry* me, log_id_t id, unsigned long pruneRows) {if (stats.sizes(id) > (2 * log_buffer_size(id))) {  // +100%// A misbehaving or slow reader has its connection// dropped if we hit too much memory pressure.me->release_Locked();

2. logd.reader.per线程线程退出后,会调用SocketListener监听类的SocketListener::release,logd开启的LogReader是继承自SocketListener,会调用到doSocketDelete,SocketClient相关联的decRef函数,mRefCount—减值后会调用到~SocketClient析构函数,析构后会调用close(mSocket) 关闭SocketListener端的socket连接。

 3.导致最终Logcat端进程的while循环中android_logger_list_read读取到的数据为0,logcat进程主动调用logcat_panic进程,logcat进程退出。

 while (!context->stop &&(!context->maxCount || (context->printCount < context->maxCount))) {struct log_msg log_msg;int ret = android_logger_list_read(logger_list, &log_msg);if (!ret) {fprintf(stderr, "android_logger_list_read error ,ret:%d !\n", ret);logcat_panic(context, HELP_FALSE, "read: unexpected EOF!\n");break;}

既然找到了原因,是否可以想办法扩充一下这个默认日志缓存区的大小了,当然是可以的。所以我们只需要将buffer size设置为2M/3M/4M......即可,下面我们看一下如何扩充缓存区。

如何扩充日志缓存区大小

首先,我们要先知道logcat常用的命令,以及常用命令的作用。我们打开系统的终端,或者开发工具idea的终端。然后输入adb shell logcat -help,常看常用的命令如下:

C:\Users\Administrator>adb shell logcat -help
Unrecognized Option h
Usage: logcat [options] [filterspecs]
options include:-s              Set default filter to silent. Equivalent to filterspec '*:S'-f <file>, --file=<file>               Log to file. Default is stdout-r <kbytes>, --rotate-kbytes=<kbytes>Rotate log every kbytes. Requires -f option-n <count>, --rotate-count=<count>Sets max number of rotated logs to <count>, default 4-v <format>, --format=<format>Sets the log print format, where <format> is:brief color epoch long monotonic printable process rawtag thread threadtime time uid usec UTC year zone-D, --dividers  Print dividers between each log buffer-c, --clear     Clear (flush) the entire log and exitif Log to File specified, clear fileset instead-d              Dump the log and then exit (don't block)-e <expr>, --regex=<expr>Only print lines where the log message matches <expr>where <expr> is a regular expression-m <count>, --max-count=<count>Quit after printing <count> lines. This is meant to bepaired with --regex, but will work on its own.--print         Paired with --regex and --max-count to let content bypassregex filter but still stop at number of matches.-t <count>      Print only the most recent <count> lines (implies -d)-t '<time>'     Print most recent lines since specified time (implies -d)-T <count>      Print only the most recent <count> lines (does not imply -d)-T '<time>'     Print most recent lines since specified time (not imply -d)count is pure numerical, time is 'MM-DD hh:mm:ss.mmm...''YYYY-MM-DD hh:mm:ss.mmm...' or 'sssss.mmm...' format-g, --buffer-size                      Get the size of the ring buffer.-G <size>, --buffer-size=<size>Set size of log ring buffer, may suffix with K or M.-L, -last       Dump logs from prior to last reboot-b <buffer>, --buffer=<buffer>         Request alternate ring buffer, 'main','system', 'radio', 'events', 'crash', 'default' or 'all'.Multiple -b parameters or comma separated list of buffers areallowed. Buffers interleaved. Default -b main,system,crash.-B, --binary    Output the log in binary.-S, --statistics                       Output statistics.-p, --prune     Print prune white and ~black list. Service is specified asUID, UID/PID or /PID. Weighed for quicker pruning if prefixwith ~, otherwise weighed for longevity if unadorned. Allother pruning activity is oldest first. Special case ~!represents an automatic quicker pruning for the noisiestUID as determined by the current statistics.-P '<list> ...', --prune='<list> ...'Set prune white and ~black list, using same format aslisted above. Must be quoted.--pid=<pid>     Only prints logs from the given pid.--wrap          Sleep for 2 hours or when buffer about to wrap whichevercomes first. Improves efficiency of polling by providingan about-to-wrap wakeup.filterspecs are a series of<tag>[:priority]where <tag> is a log component tag (or * for all) and priority is:V    Verbose (default for <tag>)D    Debug (default for '*')I    InfoW    WarnE    ErrorF    FatalS    Silent (suppress all output)'*' by itself means '*:D' and <tag> by itself means <tag>:V.
If no '*' filterspec or -s on command line, all filter defaults to '*:V'.
eg: '*:S <tag>' prints only <tag>, '<tag>:S' suppresses all <tag> log messages.If not specified on the command line, filterspec is set from ANDROID_LOG_TAGS.If not specified with -v on command line, format is set from ANDROID_PRINTF_LOG
or defaults to "threadtime"

下面我就简单介绍几个常用命令,只有熟悉了这些命令,你才能更快更直接定位和解决问题。

  1. 查看buffer size 命令 :logcat -g                                                                                                                                                -g命令可以查看当前设备日志缓存区 大小,打开终端输入logcat -g:

C:\Users\Administrator>adb logcat -g
main: ring buffer is 2Mb (1Mb consumed), max entry is 5120b, max payload is 4068b
system: ring buffer is 2Mb (634b consumed), max entry is 5120b, max payload is 4068b
crash: ring buffer is 2Mb (0b consumed), max entry is 5120b, max payload is 4068b

2.修改buffer size 命令:logcat -G<size>

-G<size>命令修改buffer size ,打开终端输入logcat -G<size>,如下图所示,buffer size 从2M变成了4M

注意:

修改和查看buffer日志缓存区大小都是用大英语字母“g”的,但是区别在于,修改是大写字母"G",而查看是小写字母“g”.

更多命令这里就不在一一介绍说明,言归正传继续解决read: Unexpected EOF。

解决read: Unexpected EOF方式:

通过上面的分析和对应logcat命令介绍,大概知道了如何解决read: Unexpected EOF这个问题,下面就介绍一下俩种常用2种方式:

1.logcat -G《size》命令解决

adb logcat -G 4m

2.手动修改系统设置

打开自己手中的调试手机或者调试设备,以此点击:开发者选项===》日志记录器缓冲区大小, 然后修改一下系统的日志缓冲区大小即可,如下图所示:

系统默认是256kb

可以发现,系统默认的就是256kb,我们可以手动修改日志大小。至于使用哪种方式,可以根据具体实际的使用情况进行设置。

分析解决logcat报read: Unexpected EOF!异常相关推荐

  1. 分析解决logcat报read: Unexpected EOF

    分析解决logcat报read: Unexpected EOF 0. 前言 1. 分析 2. 解决方案 2.1 logcat命令 2.2 属性控制 0. 前言   在做android开发调试过程中,我 ...

  2. 一招解决 adb logcat 输出 日志 报错 read: unexpected EOF 俩种解决办法 设置缓存区 清空logcat 缓存

    问题: adb 输出日志报错read: unexpected EOF! 然后adb 就自动中断了 read: unexpected EOF! 原因: logcat 进程异常退出,是因为 log buf ...

  3. 【错误记录】Android Studio Logcat 报错 ( read: unexpected EOF! )

    文章目录 一.报错信息 二.解决方案 一.报错信息 Android Studio 运行一段时间后 , 出现 Logcat 不打印日志的情况 ; 报错 : read: unexpected EOF! 二 ...

  4. 达梦数据库报“网络通信异常”分析解决

    前言: 达梦数据库在通过程序插入具有BLOB字段的记录时(非通过SQL直接插入),报"通信异常",通过更换达梦库驱动包解决. 问题: 在一个项目现场,在进行数据导入时,总时报&qu ...

  5. JS Uncaught SyntaxError:Unexpected identifier异常报错原因及其解决方法

    最近在写ajax的时候,调用js方法,遇到了Uncaught SyntaxError:Unexpected identifier异常报错,开始搞不清原因,很苦恼. 以为是js方法参数个数和长度的问题, ...

  6. SyntaxError: unexpected EOF while parsing解决方法

    SyntaxError: unexpected EOF while parsing 意思是:语法错误:分析时出现意外的EOF(循环结束不了) 我遇到的问题是:在读取文件中的str类型时,将其通过eva ...

  7. 解决 unexpected EOF while looking for matching `“‘

    原因在于符号 " 错误,解决办法:检查报错行的符号 例子: error: line 12: unexpected EOF while looking for matching `" ...

  8. 解决WAS报错SRVE0207E: servlet 创建了未捕获到的初始化异常

    解决WAS报错SRVE0207E: servlet 创建了未捕获到的初始化异常 参考文章: (1)解决WAS报错SRVE0207E: servlet 创建了未捕获到的初始化异常 (2)https:// ...

  9. 解决eclipse报PermGen space异常的问题

    解决eclipse报PermGen space异常的问题 参考文章: (1)解决eclipse报PermGen space异常的问题 (2)https://www.cnblogs.com/zj0208 ...

最新文章

  1. 交换机启用光口命令_如何在思科交换机上查询光模块状态?
  2. 支付宝支付回调异常_支付宝崩了是怎么回事 支付宝崩了部分用户使用异常现已恢复...
  3. 将文件标记为 side-effect-free(无副作用)
  4. 文档协作编辑 ONLYOFFICE 部署和使用教程
  5. [转]UML建模的要点总结
  6. 可由一个尾指针唯一确定的链表有_六十九、数据结构链表的实现
  7. 【软件工程】对于Why Software Development Methodologies Suck(为什么软件开发方法论让你觉得糟糕)问题的探讨
  8. 算法图解第一章笔记与习题(算法简介)
  9. oracle 设置 锁模式,oracle表锁的几种模式v$locked_object-locked_mode
  10. 商空间的理解(Quotient space)
  11. Instant类[java]
  12. python-patterns
  13. PS知识点大总结(二)——通道蒙版与图形图标设计
  14. Log4j写日志文件使用详解
  15. 数据结构之不带头结点单链表和带头结点单链表相关操作实现(C语言)
  16. 2021前端面试总结及反思
  17. virtualbox虚机硬盘扩容
  18. png 微软ppt 透明度_挖到一个宝藏级的PPT素材网站,哭着五星好评,免费又实用...
  19. 寒冬已至?四面楚歌的Android工程师该何去何从
  20. BUG的优先级(Priority)和严重程度(Severity)Blocker, Critical, Major, Minor/Trivial

热门文章

  1. s3c6410_user_manual_rev1.1
  2. ARM指令集与Thumb指令集--区别关联--汇编指令 BX LR ; 跳转回LR地址处,既可以是ARM模式也可以是Thumb模式
  3. about Microsoft Office SharePoint Portal Server 2003
  4. 汇编语言比C51需要效率高,汇编语言与C51语言实现跑马灯实验的比较 -
  5. linux文件名快速键入,linux修改文件名【使用模式】
  6. 超大杯来了!一加10 Ultra将在第三季度登场:或搭载OPPO 自研影像芯片
  7. 网易手游《幻书启世录》将于2022年2月14日停止运营
  8. 腾讯接入华为鸿蒙!QQ音乐与Harmony OS达成合作 推出听歌识曲万能卡片
  9. 世界地球日:全国网友用手机种出“保护黄河幸福林”
  10. 飞书成小米数万员工协作工具,雷军:越用越顺手