Loading Android.mk +8 −3 Original line number Diff line number Diff line Loading @@ -46,6 +46,7 @@ LOCAL_SRC_FILES := \ install.cpp \ recovery.cpp \ roots.cpp \ rotate_logs.cpp \ screen_ui.cpp \ ui.cpp \ verifier.cpp \ Loading Loading @@ -119,7 +120,9 @@ include $(BUILD_EXECUTABLE) # recovery-persist (system partition dynamic executable run after /data mounts) # =============================== include $(CLEAR_VARS) LOCAL_SRC_FILES := recovery-persist.cpp LOCAL_SRC_FILES := \ recovery-persist.cpp \ rotate_logs.cpp LOCAL_MODULE := recovery-persist LOCAL_SHARED_LIBRARIES := liblog libbase LOCAL_CFLAGS := -Werror Loading @@ -129,9 +132,11 @@ include $(BUILD_EXECUTABLE) # recovery-refresh (system partition dynamic executable run at init) # =============================== include $(CLEAR_VARS) LOCAL_SRC_FILES := recovery-refresh.cpp LOCAL_SRC_FILES := \ recovery-refresh.cpp \ rotate_logs.cpp LOCAL_MODULE := recovery-refresh LOCAL_SHARED_LIBRARIES := liblog LOCAL_SHARED_LIBRARIES := liblog libbase LOCAL_CFLAGS := -Werror LOCAL_INIT_RC := recovery-refresh.rc include $(BUILD_EXECUTABLE) Loading recovery-persist.cpp +4 −38 Original line number Diff line number Diff line Loading @@ -30,7 +30,6 @@ // --force-persist ignore /cache mount, always rotate in the contents. // #include <errno.h> #include <stdio.h> #include <stdlib.h> #include <string.h> Loading @@ -40,17 +39,16 @@ #include <android-base/file.h> #include <android-base/logging.h> #include <private/android_logger.h> /* private pmsg functions */ #include "rotate_logs.h" static const char *LAST_LOG_FILE = "/data/misc/recovery/last_log"; static const char *LAST_PMSG_FILE = "/sys/fs/pstore/pmsg-ramoops-0"; static const char *LAST_KMSG_FILE = "/data/misc/recovery/last_kmsg"; static const char *LAST_CONSOLE_FILE = "/sys/fs/pstore/console-ramoops-0"; static const char *ALT_LAST_CONSOLE_FILE = "/sys/fs/pstore/console-ramoops"; static const int KEEP_LOG_COUNT = 10; // close a file, log an error if the error indicator is set static void check_and_fclose(FILE *fp, const char *name) { fflush(fp); Loading Loading @@ -80,39 +78,6 @@ static void copy_file(const char* source, const char* destination) { static bool rotated = false; // 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. static void rotate_logs(int max) { // Logs should only be rotated once. if (rotated) { return; } rotated = true; for (int i = max-1; i >= 0; --i) { std::string old_log(LAST_LOG_FILE); if (i > 0) { old_log += "." + std::to_string(i); } std::string new_log(LAST_LOG_FILE); new_log += "." + std::to_string(i+1); // Ignore errors if old_log doesn't exist. rename(old_log.c_str(), new_log.c_str()); std::string old_kmsg(LAST_KMSG_FILE); if (i > 0) { old_kmsg += "." + std::to_string(i); } std::string new_kmsg(LAST_KMSG_FILE); new_kmsg += "." + std::to_string(i+1); rename(old_kmsg.c_str(), new_kmsg.c_str()); } } ssize_t logsave( log_id_t /* logId */, char /* prio */, Loading @@ -138,7 +103,8 @@ ssize_t logsave( // already-rotated files? Algorithm thus far is KISS: one file, // one rotation allowed. rotate_logs(KEEP_LOG_COUNT); rotate_logs(LAST_LOG_FILE, LAST_KMSG_FILE); rotated = true; return android::base::WriteStringToFile(buffer, destination.c_str()); } Loading recovery-refresh.cpp +1 −57 Original line number Diff line number Diff line Loading @@ -14,8 +14,6 @@ * limitations under the License. */ #define LOG_TAG "recovery-refresh" // // Strictly to deal with reboot into system after OTA, then // reboot while in system before boot complete landing us back Loading @@ -40,64 +38,11 @@ // #include <string.h> #include <string> #include <android/log.h> /* Android Log Priority Tags */ #include <private/android_logger.h> /* private pmsg functions */ static const char LAST_KMSG_FILE[] = "recovery/last_kmsg"; static const char LAST_LOG_FILE[] = "recovery/last_log"; static ssize_t logbasename( log_id_t /* logId */, char /* prio */, const char *filename, const char * /* buf */, size_t len, void *arg) { if (strstr(LAST_KMSG_FILE, filename) || strstr(LAST_LOG_FILE, filename)) { bool *doRotate = reinterpret_cast<bool *>(arg); *doRotate = true; } return len; } static ssize_t logrotate( log_id_t logId, char prio, const char *filename, const char *buf, size_t len, void *arg) { bool *doRotate = reinterpret_cast<bool *>(arg); if (!*doRotate) { return __android_log_pmsg_file_write(logId, prio, filename, buf, len); } std::string name(filename); size_t dot = name.find_last_of('.'); std::string sub = name.substr(0, dot); if (!strstr(LAST_KMSG_FILE, sub.c_str()) && !strstr(LAST_LOG_FILE, sub.c_str())) { return __android_log_pmsg_file_write(logId, prio, filename, buf, len); } // filename rotation if (dot == std::string::npos) { name += ".1"; } else { std::string number = name.substr(dot + 1); if (!isdigit(number.data()[0])) { name += ".1"; } else { auto i = std::stoull(number); name = sub + "." + std::to_string(i + 1); } } return __android_log_pmsg_file_write(logId, prio, name.c_str(), buf, len); } #include "rotate_logs.h" int main(int argc, char **argv) { static const char filter[] = "recovery/"; Loading @@ -105,7 +50,6 @@ int main(int argc, char **argv) { static const char rotate_flag[] = "--rotate"; ssize_t ret; bool doRotate = false; // Take last pmsg contents and rewrite it to the current pmsg session. if ((argc <= 1) || !argv[1] || (((doRotate = strcmp(argv[1], rotate_flag))) && Loading recovery.cpp +6 −84 Original line number Diff line number Diff line Loading @@ -66,8 +66,9 @@ #include "minui/minui.h" #include "otautil/DirUtil.h" #include "roots.h" #include "ui.h" #include "rotate_logs.h" #include "screen_ui.h" #include "ui.h" static const struct option OPTIONS[] = { { "update_package", required_argument, NULL, 'u' }, Loading Loading @@ -108,7 +109,6 @@ static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log"; static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install"; static const char *LAST_KMSG_FILE = "/cache/recovery/last_kmsg"; static const char *LAST_LOG_FILE = "/cache/recovery/last_log"; static const int KEEP_LOG_COUNT = 10; // We will try to apply the update package 5 times at most in case of an I/O error. static const int EIO_RETRY_COUNT = 4; static const int BATTERY_READ_TIMEOUT_IN_SEC = 10; Loading Loading @@ -451,37 +451,6 @@ static void copy_log_file(const char* source, const char* destination, bool appe } } // 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. static void rotate_logs(int max) { // Logs should only be rotated once. static bool rotated = false; if (rotated) { return; } rotated = true; ensure_path_mounted(LAST_LOG_FILE); ensure_path_mounted(LAST_KMSG_FILE); for (int i = max-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()); } } static void copy_logs() { // We only rotate and record the log of the current session if there are // actual attempts to modify the flash, such as wipes, installs from BCB Loading @@ -500,7 +469,9 @@ static void copy_logs() { return; } rotate_logs(KEEP_LOG_COUNT); ensure_path_mounted(LAST_LOG_FILE); ensure_path_mounted(LAST_KMSG_FILE); rotate_logs(LAST_LOG_FILE, LAST_KMSG_FILE); // Copy logs to cache so the system can find out what happened. copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true); Loading Loading @@ -1418,56 +1389,6 @@ static void log_failure_code(ErrorCode code, const char *update_package) { LOG(INFO) << log_content; } static ssize_t logbasename( log_id_t /* logId */, char /* prio */, const char *filename, const char * /* buf */, size_t len, void *arg) { if (strstr(LAST_KMSG_FILE, filename) || strstr(LAST_LOG_FILE, filename)) { bool *doRotate = reinterpret_cast<bool *>(arg); *doRotate = true; } return len; } static ssize_t logrotate( log_id_t logId, char prio, const char *filename, const char *buf, size_t len, void *arg) { bool *doRotate = reinterpret_cast<bool *>(arg); if (!*doRotate) { return __android_log_pmsg_file_write(logId, prio, filename, buf, len); } std::string name(filename); size_t dot = name.find_last_of('.'); std::string sub = name.substr(0, dot); if (!strstr(LAST_KMSG_FILE, sub.c_str()) && !strstr(LAST_LOG_FILE, sub.c_str())) { return __android_log_pmsg_file_write(logId, prio, filename, buf, len); } // filename rotation if (dot == std::string::npos) { name += ".1"; } else { std::string number = name.substr(dot + 1); if (!isdigit(number.data()[0])) { name += ".1"; } else { auto i = std::stoull(number); name = sub + "." + std::to_string(i + 1); } } return __android_log_pmsg_file_write(logId, prio, name.c_str(), buf, len); } 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. Loading @@ -1477,6 +1398,7 @@ int main(int argc, char **argv) { static const char filter[] = "recovery/"; // Do we need to rotate? bool doRotate = false; __android_log_pmsg_file_read( LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter, logbasename, &doRotate); Loading rotate_logs.cpp 0 → 100644 +115 −0 Original line number Diff line number Diff line /* * Copyright (C) 2016 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "rotate_logs.h" #include <stdio.h> #include <string.h> #include <sys/types.h> #include <string> #include <android-base/file.h> #include <android-base/logging.h> #include <android-base/parseint.h> #include <android-base/stringprintf.h> #include <private/android_logger.h> /* private pmsg functions */ 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<bool*>(arg); if (LAST_KMSG_FILTER.find(filename) != std::string::npos || LAST_LOG_FILTER.find(filename) != std::string::npos) { *doRotate = 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<bool*>(arg); if (!*doRotate) { return __android_log_pmsg_file_write(logId, prio, filename, buf, len); } 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); } // filename rotation if (dot == std::string::npos) { 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); } } return __android_log_pmsg_file_write(logId, 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; 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()); } } Loading
Android.mk +8 −3 Original line number Diff line number Diff line Loading @@ -46,6 +46,7 @@ LOCAL_SRC_FILES := \ install.cpp \ recovery.cpp \ roots.cpp \ rotate_logs.cpp \ screen_ui.cpp \ ui.cpp \ verifier.cpp \ Loading Loading @@ -119,7 +120,9 @@ include $(BUILD_EXECUTABLE) # recovery-persist (system partition dynamic executable run after /data mounts) # =============================== include $(CLEAR_VARS) LOCAL_SRC_FILES := recovery-persist.cpp LOCAL_SRC_FILES := \ recovery-persist.cpp \ rotate_logs.cpp LOCAL_MODULE := recovery-persist LOCAL_SHARED_LIBRARIES := liblog libbase LOCAL_CFLAGS := -Werror Loading @@ -129,9 +132,11 @@ include $(BUILD_EXECUTABLE) # recovery-refresh (system partition dynamic executable run at init) # =============================== include $(CLEAR_VARS) LOCAL_SRC_FILES := recovery-refresh.cpp LOCAL_SRC_FILES := \ recovery-refresh.cpp \ rotate_logs.cpp LOCAL_MODULE := recovery-refresh LOCAL_SHARED_LIBRARIES := liblog LOCAL_SHARED_LIBRARIES := liblog libbase LOCAL_CFLAGS := -Werror LOCAL_INIT_RC := recovery-refresh.rc include $(BUILD_EXECUTABLE) Loading
recovery-persist.cpp +4 −38 Original line number Diff line number Diff line Loading @@ -30,7 +30,6 @@ // --force-persist ignore /cache mount, always rotate in the contents. // #include <errno.h> #include <stdio.h> #include <stdlib.h> #include <string.h> Loading @@ -40,17 +39,16 @@ #include <android-base/file.h> #include <android-base/logging.h> #include <private/android_logger.h> /* private pmsg functions */ #include "rotate_logs.h" static const char *LAST_LOG_FILE = "/data/misc/recovery/last_log"; static const char *LAST_PMSG_FILE = "/sys/fs/pstore/pmsg-ramoops-0"; static const char *LAST_KMSG_FILE = "/data/misc/recovery/last_kmsg"; static const char *LAST_CONSOLE_FILE = "/sys/fs/pstore/console-ramoops-0"; static const char *ALT_LAST_CONSOLE_FILE = "/sys/fs/pstore/console-ramoops"; static const int KEEP_LOG_COUNT = 10; // close a file, log an error if the error indicator is set static void check_and_fclose(FILE *fp, const char *name) { fflush(fp); Loading Loading @@ -80,39 +78,6 @@ static void copy_file(const char* source, const char* destination) { static bool rotated = false; // 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. static void rotate_logs(int max) { // Logs should only be rotated once. if (rotated) { return; } rotated = true; for (int i = max-1; i >= 0; --i) { std::string old_log(LAST_LOG_FILE); if (i > 0) { old_log += "." + std::to_string(i); } std::string new_log(LAST_LOG_FILE); new_log += "." + std::to_string(i+1); // Ignore errors if old_log doesn't exist. rename(old_log.c_str(), new_log.c_str()); std::string old_kmsg(LAST_KMSG_FILE); if (i > 0) { old_kmsg += "." + std::to_string(i); } std::string new_kmsg(LAST_KMSG_FILE); new_kmsg += "." + std::to_string(i+1); rename(old_kmsg.c_str(), new_kmsg.c_str()); } } ssize_t logsave( log_id_t /* logId */, char /* prio */, Loading @@ -138,7 +103,8 @@ ssize_t logsave( // already-rotated files? Algorithm thus far is KISS: one file, // one rotation allowed. rotate_logs(KEEP_LOG_COUNT); rotate_logs(LAST_LOG_FILE, LAST_KMSG_FILE); rotated = true; return android::base::WriteStringToFile(buffer, destination.c_str()); } Loading
recovery-refresh.cpp +1 −57 Original line number Diff line number Diff line Loading @@ -14,8 +14,6 @@ * limitations under the License. */ #define LOG_TAG "recovery-refresh" // // Strictly to deal with reboot into system after OTA, then // reboot while in system before boot complete landing us back Loading @@ -40,64 +38,11 @@ // #include <string.h> #include <string> #include <android/log.h> /* Android Log Priority Tags */ #include <private/android_logger.h> /* private pmsg functions */ static const char LAST_KMSG_FILE[] = "recovery/last_kmsg"; static const char LAST_LOG_FILE[] = "recovery/last_log"; static ssize_t logbasename( log_id_t /* logId */, char /* prio */, const char *filename, const char * /* buf */, size_t len, void *arg) { if (strstr(LAST_KMSG_FILE, filename) || strstr(LAST_LOG_FILE, filename)) { bool *doRotate = reinterpret_cast<bool *>(arg); *doRotate = true; } return len; } static ssize_t logrotate( log_id_t logId, char prio, const char *filename, const char *buf, size_t len, void *arg) { bool *doRotate = reinterpret_cast<bool *>(arg); if (!*doRotate) { return __android_log_pmsg_file_write(logId, prio, filename, buf, len); } std::string name(filename); size_t dot = name.find_last_of('.'); std::string sub = name.substr(0, dot); if (!strstr(LAST_KMSG_FILE, sub.c_str()) && !strstr(LAST_LOG_FILE, sub.c_str())) { return __android_log_pmsg_file_write(logId, prio, filename, buf, len); } // filename rotation if (dot == std::string::npos) { name += ".1"; } else { std::string number = name.substr(dot + 1); if (!isdigit(number.data()[0])) { name += ".1"; } else { auto i = std::stoull(number); name = sub + "." + std::to_string(i + 1); } } return __android_log_pmsg_file_write(logId, prio, name.c_str(), buf, len); } #include "rotate_logs.h" int main(int argc, char **argv) { static const char filter[] = "recovery/"; Loading @@ -105,7 +50,6 @@ int main(int argc, char **argv) { static const char rotate_flag[] = "--rotate"; ssize_t ret; bool doRotate = false; // Take last pmsg contents and rewrite it to the current pmsg session. if ((argc <= 1) || !argv[1] || (((doRotate = strcmp(argv[1], rotate_flag))) && Loading
recovery.cpp +6 −84 Original line number Diff line number Diff line Loading @@ -66,8 +66,9 @@ #include "minui/minui.h" #include "otautil/DirUtil.h" #include "roots.h" #include "ui.h" #include "rotate_logs.h" #include "screen_ui.h" #include "ui.h" static const struct option OPTIONS[] = { { "update_package", required_argument, NULL, 'u' }, Loading Loading @@ -108,7 +109,6 @@ static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log"; static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install"; static const char *LAST_KMSG_FILE = "/cache/recovery/last_kmsg"; static const char *LAST_LOG_FILE = "/cache/recovery/last_log"; static const int KEEP_LOG_COUNT = 10; // We will try to apply the update package 5 times at most in case of an I/O error. static const int EIO_RETRY_COUNT = 4; static const int BATTERY_READ_TIMEOUT_IN_SEC = 10; Loading Loading @@ -451,37 +451,6 @@ static void copy_log_file(const char* source, const char* destination, bool appe } } // 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. static void rotate_logs(int max) { // Logs should only be rotated once. static bool rotated = false; if (rotated) { return; } rotated = true; ensure_path_mounted(LAST_LOG_FILE); ensure_path_mounted(LAST_KMSG_FILE); for (int i = max-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()); } } static void copy_logs() { // We only rotate and record the log of the current session if there are // actual attempts to modify the flash, such as wipes, installs from BCB Loading @@ -500,7 +469,9 @@ static void copy_logs() { return; } rotate_logs(KEEP_LOG_COUNT); ensure_path_mounted(LAST_LOG_FILE); ensure_path_mounted(LAST_KMSG_FILE); rotate_logs(LAST_LOG_FILE, LAST_KMSG_FILE); // Copy logs to cache so the system can find out what happened. copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true); Loading Loading @@ -1418,56 +1389,6 @@ static void log_failure_code(ErrorCode code, const char *update_package) { LOG(INFO) << log_content; } static ssize_t logbasename( log_id_t /* logId */, char /* prio */, const char *filename, const char * /* buf */, size_t len, void *arg) { if (strstr(LAST_KMSG_FILE, filename) || strstr(LAST_LOG_FILE, filename)) { bool *doRotate = reinterpret_cast<bool *>(arg); *doRotate = true; } return len; } static ssize_t logrotate( log_id_t logId, char prio, const char *filename, const char *buf, size_t len, void *arg) { bool *doRotate = reinterpret_cast<bool *>(arg); if (!*doRotate) { return __android_log_pmsg_file_write(logId, prio, filename, buf, len); } std::string name(filename); size_t dot = name.find_last_of('.'); std::string sub = name.substr(0, dot); if (!strstr(LAST_KMSG_FILE, sub.c_str()) && !strstr(LAST_LOG_FILE, sub.c_str())) { return __android_log_pmsg_file_write(logId, prio, filename, buf, len); } // filename rotation if (dot == std::string::npos) { name += ".1"; } else { std::string number = name.substr(dot + 1); if (!isdigit(number.data()[0])) { name += ".1"; } else { auto i = std::stoull(number); name = sub + "." + std::to_string(i + 1); } } return __android_log_pmsg_file_write(logId, prio, name.c_str(), buf, len); } 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. Loading @@ -1477,6 +1398,7 @@ int main(int argc, char **argv) { static const char filter[] = "recovery/"; // Do we need to rotate? bool doRotate = false; __android_log_pmsg_file_read( LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter, logbasename, &doRotate); Loading
rotate_logs.cpp 0 → 100644 +115 −0 Original line number Diff line number Diff line /* * Copyright (C) 2016 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "rotate_logs.h" #include <stdio.h> #include <string.h> #include <sys/types.h> #include <string> #include <android-base/file.h> #include <android-base/logging.h> #include <android-base/parseint.h> #include <android-base/stringprintf.h> #include <private/android_logger.h> /* private pmsg functions */ 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<bool*>(arg); if (LAST_KMSG_FILTER.find(filename) != std::string::npos || LAST_LOG_FILTER.find(filename) != std::string::npos) { *doRotate = 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<bool*>(arg); if (!*doRotate) { return __android_log_pmsg_file_write(logId, prio, filename, buf, len); } 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); } // filename rotation if (dot == std::string::npos) { 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); } } return __android_log_pmsg_file_write(logId, 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; 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()); } }