Merge "rotate_logs: Clean up the header includes." am: 30ec005104 am: c8eba4a036

am: 41763a5dbb

Change-Id: I257ece1d0df06c45faac1501c8cc3c13da43a3db
This commit is contained in:
Tao Bao
2017-10-18 20:38:58 +00:00
committed by android-build-merger
2 changed files with 65 additions and 79 deletions
+57 -65
View File
@@ -31,85 +31,77 @@
static const std::string LAST_KMSG_FILTER = "recovery/last_kmsg"; static const std::string LAST_KMSG_FILTER = "recovery/last_kmsg";
static const std::string LAST_LOG_FILTER = "recovery/last_log"; static const std::string LAST_LOG_FILTER = "recovery/last_log";
ssize_t logbasename( ssize_t logbasename(log_id_t /* id */, char /* prio */, const char* filename, const char* /* buf */,
log_id_t /* logId */, size_t len, void* arg) {
char /* prio */, bool* do_rotate = static_cast<bool*>(arg);
const char *filename, if (LAST_KMSG_FILTER.find(filename) != std::string::npos ||
const char * /* buf */, size_t len, LAST_LOG_FILTER.find(filename) != std::string::npos) {
void *arg) { *do_rotate = true;
bool* doRotate = static_cast<bool*>(arg); }
if (LAST_KMSG_FILTER.find(filename) != std::string::npos || return len;
LAST_LOG_FILTER.find(filename) != std::string::npos) {
*doRotate = true;
}
return len;
} }
ssize_t logrotate( ssize_t logrotate(log_id_t id, char prio, const char* filename, const char* buf, size_t len,
log_id_t logId, void* arg) {
char prio, bool* do_rotate = static_cast<bool*>(arg);
const char *filename, if (!*do_rotate) {
const char *buf, size_t len, return __android_log_pmsg_file_write(id, prio, filename, buf, 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); std::string name(filename);
size_t dot = name.find_last_of('.'); size_t dot = name.find_last_of('.');
std::string sub = name.substr(0, dot); std::string sub = name.substr(0, dot);
if (LAST_KMSG_FILTER.find(sub) == std::string::npos && if (LAST_KMSG_FILTER.find(sub) == std::string::npos &&
LAST_LOG_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); return __android_log_pmsg_file_write(id, prio, filename, buf, len);
} }
// filename rotation // filename rotation
if (dot == std::string::npos) { if (dot == std::string::npos) {
name += ".1"; name += ".1";
} else {
std::string number = name.substr(dot + 1);
if (!isdigit(number[0])) {
name += ".1";
} else { } else {
std::string number = name.substr(dot + 1); size_t i;
if (!isdigit(number[0])) { if (!android::base::ParseUint(number, &i)) {
name += ".1"; LOG(ERROR) << "failed to parse uint in " << number;
} else { return -1;
size_t i; }
if (!android::base::ParseUint(number, &i)) { name = sub + "." + std::to_string(i + 1);
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. // Rename last_log -> last_log.1 -> last_log.2 -> ... -> last_log.$max.
// Similarly rename last_kmsg -> last_kmsg.1 -> ... -> last_kmsg.$max. // Similarly rename last_kmsg -> last_kmsg.1 -> ... -> last_kmsg.$max.
// Overwrite any existing last_log.$max and 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) { void rotate_logs(const char* last_log_file, const char* last_kmsg_file) {
// Logs should only be rotated once. // Logs should only be rotated once.
static bool rotated = false; static bool rotated = false;
if (rotated) { if (rotated) {
return; return;
} }
rotated = true; rotated = true;
for (int i = KEEP_LOG_COUNT - 1; i >= 0; --i) { for (int i = KEEP_LOG_COUNT - 1; i >= 0; --i) {
std::string old_log = android::base::StringPrintf("%s", last_log_file); std::string old_log = android::base::StringPrintf("%s", last_log_file);
if (i > 0) { if (i > 0) {
old_log += "." + std::to_string(i); 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 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());
}
} }
+8 -14
View File
@@ -17,24 +17,18 @@
#ifndef _ROTATE_LOGS_H #ifndef _ROTATE_LOGS_H
#define _ROTATE_LOGS_H #define _ROTATE_LOGS_H
#include <string> #include <stddef.h>
#include <sys/types.h>
#include <private/android_logger.h> /* private pmsg functions */ #include <log/log_id.h>
constexpr int KEEP_LOG_COUNT = 10; static constexpr int KEEP_LOG_COUNT = 10;
ssize_t logbasename(log_id_t /* logId */, ssize_t logbasename(log_id_t id, char prio, const char* filename, const char* buf, size_t len,
char /* prio */, void* arg);
const char *filename,
const char * /* buf */, size_t len,
void *arg);
ssize_t logrotate( ssize_t logrotate(log_id_t id, char prio, const char* filename, const char* buf, size_t len,
log_id_t logId, void* arg);
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. // Rename last_log -> last_log.1 -> last_log.2 -> ... -> last_log.$max.
// Similarly rename last_kmsg -> last_kmsg.1 -> ... -> last_kmsg.$max. // Similarly rename last_kmsg -> last_kmsg.1 -> ... -> last_kmsg.$max.