Browse Source

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
Tao Bao 3 years ago
parent
commit
245c522588
2 changed files with 64 additions and 78 deletions
  1. 56
    64
      rotate_logs.cpp
  2. 8
    14
      rotate_logs.h

+ 56
- 64
rotate_logs.cpp View File

@@ -31,85 +31,77 @@
31 31
 static const std::string LAST_KMSG_FILTER = "recovery/last_kmsg";
32 32
 static const std::string LAST_LOG_FILTER = "recovery/last_log";
33 33
 
34
-ssize_t logbasename(
35
-        log_id_t /* logId */,
36
-        char /* prio */,
37
-        const char *filename,
38
-        const char * /* buf */, size_t len,
39
-        void *arg) {
40
-    bool* doRotate  = static_cast<bool*>(arg);
41
-    if (LAST_KMSG_FILTER.find(filename) != std::string::npos ||
42
-            LAST_LOG_FILTER.find(filename) != std::string::npos) {
43
-        *doRotate = true;
44
-    }
45
-    return len;
34
+ssize_t logbasename(log_id_t /* id */, char /* prio */, const char* filename, const char* /* buf */,
35
+                    size_t len, void* arg) {
36
+  bool* do_rotate = static_cast<bool*>(arg);
37
+  if (LAST_KMSG_FILTER.find(filename) != std::string::npos ||
38
+      LAST_LOG_FILTER.find(filename) != std::string::npos) {
39
+    *do_rotate = true;
40
+  }
41
+  return len;
46 42
 }
47 43
 
48
-ssize_t logrotate(
49
-        log_id_t logId,
50
-        char prio,
51
-        const char *filename,
52
-        const char *buf, size_t len,
53
-        void *arg) {
54
-    bool* doRotate  = static_cast<bool*>(arg);
55
-    if (!*doRotate) {
56
-        return __android_log_pmsg_file_write(logId, prio, filename, buf, len);
57
-    }
44
+ssize_t logrotate(log_id_t id, char prio, const char* filename, const char* buf, size_t len,
45
+                  void* arg) {
46
+  bool* do_rotate = static_cast<bool*>(arg);
47
+  if (!*do_rotate) {
48
+    return __android_log_pmsg_file_write(id, prio, filename, buf, len);
49
+  }
58 50
 
59
-    std::string name(filename);
60
-    size_t dot = name.find_last_of('.');
61
-    std::string sub = name.substr(0, dot);
51
+  std::string name(filename);
52
+  size_t dot = name.find_last_of('.');
53
+  std::string sub = name.substr(0, dot);
62 54
 
63
-    if (LAST_KMSG_FILTER.find(sub) == std::string::npos &&
64
-            LAST_LOG_FILTER.find(sub) == std::string::npos) {
65
-        return __android_log_pmsg_file_write(logId, prio, filename, buf, len);
66
-    }
55
+  if (LAST_KMSG_FILTER.find(sub) == std::string::npos &&
56
+      LAST_LOG_FILTER.find(sub) == std::string::npos) {
57
+    return __android_log_pmsg_file_write(id, prio, filename, buf, len);
58
+  }
67 59
 
68
-    // filename rotation
69
-    if (dot == std::string::npos) {
70
-        name += ".1";
60
+  // filename rotation
61
+  if (dot == std::string::npos) {
62
+    name += ".1";
63
+  } else {
64
+    std::string number = name.substr(dot + 1);
65
+    if (!isdigit(number[0])) {
66
+      name += ".1";
71 67
     } else {
72
-        std::string number = name.substr(dot + 1);
73
-        if (!isdigit(number[0])) {
74
-            name += ".1";
75
-        } else {
76
-            size_t i;
77
-            if (!android::base::ParseUint(number, &i)) {
78
-                LOG(ERROR) << "failed to parse uint in " << number;
79
-                return -1;
80
-            }
81
-            name = sub + "." + std::to_string(i + 1);
82
-        }
68
+      size_t i;
69
+      if (!android::base::ParseUint(number, &i)) {
70
+        LOG(ERROR) << "failed to parse uint in " << number;
71
+        return -1;
72
+      }
73
+      name = sub + "." + std::to_string(i + 1);
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 80
 // Rename last_log -> last_log.1 -> last_log.2 -> ... -> last_log.$max.
89 81
 // Similarly rename last_kmsg -> last_kmsg.1 -> ... -> last_kmsg.$max.
90 82
 // Overwrite any existing last_log.$max and last_kmsg.$max.
91 83
 void rotate_logs(const char* last_log_file, const char* last_kmsg_file) {
92
-    // Logs should only be rotated once.
93
-    static bool rotated = false;
94
-    if (rotated) {
95
-        return;
96
-    }
97
-    rotated = true;
84
+  // Logs should only be rotated once.
85
+  static bool rotated = false;
86
+  if (rotated) {
87
+    return;
88
+  }
89
+  rotated = true;
98 90
 
99
-    for (int i = KEEP_LOG_COUNT - 1; i >= 0; --i) {
100
-        std::string old_log = android::base::StringPrintf("%s", last_log_file);
101
-        if (i > 0) {
102
-          old_log += "." + std::to_string(i);
103
-        }
104
-        std::string new_log = android::base::StringPrintf("%s.%d", last_log_file, i+1);
105
-        // Ignore errors if old_log doesn't exist.
106
-        rename(old_log.c_str(), new_log.c_str());
91
+  for (int i = KEEP_LOG_COUNT - 1; i >= 0; --i) {
92
+    std::string old_log = android::base::StringPrintf("%s", last_log_file);
93
+    if (i > 0) {
94
+      old_log += "." + std::to_string(i);
95
+    }
96
+    std::string new_log = android::base::StringPrintf("%s.%d", last_log_file, i + 1);
97
+    // Ignore errors if old_log doesn't exist.
98
+    rename(old_log.c_str(), new_log.c_str());
107 99
 
108
-        std::string old_kmsg = android::base::StringPrintf("%s", last_kmsg_file);
109
-        if (i > 0) {
110
-          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());
100
+    std::string old_kmsg = android::base::StringPrintf("%s", last_kmsg_file);
101
+    if (i > 0) {
102
+      old_kmsg += "." + std::to_string(i);
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
 }

+ 8
- 14
rotate_logs.h View File

@@ -17,24 +17,18 @@
17 17
 #ifndef _ROTATE_LOGS_H
18 18
 #define _ROTATE_LOGS_H
19 19
 
20
-#include <string>
20
+#include <stddef.h>
21
+#include <sys/types.h>
21 22
 
22
-#include <private/android_logger.h> /* private pmsg functions */
23
+#include <log/log_id.h>
23 24
 
24
-constexpr int KEEP_LOG_COUNT = 10;
25
+static constexpr int KEEP_LOG_COUNT = 10;
25 26
 
26
-ssize_t logbasename(log_id_t /* logId */,
27
-        char /* prio */,
28
-        const char *filename,
29
-        const char * /* buf */, size_t len,
30
-        void *arg);
27
+ssize_t logbasename(log_id_t id, char prio, const char* filename, const char* buf, size_t len,
28
+                    void* arg);
31 29
 
32
-ssize_t logrotate(
33
-        log_id_t logId,
34
-        char prio,
35
-        const char *filename,
36
-        const char *buf, size_t len,
37
-        void *arg);
30
+ssize_t logrotate(log_id_t id, char prio, const char* filename, const char* buf, size_t len,
31
+                  void* arg);
38 32
 
39 33
 // Rename last_log -> last_log.1 -> last_log.2 -> ... -> last_log.$max.
40 34
 // Similarly rename last_kmsg -> last_kmsg.1 -> ... -> last_kmsg.$max.