[core] Create log dir when it was deleted (#1037)

This commit is contained in:
LoveSy 2021-09-02 22:23:38 +08:00 committed by GitHub
parent 8f5f2d224d
commit 71780b4b70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 26 deletions

View File

@ -46,7 +46,7 @@ private:
void ProcessBuffer(struct log_msg *buf);
static int PrintLogLine(const AndroidLogEntry &entry, FILE *out);
static size_t PrintLogLine(const AndroidLogEntry &entry, FILE *out);
JNIEnv *env_;
jobject thiz_;
@ -63,7 +63,7 @@ private:
bool verbose_ = true;
};
int Logcat::PrintLogLine(const AndroidLogEntry &entry, FILE *out) {
size_t Logcat::PrintLogLine(const AndroidLogEntry &entry, FILE *out) {
if (!out) return 0;
constexpr static size_t kMaxTimeBuff = 64;
struct tm tm{};
@ -81,6 +81,8 @@ int Logcat::PrintLogLine(const AndroidLogEntry &entry, FILE *out) {
}
localtime_r(&now, &tm);
strftime(time_buff.data(), time_buff.size(), "%Y-%m-%dT%H:%M:%S", &tm);
// implicitly convert to size_t and intentionally trigger overflow when failed
// to generate a new fd
return fprintf(out, "[ %s.%03ld %8d:%6d:%6d %c/%-15.*s ] %.*s\n",
time_buff.data(),
nsec / MS_PER_NSEC,
@ -142,12 +144,12 @@ void Logcat::ProcessBuffer(struct log_msg *buf) {
void Logcat::Run() {
constexpr size_t tail_after_crash = 10U;
constexpr size_t kMaxRestartLogdWait = 1024U;
constexpr size_t max_restart_logd_wait = 1U << 10;
size_t tail = 0;
RefreshFd(true);
RefreshFd(false);
size_t logd_crash_times = 0;
size_t logd_restart_wait = 8;
size_t logd_crash_count = 0;
size_t logd_restart_wait = 1 << 3;
while (true) {
std::unique_ptr<logger_list, decltype(&android_logger_list_free)> logger_list{
android_logger_list_alloc(0, tail, 0), &android_logger_list_free};
@ -172,13 +174,18 @@ void Logcat::Run() {
if (verbose_print_count_ >= kMaxLogSize) [[unlikely]] RefreshFd(true);
if (modules_print_count_ >= kMaxLogSize) [[unlikely]] RefreshFd(false);
}
logd_crash_times++;
if (logd_crash_times >= logd_restart_wait) {
fprintf(verbose_file_.get(), "\nLogd crashed too many times, trying mannually start...\n");
fprintf(modules_file_.get(), "\nLogd crashed too many times, trying mannually start...\n");
logd_crash_count++;
if (logd_crash_count >= logd_restart_wait) {
fprintf(verbose_file_.get(),
"\nLogd crashed too many times, trying manually start...\n");
fprintf(modules_file_.get(),
"\nLogd crashed too many times, trying manually start...\n");
__system_property_set("ctl.restart", "logd");
if (logd_restart_wait < kMaxRestartLogdWait) logd_restart_wait *= 2;
else logd_crash_times = 0;
if (logd_restart_wait < max_restart_logd_wait) {
logd_restart_wait <<= 1;
} else {
logd_crash_count = 0;
}
}
fprintf(verbose_file_.get(), "\nLogd maybe crashed, retrying in 1s...\n");

View File

@ -90,11 +90,13 @@ class ConfigFileManager {
return prefix + "_" + formatter.format(Instant.now()) + ".txt";
}
static File getNewVerboseLogPath() {
static File getNewVerboseLogPath() throws IOException {
Files.createDirectories(logDirPath);
return logDirPath.resolve(getNewLogFileName("verbose")).toFile();
}
static File getNewModulesLogPath() {
static File getNewModulesLogPath() throws IOException {
Files.createDirectories(logDirPath);
return logDirPath.resolve(getNewLogFileName("modules")).toFile();
}

View File

@ -35,17 +35,11 @@ public class LogcatService implements Runnable {
@SuppressWarnings("unused")
private int refreshFd(boolean isVerboseLog) {
File log;
if (isVerboseLog) {
verboseLog = ConfigFileManager.getNewVerboseLogPath();
log = verboseLog;
} else {
modulesLog = ConfigFileManager.getNewModulesLogPath();
log = modulesLog;
}
try (var fd = ParcelFileDescriptor.open(log, mode)) {
Log.i(TAG, "New " + (isVerboseLog ? "verbose log" : "modules log") + " file: " + log);
return fd.detachFd();
try {
File log = isVerboseLog ? (verboseLog = ConfigFileManager.getNewVerboseLogPath()) :
(modulesLog = ConfigFileManager.getNewModulesLogPath());
Log.i(TAG, "New " + (isVerboseLog ? "verbose" : "modules") + " log file: " + log);
return ParcelFileDescriptor.open(log, mode).detachFd();
} catch (IOException e) {
Log.w(TAG, "someone chattr +i ?", e);
return -1;