logcat源码分析(六)

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

对logcat源码的分析到此结束了!
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值