aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTao Bao2017-10-18 13:06:36 -0500
committerTao Bao2017-10-18 13:08:37 -0500
commit245c5225880f763a31e9ac0fd42dee2cc42cfc9d (patch)
tree263387efad391594209239cb153126d0fdc3f21a /rotate_logs.cpp
parentbab1e8efc87029c347fdf14dea6d154889695b18 (diff)
downloadplatform-bootable-recovery-245c5225880f763a31e9ac0fd42dee2cc42cfc9d.tar.gz
platform-bootable-recovery-245c5225880f763a31e9ac0fd42dee2cc42cfc9d.tar.xz
platform-bootable-recovery-245c5225880f763a31e9ac0fd42dee2cc42cfc9d.zip
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
Diffstat (limited to 'rotate_logs.cpp')
-rw-r--r--rotate_logs.cpp120
1 files changed, 56 insertions, 64 deletions
diff --git a/rotate_logs.cpp b/rotate_logs.cpp
index fc220215..da008792 100644
--- a/rotate_logs.cpp
+++ b/rotate_logs.cpp
@@ -31,85 +31,77 @@
31static const std::string LAST_KMSG_FILTER = "recovery/last_kmsg"; 31static const std::string LAST_KMSG_FILTER = "recovery/last_kmsg";
32static const std::string LAST_LOG_FILTER = "recovery/last_log"; 32static const std::string LAST_LOG_FILTER = "recovery/last_log";
33 33
34ssize_t logbasename( 34ssize_t logbasename(log_id_t /* id */, char /* prio */, const char* filename, const char* /* buf */,
35 log_id_t /* logId */, 35 size_t len, void* arg) {
36 char /* prio */, 36 bool* do_rotate = static_cast<bool*>(arg);
37 const char *filename, 37 if (LAST_KMSG_FILTER.find(filename) != std::string::npos ||
38 const char * /* buf */, size_t len, 38 LAST_LOG_FILTER.find(filename) != std::string::npos) {
39 void *arg) { 39 *do_rotate = true;
40 bool* doRotate = static_cast<bool*>(arg); 40 }
41 if (LAST_KMSG_FILTER.find(filename) != std::string::npos || 41 return len;
42 LAST_LOG_FILTER.find(filename) != std::string::npos) {
43 *doRotate = true;
44 }
45 return len;
46} 42}
47 43
48ssize_t logrotate( 44ssize_t logrotate(log_id_t id, char prio, const char* filename, const char* buf, size_t len,
49 log_id_t logId, 45 void* arg) {
50 char prio, 46 bool* do_rotate = static_cast<bool*>(arg);
51 const char *filename, 47 if (!*do_rotate) {
52 const char *buf, size_t len, 48 return __android_log_pmsg_file_write(id, prio, filename, buf, len);
53 void *arg) { 49 }
54 bool* doRotate = static_cast<bool*>(arg);
55 if (!*doRotate) {
56 return __android_log_pmsg_file_write(logId, prio, filename, buf, len);
57 }
58 50
59 std::string name(filename); 51 std::string name(filename);
60 size_t dot = name.find_last_of('.'); 52 size_t dot = name.find_last_of('.');
61 std::string sub = name.substr(0, dot); 53 std::string sub = name.substr(0, dot);
62 54
63 if (LAST_KMSG_FILTER.find(sub) == std::string::npos && 55 if (LAST_KMSG_FILTER.find(sub) == std::string::npos &&
64 LAST_LOG_FILTER.find(sub) == std::string::npos) { 56 LAST_LOG_FILTER.find(sub) == std::string::npos) {
65 return __android_log_pmsg_file_write(logId, prio, filename, buf, len); 57 return __android_log_pmsg_file_write(id, prio, filename, buf, len);
66 } 58 }
67 59
68 // filename rotation 60 // filename rotation
69 if (dot == std::string::npos) { 61 if (dot == std::string::npos) {
70 name += ".1"; 62 name += ".1";
63 } else {
64 std::string number = name.substr(dot + 1);
65 if (!isdigit(number[0])) {
66 name += ".1";
71 } else { 67 } else {
72 std::string number = name.substr(dot + 1); 68 size_t i;
73 if (!isdigit(number[0])) { 69 if (!android::base::ParseUint(number, &i)) {
74 name += ".1"; 70 LOG(ERROR) << "failed to parse uint in " << number;
75 } else { 71 return -1;
76 size_t i; 72 }
77 if (!android::base::ParseUint(number, &i)) { 73 name = sub + "." + std::to_string(i + 1);
78 LOG(ERROR) << "failed to parse uint in " << number;
79 return -1;
80 }
81 name = sub + "." + std::to_string(i + 1);
82 }
83 } 74 }
75 }
84 76
85 return __android_log_pmsg_file_write(logId, prio, name.c_str(), buf, len); 77 return __android_log_pmsg_file_write(id, prio, name.c_str(), buf, len);
86} 78}
87 79
88// Rename last_log -> last_log.1 -> last_log.2 -> ... -> last_log.$max. 80// Rename last_log -> last_log.1 -> last_log.2 -> ... -> last_log.$max.
89// Similarly rename last_kmsg -> last_kmsg.1 -> ... -> last_kmsg.$max. 81// Similarly rename last_kmsg -> last_kmsg.1 -> ... -> last_kmsg.$max.
90// Overwrite any existing last_log.$max and last_kmsg.$max. 82// Overwrite any existing last_log.$max and last_kmsg.$max.
91void rotate_logs(const char* last_log_file, const char* last_kmsg_file) { 83void rotate_logs(const char* last_log_file, const char* last_kmsg_file) {
92 // Logs should only be rotated once. 84 // Logs should only be rotated once.
93 static bool rotated = false; 85 static bool rotated = false;
94 if (rotated) { 86 if (rotated) {
95 return; 87 return;
96 } 88 }
97 rotated = true; 89 rotated = true;
98 90
99 for (int i = KEEP_LOG_COUNT - 1; i >= 0; --i) { 91 for (int i = KEEP_LOG_COUNT - 1; i >= 0; --i) {
100 std::string old_log = android::base::StringPrintf("%s", last_log_file); 92 std::string old_log = android::base::StringPrintf("%s", last_log_file);
101 if (i > 0) { 93 if (i > 0) {
102 old_log += "." + std::to_string(i); 94 old_log += "." + std::to_string(i);
103 } 95 }
104 std::string new_log = android::base::StringPrintf("%s.%d", last_log_file, i+1); 96 std::string new_log = android::base::StringPrintf("%s.%d", last_log_file, i + 1);
105 // Ignore errors if old_log doesn't exist. 97 // Ignore errors if old_log doesn't exist.
106 rename(old_log.c_str(), new_log.c_str()); 98 rename(old_log.c_str(), new_log.c_str());
107 99
108 std::string old_kmsg = android::base::StringPrintf("%s", last_kmsg_file); 100 std::string old_kmsg = android::base::StringPrintf("%s", last_kmsg_file);
109 if (i > 0) { 101 if (i > 0) {
110 old_kmsg += "." + std::to_string(i); 102 old_kmsg += "." + std::to_string(i);
111 }
112 std::string new_kmsg = android::base::StringPrintf("%s.%d", last_kmsg_file, i+1);
113 rename(old_kmsg.c_str(), new_kmsg.c_str());
114 } 103 }
104 std::string new_kmsg = android::base::StringPrintf("%s.%d", last_kmsg_file, i + 1);
105 rename(old_kmsg.c_str(), new_kmsg.c_str());
106 }
115} 107}