No Description

rotate_logs.cpp 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (C) 2016 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "rotate_logs.h"
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <sys/types.h>
  20. #include <string>
  21. #include <android-base/file.h>
  22. #include <android-base/logging.h>
  23. #include <android-base/parseint.h>
  24. #include <android-base/stringprintf.h>
  25. #include <private/android_logger.h> /* private pmsg functions */
  26. static const std::string LAST_KMSG_FILTER = "recovery/last_kmsg";
  27. static const std::string LAST_LOG_FILTER = "recovery/last_log";
  28. ssize_t logbasename(log_id_t /* id */, char /* prio */, const char* filename, const char* /* buf */,
  29. size_t len, void* arg) {
  30. bool* do_rotate = static_cast<bool*>(arg);
  31. if (LAST_KMSG_FILTER.find(filename) != std::string::npos ||
  32. LAST_LOG_FILTER.find(filename) != std::string::npos) {
  33. *do_rotate = true;
  34. }
  35. return len;
  36. }
  37. ssize_t logrotate(log_id_t id, char prio, const char* filename, const char* buf, size_t len,
  38. void* arg) {
  39. bool* do_rotate = static_cast<bool*>(arg);
  40. if (!*do_rotate) {
  41. return __android_log_pmsg_file_write(id, prio, filename, buf, len);
  42. }
  43. std::string name(filename);
  44. size_t dot = name.find_last_of('.');
  45. std::string sub = name.substr(0, dot);
  46. if (LAST_KMSG_FILTER.find(sub) == std::string::npos &&
  47. LAST_LOG_FILTER.find(sub) == std::string::npos) {
  48. return __android_log_pmsg_file_write(id, prio, filename, buf, len);
  49. }
  50. // filename rotation
  51. if (dot == std::string::npos) {
  52. name += ".1";
  53. } else {
  54. std::string number = name.substr(dot + 1);
  55. if (!isdigit(number[0])) {
  56. name += ".1";
  57. } else {
  58. size_t i;
  59. if (!android::base::ParseUint(number, &i)) {
  60. LOG(ERROR) << "failed to parse uint in " << number;
  61. return -1;
  62. }
  63. name = sub + "." + std::to_string(i + 1);
  64. }
  65. }
  66. return __android_log_pmsg_file_write(id, prio, name.c_str(), buf, len);
  67. }
  68. // Rename last_log -> last_log.1 -> last_log.2 -> ... -> last_log.$max.
  69. // Similarly rename last_kmsg -> last_kmsg.1 -> ... -> last_kmsg.$max.
  70. // Overwrite any existing last_log.$max and last_kmsg.$max.
  71. void rotate_logs(const char* last_log_file, const char* last_kmsg_file) {
  72. // Logs should only be rotated once.
  73. static bool rotated = false;
  74. if (rotated) {
  75. return;
  76. }
  77. rotated = true;
  78. for (int i = KEEP_LOG_COUNT - 1; i >= 0; --i) {
  79. std::string old_log = android::base::StringPrintf("%s", last_log_file);
  80. if (i > 0) {
  81. old_log += "." + std::to_string(i);
  82. }
  83. std::string new_log = android::base::StringPrintf("%s.%d", last_log_file, i + 1);
  84. // Ignore errors if old_log doesn't exist.
  85. rename(old_log.c_str(), new_log.c_str());
  86. std::string old_kmsg = android::base::StringPrintf("%s", last_kmsg_file);
  87. if (i > 0) {
  88. old_kmsg += "." + std::to_string(i);
  89. }
  90. std::string new_kmsg = android::base::StringPrintf("%s.%d", last_kmsg_file, i + 1);
  91. rename(old_kmsg.c_str(), new_kmsg.c_str());
  92. }
  93. }