From 747781433fb01f745529c7e9dd97c5599070ad0d Mon Sep 17 00:00:00 2001 From: Tianjie Xu Date: Fri, 5 Aug 2016 18:00:04 -0700 Subject: Switch recovery to libbase logging Clean up the recovery image and switch to libbase logging. Bug: 28191554 Change-Id: Icd999c3cc832f0639f204b5c36cea8afe303ad35 --- recovery.cpp | 99 ++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 60 insertions(+), 39 deletions(-) (limited to 'recovery.cpp') diff --git a/recovery.cpp b/recovery.cpp index eabae68e0..d3f9c47ae 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -41,6 +41,7 @@ #include #include /* Android Log Priority Tags */ #include +#include #include #include #include @@ -49,7 +50,6 @@ #include #include #include -#include /* Android Log packet format */ #include /* private pmsg functions */ #include #include @@ -191,7 +191,7 @@ static const int MAX_ARGS = 100; // open a given path, mounting partitions as necessary FILE* fopen_path(const char *path, const char *mode) { if (ensure_path_mounted(path) != 0) { - LOGE("Can't mount %s\n", path); + LOG(ERROR) << "Can't mount " << path; return NULL; } @@ -206,7 +206,9 @@ FILE* fopen_path(const char *path, const char *mode) { // close a file, log an error if the error indicator is set static void check_and_fclose(FILE *fp, const char *name) { fflush(fp); - if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno)); + if (ferror(fp)) { + PLOG(ERROR) << "Error in " << name; + } fclose(fp); } @@ -218,7 +220,7 @@ bool is_ro_debuggable() { static void redirect_stdio(const char* filename) { int pipefd[2]; if (pipe(pipefd) == -1) { - LOGE("pipe failed: %s\n", strerror(errno)); + PLOG(ERROR) << "pipe failed"; // Fall back to traditional logging mode without timestamps. // If these fail, there's not really anywhere to complain... @@ -230,7 +232,7 @@ static void redirect_stdio(const char* filename) { pid_t pid = fork(); if (pid == -1) { - LOGE("fork failed: %s\n", strerror(errno)); + PLOG(ERROR) << "fork failed"; // Fall back to traditional logging mode without timestamps. // If these fail, there's not really anywhere to complain... @@ -249,14 +251,14 @@ static void redirect_stdio(const char* filename) { // Child logger to actually write to the log file. FILE* log_fp = fopen(filename, "a"); if (log_fp == nullptr) { - LOGE("fopen \"%s\" failed: %s\n", filename, strerror(errno)); + PLOG(ERROR) << "fopen \"" << filename << "\" failed"; close(pipefd[0]); _exit(1); } FILE* pipe_fp = fdopen(pipefd[0], "r"); if (pipe_fp == nullptr) { - LOGE("fdopen failed: %s\n", strerror(errno)); + PLOG(ERROR) << "fdopen failed"; check_and_fclose(log_fp, filename); close(pipefd[0]); _exit(1); @@ -276,7 +278,7 @@ static void redirect_stdio(const char* filename) { fflush(log_fp); } - LOGE("getline failed: %s\n", strerror(errno)); + PLOG(ERROR) << "getline failed"; free(line); check_and_fclose(log_fp, filename); @@ -291,10 +293,10 @@ static void redirect_stdio(const char* filename) { setbuf(stderr, nullptr); if (dup2(pipefd[1], STDOUT_FILENO) == -1) { - LOGE("dup2 stdout failed: %s\n", strerror(errno)); + PLOG(ERROR) << "dup2 stdout failed"; } if (dup2(pipefd[1], STDERR_FILENO) == -1) { - LOGE("dup2 stderr failed: %s\n", strerror(errno)); + PLOG(ERROR) << "dup2 stderr failed"; } close(pipefd[1]); @@ -310,18 +312,20 @@ get_args(int *argc, char ***argv) { bootloader_message boot = {}; std::string err; if (!read_bootloader_message(&boot, &err)) { - LOGE("%s\n", err.c_str()); + LOG(ERROR) << err; // If fails, leave a zeroed bootloader_message. memset(&boot, 0, sizeof(boot)); } stage = strndup(boot.stage, sizeof(boot.stage)); if (boot.command[0] != 0 && boot.command[0] != 255) { - LOGI("Boot command: %.*s\n", (int)sizeof(boot.command), boot.command); + std::string boot_command = std::string(boot.command, sizeof(boot.command)); + LOG(INFO) << "Boot command: " << boot_command; } if (boot.status[0] != 0 && boot.status[0] != 255) { - LOGI("Boot status: %.*s\n", (int)sizeof(boot.status), boot.status); + std::string boot_status = std::string(boot.status, sizeof(boot.status)); + LOG(INFO) << "Boot status: " << boot_status; } // --- if arguments weren't supplied, look in the bootloader control block @@ -335,9 +339,10 @@ get_args(int *argc, char ***argv) { if ((arg = strtok(NULL, "\n")) == NULL) break; (*argv)[*argc] = strdup(arg); } - LOGI("Got arguments from boot message\n"); + LOG(INFO) << "Got arguments from boot message"; } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) { - LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery); + std::string boot_recovery = std::string(boot.recovery, 20); + LOG(ERROR) << "Bad boot message\n" << "\"" <WasTextEverVisible()) { continue; } else { - LOGI("timed out waiting for key input; rebooting.\n"); + LOG(INFO) << "timed out waiting for key input; rebooting."; ui->EndMenu(); return 0; // XXX fixme } @@ -729,7 +734,7 @@ static char* browse_directory(const char* path, Device* device) { DIR* d = opendir(path); if (d == NULL) { - LOGE("error opening %s: %s\n", path, strerror(errno)); + PLOG(ERROR) << "error opening " << path; return NULL; } @@ -874,13 +879,13 @@ static bool wipe_cache(bool should_confirm, Device* device) { static bool secure_wipe_partition(const std::string& partition) { android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(partition.c_str(), O_WRONLY))); if (fd == -1) { - LOGE("failed to open \"%s\": %s\n", partition.c_str(), strerror(errno)); + PLOG(ERROR) << "failed to open \"" << partition << "\""; return false; } uint64_t range[2] = {0, 0}; if (ioctl(fd, BLKGETSIZE64, &range[1]) == -1 || range[1] == 0) { - LOGE("failed to get partition size: %s\n", strerror(errno)); + PLOG(ERROR) << "failed to get partition size"; return false; } printf("Secure-wiping \"%s\" from %" PRIu64 " to %" PRIu64 ".\n", @@ -916,18 +921,18 @@ static bool secure_wipe_partition(const std::string& partition) { // 2. check metadata (ota-type, pre-device and serial number if having one). static bool check_wipe_package(size_t wipe_package_size) { if (wipe_package_size == 0) { - LOGE("wipe_package_size is zero.\n"); + LOG(ERROR) << "wipe_package_size is zero"; return false; } std::string wipe_package; std::string err_str; if (!read_wipe_package(&wipe_package, wipe_package_size, &err_str)) { - LOGE("Failed to read wipe package: %s\n", err_str.c_str()); + PLOG(ERROR) << "Failed to read wipe package"; return false; } if (!verify_package(reinterpret_cast(wipe_package.data()), wipe_package.size())) { - LOGE("Failed to verify package.\n"); + LOG(ERROR) << "Failed to verify package"; return false; } @@ -936,7 +941,7 @@ static bool check_wipe_package(size_t wipe_package_size) { int err = mzOpenZipArchive(reinterpret_cast(&wipe_package[0]), wipe_package.size(), &zip); if (err != 0) { - LOGE("Can't open wipe package: %s\n", err != -1 ? strerror(err) : "bad"); + LOG(ERROR) << "Can't open wipe package"; return false; } std::string metadata; @@ -978,12 +983,12 @@ static bool wipe_ab_device(size_t wipe_package_size) { ui->SetProgressType(RecoveryUI::INDETERMINATE); if (!check_wipe_package(wipe_package_size)) { - LOGE("Failed to verify wipe package\n"); + LOG(ERROR) << "Failed to verify wipe package"; return false; } std::string partition_list; if (!android::base::ReadFileToString(RECOVERY_WIPE, &partition_list)) { - LOGE("failed to read \"%s\".\n", RECOVERY_WIPE); + LOG(ERROR) << "failed to read \"" << RECOVERY_WIPE << "\""; return false; } @@ -1146,7 +1151,7 @@ static int apply_from_sdcard(Device* device, bool* wipe_cache) { sleep(1); continue; } else { - LOGE("Timed out waiting for the fuse-provided package.\n"); + LOG(ERROR) << "Timed out waiting for the fuse-provided package."; result = INSTALL_ERROR; kill(child, SIGKILL); break; @@ -1168,7 +1173,7 @@ static int apply_from_sdcard(Device* device, bool* wipe_cache) { } if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { - LOGE("Error exit from the fuse process: %d\n", WEXITSTATUS(status)); + LOG(ERROR) << "Error exit from the fuse process: " << WEXITSTATUS(status); } ensure_path_unmounted(SDCARD_ROOT); @@ -1324,6 +1329,18 @@ ui_print(const char* format, ...) { } } +static constexpr char log_characters[] = "VDIWEF"; + +void UiLogger(android::base::LogId id, android::base::LogSeverity severity, + const char* tag, const char* file, unsigned int line, + const char* message) { + if (severity >= android::base::ERROR && gCurrentUI != NULL) { + gCurrentUI->Print("E:%s\n", message); + } else { + fprintf(stdout, "%c:%s\n", log_characters[severity], message); + } +} + static bool is_battery_ok() { struct healthd_config healthd_config = { .batteryStatusPath = android::String8(android::String8::kEmptyString), @@ -1400,7 +1417,7 @@ static void set_retry_bootloader_message(int retry_count, int argc, char** argv) } std::string err; if (!write_bootloader_message(boot, &err)) { - LOGE("%s\n", err.c_str()); + LOG(ERROR) << err; } } @@ -1424,7 +1441,7 @@ static void log_failure_code(ErrorCode code, const char *update_package) { fprintf(install_log, "error: %d\n", code); fclose(install_log); } else { - LOGE("failed to open last_install: %s\n", strerror(errno)); + PLOG(ERROR) << "failed to open last_install"; } } @@ -1479,6 +1496,10 @@ static ssize_t logrotate( } int main(int argc, char **argv) { + // We don't have logcat yet under recovery; so we'll print error on screen and + // log to stdout (which is redirected to recovery.log) as we used to do. + android::base::InitLogging(argv, &UiLogger); + // Take last pmsg contents and rewrite it to the current pmsg session. static const char filter[] = "recovery/"; // Do we need to rotate? @@ -1564,7 +1585,7 @@ int main(int argc, char **argv) { break; } case '?': - LOGE("Invalid command argument\n"); + LOG(ERROR) << "Invalid command argument"; continue; } } -- cgit v1.2.3