继续看logcat.cpp的processBuffer函数,如果执行完Android_log_shouldPrintLine函数后,表明当前日志记录应当输出,则调用android_log_printLogLine函数来输出日志记录到文件fd中, 这个函数也是定义在system/core/liblog/logprint.c文件中:<wbr style="line-height:25px"><div style="line-height:25px"> <div style="line-height:25px"> <span style="color:#993300; line-height:25px">int</span><span style="color:#ff00ff; line-height:25px">Android_log_printLogLine</span>( </div> <div style="line-height:25px"> AndroidLogFormat *p_format, </div> <div style="line-height:25px"> int fd, </div> <div style="line-height:25px"> const AndroidLogEntry *entry) </div> <div style="line-height:25px">{ </div> <div style="line-height:25px"> <span style="color:#993300; line-height:25px">int</span>ret; </div> <div style="line-height:25px"> <span style="color:#993300; line-height:25px">char</span>defaultBuffer[512]; </div> <div style="line-height:25px"> <span style="color:#993300; line-height:25px">char</span>*outBuffer = NULL; </div> <div style="line-height:25px"> size_t totalLen; </div> <div style="line-height:25px"></div> <div style="line-height:25px"><span style="color:#0000ff; line-height:25px"> outBuffer = Android_log_formatLogLine(p_format, defaultBuffer, </span></div> <div style="line-height:25px"><span style="color:#0000ff; line-height:25px"> sizeof(defaultBuffer), entry, &totalLen); </span></div> <div style="line-height:25px"></div> <div style="line-height:25px"> <span style="color:#993300; line-height:25px">if</span>(!outBuffer) </div> <div style="line-height:25px"> return -1; </div> <div style="line-height:25px"></div> <div style="line-height:25px"> <span style="color:#993300; line-height:25px">do</span>{ </div> <div style="line-height:25px"> <span style="color:#ff6600; line-height:25px">ret = write(fd, outBuffer, totalLen); </span> </div> <div style="line-height:25px"> }<span style="color:#993300; line-height:25px">while</span>(ret < 0 && errno == EINTR); </div> <div style="line-height:25px"></div> <div style="line-height:25px"> <span style="color:#993300; line-height:25px">if</span>(ret < 0) { </div> <div style="line-height:25px"> fprintf(stderr, "+++ LOG: write failed (errno=%d)\n", errno); </div> <div style="line-height:25px"> ret = 0; </div> <div style="line-height:25px"> goto done; </div> <div style="line-height:25px"> } </div> <div style="line-height:25px"></div> <div style="line-height:25px"> if (((size_t)ret) < totalLen) { </div> <div style="line-height:25px"> fprintf(stderr, "+++ LOG: write partial (%d of %d)\n", ret, </div> <div style="line-height:25px"> (int)totalLen); </div> <div style="line-height:25px"> goto done; </div> <div style="line-height:25px"> } </div> <div style="line-height:25px"></div> <div style="line-height:25px"> <span style="color:#993300; line-height:25px">done</span>: </div> <div style="line-height:25px"> <span style="color:#993300; line-height:25px">if</span>(outBuffer != defaultBuffer) { </div> <div style="line-height:25px"> free(outBuffer); </div> <div style="line-height:25px"> } </div> <div style="line-height:25px"></div> <div style="line-height:25px"> <span style="color:#993300; line-height:25px">return</span>ret; </div> <div style="line-height:25px">} </div> <div style="line-height:25px"> <span style="color:#000080; line-height:25px"> 这个函数的作用就是把</span><span style="color:#0000ff; line-height:25px">AndroidLogEntry</span><span style="color:#000080; line-height:25px">格式的日志记录按照指定的格式</span><span style="color:#0000ff; line-height:25px">AndroidLogFormat</span><span style="color:#000080; line-height:25px">进行输出了,这里,不再进一步分析这个函数。</span> </div> <div style="line-height:25px"> <span style="color:#003366; line-height:25px">在</span><span style="color:#0000ff; line-height:25px">processBuffer函数</span><span style="color:#003366; line-height:25px">中,最后还有一个</span><span style="color:#ff6600; line-height:25px">rotateLogs</span><span style="color:#003366; line-height:25px">的操作:</span> </div> <div style="line-height:25px"></div> <div style="line-height:25px"> <span style="color:#993300; line-height:25px">static</span><span style="color:#993300; line-height:25px">void</span><span style="color:#ff00ff; line-height:25px">rotateLogs() </span> </div> <div style="line-height:25px">{ </div> <div style="line-height:25px"> <span style="color:#993300; line-height:25px">int</span>err; </div> <div style="line-height:25px"></div> <div style="line-height:25px"> // Can't rotate logs if we're not outputting to a file </div> <div style="line-height:25px"> if (g_outputFileName == NULL) { </div> <div style="line-height:25px"> return; </div> <div style="line-height:25px"> } </div> <div style="line-height:25px"></div> <div style="line-height:25px"> close(g_outFD); </div> <div style="line-height:25px"></div> <div style="line-height:25px"> <span style="color:#993300; line-height:25px">for</span>(int i = g_maxRotatedLogs ; i > 0 ; i--) { </div> <div style="line-height:25px"> char *file0, *file1; </div> <div style="line-height:25px"></div> <div style="line-height:25px"> <span style="color:#ff6600; line-height:25px"> asprintf(&file1, "%s.%d", g_outputFileName, i); </span> </div> <div style="line-height:25px"></div> <div style="line-height:25px"> <span style="color:#993300; line-height:25px">if</span>(i - 1 == 0) { </div> <div style="line-height:25px"> asprintf(&file0, "%s", g_outputFileName); </div> <div style="line-height:25px"> } else { </div> <div style="line-height:25px"> asprintf(&file0, "%s.%d", g_outputFileName, i - 1); </div> <div style="line-height:25px"> } </div> <div style="line-height:25px"></div> <div style="line-height:25px"> err = rename (file0, file1); </div> <div style="line-height:25px"></div> <div style="line-height:25px"> <span style="color:#993300; line-height:25px">if</span>(err < 0 && errno != ENOENT) { </div> <div style="line-height:25px"> perror("while rotating log files"); </div> <div style="line-height:25px"> } </div> <div style="line-height:25px"></div> <div style="line-height:25px"> free(file1); </div> <div style="line-height:25px"> free(file0); </div> <div style="line-height:25px"> } </div> <div style="line-height:25px"></div> <div style="line-height:25px"> g_outFD = openLogFile (g_outputFileName); </div> <div style="line-height:25px"></div> <div style="line-height:25px"> if (g_outFD < 0) { </div> <div style="line-height:25px"> perror ("couldn't open output file"); </div> <div style="line-height:25px"> exit(-1); </div> <div style="line-height:25px"> } </div> <div style="line-height:25px"></div> <div style="line-height:25px"> g_outByteCount = 0; </div> <div style="line-height:25px"></div> <div style="line-height:25px">} </div> <div style="line-height:25px"> <span style="color:#003366; line-height:25px">这个函数只有在执行</span><span style="color:#ff6600; line-height:25px">logcat</span><span style="color:#003366; line-height:25px">命令时,指定了</span><span style="color:#0000ff; line-height:25px">-f <filename></span><span style="color:#003366; line-height:25px">参数时,即</span><span style="color:#0000ff; line-height:25px">g_outputFileName</span><span style="color:#003366; line-height:25px">不为NULL时才起作用。它的作用是在将日志记录循环输出到一组文件中。例如,</span><span style="color:#000080; line-height:25px">指定-f参数为logfile,g_maxRotatedLogs为3,则这组文件分别为:</span><span style="line-height:25px; color:rgb(0,0,128)">logfile,logfile.1,logfile.2,logfile.3</span> </div> <div style="line-height:25px"></div> <div style="line-height:25px"> <span style="color:#333399; line-height:25px">当当前输入到logfile文件的日志记录大小</span><span style="color:#0000ff; line-height:25px">g_outByteCount</span><span style="color:#333399; line-height:25px">大于等于</span><span style="color:#0000ff; line-height:25px">g_logRotateSizeKBytes</span><span style="color:#333399; line-height:25px">时,就要将logfile.2的内容移至logfile.3中,同时将logfile.1的内容移至logfile.2中,同时logfle的内容移至logfile.1中,再重新打开logfile文件进入后续输入。这样做的作用是不至于使得日志文件变得越来越来大,以至于占用过多的磁盘空间,而是只在磁盘上保存一定量的最新的日志记录。这样,旧的日志记录就会可能被新的日志记录所覆盖。默认的</span><span style="line-height:25px; color:rgb(0,0,255)">g_logRotateSizeKBytes</span><span style="color:#003366; line-height:25px">为</span><span style="line-height:25px; color:rgb(0,0,255)">16k</span><span style="color:#000080; line-height:25px">,可以通过</span><span style="color:#ff6600; line-height:25px">logcat</span><span style="color:#000080; line-height:25px">的</span><span style="color:#ff6600; line-height:25px">r</span><span style="color:#000080; line-height:25px">参数指定大小。</span> </div> <div style="line-height:25px"><span style="color:#000080; line-height:25px"><br style="line-height:25px"></span></div> <div style="line-height:25px"><span style="color:#003366; line-height:25px">对logcat源码的分析到此结束了!</span></div> </div> </wbr>