From 245c5225880f763a31e9ac0fd42dee2cc42cfc9d Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Wed, 18 Oct 2017 11:06:36 -0700 Subject: rotate_logs: Clean up the header includes. Also clang-format rotate_logs.cpp to get consistent style. Test: mmma bootable/recovery Test: recovery_manual_test works on marlin. Change-Id: I1210a346b97bbf0e1a8c3f8e3e73a7c114858ca8 --- rotate_logs.cpp | 120 ++++++++++++++++++++++++++------------------------------ rotate_logs.h | 22 ++++------- 2 files changed, 64 insertions(+), 78 deletions(-) diff --git a/rotate_logs.cpp b/rotate_logs.cpp index fc220215e..da008792c 100644 --- a/rotate_logs.cpp +++ b/rotate_logs.cpp @@ -31,85 +31,77 @@ static const std::string LAST_KMSG_FILTER = "recovery/last_kmsg"; static const std::string LAST_LOG_FILTER = "recovery/last_log"; -ssize_t logbasename( - log_id_t /* logId */, - char /* prio */, - const char *filename, - const char * /* buf */, size_t len, - void *arg) { - bool* doRotate = static_cast(arg); - if (LAST_KMSG_FILTER.find(filename) != std::string::npos || - LAST_LOG_FILTER.find(filename) != std::string::npos) { - *doRotate = true; - } - return len; +ssize_t logbasename(log_id_t /* id */, char /* prio */, const char* filename, const char* /* buf */, + size_t len, void* arg) { + bool* do_rotate = static_cast(arg); + if (LAST_KMSG_FILTER.find(filename) != std::string::npos || + LAST_LOG_FILTER.find(filename) != std::string::npos) { + *do_rotate = true; + } + return len; } -ssize_t logrotate( - log_id_t logId, - char prio, - const char *filename, - const char *buf, size_t len, - void *arg) { - bool* doRotate = static_cast(arg); - if (!*doRotate) { - return __android_log_pmsg_file_write(logId, prio, filename, buf, len); - } +ssize_t logrotate(log_id_t id, char prio, const char* filename, const char* buf, size_t len, + void* arg) { + bool* do_rotate = static_cast(arg); + if (!*do_rotate) { + return __android_log_pmsg_file_write(id, prio, filename, buf, len); + } - std::string name(filename); - size_t dot = name.find_last_of('.'); - std::string sub = name.substr(0, dot); + std::string name(filename); + size_t dot = name.find_last_of('.'); + std::string sub = name.substr(0, dot); - if (LAST_KMSG_FILTER.find(sub) == std::string::npos && - LAST_LOG_FILTER.find(sub) == std::string::npos) { - return __android_log_pmsg_file_write(logId, prio, filename, buf, len); - } + if (LAST_KMSG_FILTER.find(sub) == std::string::npos && + LAST_LOG_FILTER.find(sub) == std::string::npos) { + return __android_log_pmsg_file_write(id, prio, filename, buf, len); + } - // filename rotation - if (dot == std::string::npos) { - name += ".1"; + // filename rotation + if (dot == std::string::npos) { + name += ".1"; + } else { + std::string number = name.substr(dot + 1); + if (!isdigit(number[0])) { + name += ".1"; } else { - std::string number = name.substr(dot + 1); - if (!isdigit(number[0])) { - name += ".1"; - } else { - size_t i; - if (!android::base::ParseUint(number, &i)) { - LOG(ERROR) << "failed to parse uint in " << number; - return -1; - } - name = sub + "." + std::to_string(i + 1); - } + size_t i; + if (!android::base::ParseUint(number, &i)) { + LOG(ERROR) << "failed to parse uint in " << number; + return -1; + } + name = sub + "." + std::to_string(i + 1); } + } - return __android_log_pmsg_file_write(logId, prio, name.c_str(), buf, len); + return __android_log_pmsg_file_write(id, prio, name.c_str(), buf, len); } // Rename last_log -> last_log.1 -> last_log.2 -> ... -> last_log.$max. // Similarly rename last_kmsg -> last_kmsg.1 -> ... -> last_kmsg.$max. // Overwrite any existing last_log.$max and last_kmsg.$max. void rotate_logs(const char* last_log_file, const char* last_kmsg_file) { - // Logs should only be rotated once. - static bool rotated = false; - if (rotated) { - return; - } - rotated = true; + // Logs should only be rotated once. + static bool rotated = false; + if (rotated) { + return; + } + rotated = true; - for (int i = KEEP_LOG_COUNT - 1; i >= 0; --i) { - std::string old_log = android::base::StringPrintf("%s", last_log_file); - if (i > 0) { - old_log += "." + std::to_string(i); - } - std::string new_log = android::base::StringPrintf("%s.%d", last_log_file, i+1); - // Ignore errors if old_log doesn't exist. - rename(old_log.c_str(), new_log.c_str()); + for (int i = KEEP_LOG_COUNT - 1; i >= 0; --i) { + std::string old_log = android::base::StringPrintf("%s", last_log_file); + if (i > 0) { + old_log += "." + std::to_string(i); + } + std::string new_log = android::base::StringPrintf("%s.%d", last_log_file, i + 1); + // Ignore errors if old_log doesn't exist. + rename(old_log.c_str(), new_log.c_str()); - std::string old_kmsg = android::base::StringPrintf("%s", last_kmsg_file); - if (i > 0) { - old_kmsg += "." + std::to_string(i); - } - std::string new_kmsg = android::base::StringPrintf("%s.%d", last_kmsg_file, i+1); - rename(old_kmsg.c_str(), new_kmsg.c_str()); + std::string old_kmsg = android::base::StringPrintf("%s", last_kmsg_file); + if (i > 0) { + old_kmsg += "." + std::to_string(i); } + std::string new_kmsg = android::base::StringPrintf("%s.%d", last_kmsg_file, i + 1); + rename(old_kmsg.c_str(), new_kmsg.c_str()); + } } diff --git a/rotate_logs.h b/rotate_logs.h index 809c213b6..007c33d44 100644 --- a/rotate_logs.h +++ b/rotate_logs.h @@ -17,24 +17,18 @@ #ifndef _ROTATE_LOGS_H #define _ROTATE_LOGS_H -#include +#include +#include -#include /* private pmsg functions */ +#include -constexpr int KEEP_LOG_COUNT = 10; +static constexpr int KEEP_LOG_COUNT = 10; -ssize_t logbasename(log_id_t /* logId */, - char /* prio */, - const char *filename, - const char * /* buf */, size_t len, - void *arg); +ssize_t logbasename(log_id_t id, char prio, const char* filename, const char* buf, size_t len, + void* arg); -ssize_t logrotate( - log_id_t logId, - char prio, - const char *filename, - const char *buf, size_t len, - void *arg); +ssize_t logrotate(log_id_t id, char prio, const char* filename, const char* buf, size_t len, + void* arg); // Rename last_log -> last_log.1 -> last_log.2 -> ... -> last_log.$max. // Similarly rename last_kmsg -> last_kmsg.1 -> ... -> last_kmsg.$max. -- cgit v1.2.3