首先,要说明的是,我想知道或得到recovery过长的所有详细log信息。
但是log 不能保存到/data 或者 /cache ,又不能保存到/tmp目录下,
因为下次开机的时候,/tmp目录的内容又会清空。
所以只能格式化之后,factory reset 彻底成功后,才把log 从/tmp 拷贝到 /cache下。
// 临时目录
static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
// 所有log
static const char *LOG_FILE = "/cache/recovery/log";
// 最后一部分log
static const char *LAST_LOG_FILE = "/cache/recovery/last_log";
// factory reset 彻底成功后,会执行
static void finish_recovery(const char *send_intent) {
{
// Copy logs to cache so the system can find out what happened.
copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true); //追加
copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false); //直接覆盖
copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, false);
...........................
}
// 拷贝文件的实现
static void
copy_log_file(const char* source, const char* destination, int append) {
FILE *log = fopen_path(destination, append ? "a" : "w");
if (log == NULL) {
LOGE("Can't open %s\n", destination);
} else {
FILE *tmplog = fopen(source, "r");
if (tmplog != NULL) {
if (append) {
fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
}
char buf[4096];
while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
if (append) {
tmplog_offset = ftell(tmplog);
}
check_and_fclose(tmplog, source);
}
check_and_fclose(log, destination);
}
}