From a4abbfa67aa643ab59deebbb60799ab3098fe32b Mon Sep 17 00:00:00 2001 From: vvb2060 Date: Mon, 23 Aug 2021 21:19:32 +0800 Subject: [PATCH] [core] Fix logcat (#986) --- core/src/main/cpp/daemon/logcat.cpp | 39 +++++++++---------- .../lsposed/lspd/service/LogcatService.java | 11 ++++-- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/core/src/main/cpp/daemon/logcat.cpp b/core/src/main/cpp/daemon/logcat.cpp index b7e6a3da..30742e4d 100644 --- a/core/src/main/cpp/daemon/logcat.cpp +++ b/core/src/main/cpp/daemon/logcat.cpp @@ -32,10 +32,8 @@ public: class Logcat { public: - explicit Logcat(JNIEnv *env, jobject thiz, jmethodID method, jlong logger_id) : - env_(env), thiz_(thiz), refresh_fd_method_(method), logger_id_(logger_id), - stop_verbose_inst_("!!stop_verbose!!" + std::to_string(logger_id_)), - start_verbose_inst_("!!start_verbose!!" + std::to_string(logger_id_)) {} + explicit Logcat(JNIEnv *env, jobject thiz, jmethodID method) : + env_(env), thiz_(thiz), refresh_fd_method_(method) {} [[noreturn]] void Run(); @@ -49,7 +47,7 @@ private: JNIEnv *env_; jobject thiz_; jmethodID refresh_fd_method_; - jlong logger_id_; + std::string id_ = "0"; UniqueFile modules_file_{}; size_t modules_file_part_ = 0; @@ -61,8 +59,8 @@ private: bool verbose_ = false; - const std::string stop_verbose_inst_; - const std::string start_verbose_inst_; + const std::string start_verbose_inst_ = "!!start_verbose!!"; + const std::string stop_verbose_inst_ = "!!stop_verbose!!"; }; int Logcat::PrintLogLine(const AndroidLogEntry &entry, FILE *out) { @@ -92,20 +90,18 @@ int Logcat::PrintLogLine(const AndroidLogEntry &entry, FILE *out) { } void Logcat::RefreshFd(bool is_verbose) { - constexpr const char *start_info = "----%" PRId64 "-%zu start----\n"; - constexpr const char *stop_info = "----%" PRId64 "-%zu end----\n"; if (is_verbose) { verbose_print_count_ = 0; - fprintf(verbose_file_.get(), stop_info, logger_id_, verbose_file_part_); - verbose_file_ = UniqueFile(env_->CallIntMethod(thiz_, refresh_fd_method_, JNI_TRUE), "w"); + fprintf(verbose_file_.get(), "----%s-%zu end----\n", id_.data(), verbose_file_part_); + verbose_file_ = UniqueFile(env_->CallIntMethod(thiz_, refresh_fd_method_, JNI_TRUE), "a"); verbose_file_part_++; - fprintf(verbose_file_.get(), start_info, logger_id_, verbose_file_part_); + fprintf(verbose_file_.get(), "----%s-%zu start----\n", id_.data(), verbose_file_part_); } else { modules_print_count_ = 0; - fprintf(modules_file_.get(), stop_info, logger_id_, modules_file_part_); - modules_file_ = UniqueFile(env_->CallIntMethod(thiz_, refresh_fd_method_, JNI_FALSE), "w"); + fprintf(modules_file_.get(), "----%zu end----\n", modules_file_part_); + modules_file_ = UniqueFile(env_->CallIntMethod(thiz_, refresh_fd_method_, JNI_FALSE), "a"); modules_file_part_++; - fprintf(modules_file_.get(), start_info, logger_id_, modules_file_part_); + fprintf(modules_file_.get(), "----%zu start----\n", modules_file_part_); } } @@ -126,11 +122,12 @@ void Logcat::ProcessBuffer(struct log_msg *buf) { verbose_print_count_ += PrintLogLine(entry, verbose_file_.get()); } if (entry.pid == getpid() && tag == "LSPosedLogcat") [[unlikely]] { - if (std::string_view(entry.message) == stop_verbose_inst_) { - verbose_ = false; - } else if (std::string_view(entry.message) == start_verbose_inst_) { - RefreshFd(true); + if (std::string_view(entry.message).starts_with(start_verbose_inst_)) { verbose_ = true; + RefreshFd(true); + id_ = std::string(entry.message, start_verbose_inst_.length(), std::string::npos); + } else if (std::string_view(entry.message) == stop_verbose_inst_ + id_) { + verbose_ = false; } } } @@ -172,9 +169,9 @@ void Logcat::Run() { extern "C" JNIEXPORT void JNICALL // NOLINTNEXTLINE -Java_org_lsposed_lspd_service_LogcatService_runLogcat(JNIEnv *env, jobject thiz, jlong logger_id) { +Java_org_lsposed_lspd_service_LogcatService_runLogcat(JNIEnv *env, jobject thiz) { jclass clazz = env->GetObjectClass(thiz); jmethodID method = env->GetMethodID(clazz, "refreshFd", "(Z)I"); - Logcat logcat(env, thiz, method, logger_id); + Logcat logcat(env, thiz, method); logcat.Run(); } diff --git a/core/src/main/java/org/lsposed/lspd/service/LogcatService.java b/core/src/main/java/org/lsposed/lspd/service/LogcatService.java index eb458ae6..6562907c 100644 --- a/core/src/main/java/org/lsposed/lspd/service/LogcatService.java +++ b/core/src/main/java/org/lsposed/lspd/service/LogcatService.java @@ -18,6 +18,7 @@ public class LogcatService implements Runnable { private File modulesLog = null; private File verboseLog = null; private Thread thread = null; + private int id = 0; @SuppressLint("UnsafeDynamicallyLoadedCode") public LogcatService() { @@ -25,12 +26,12 @@ public class LogcatService implements Runnable { System.load(libraryPath + "/" + System.mapLibraryName("daemon")); } - private native void runLogcat(long loggerId); + private native void runLogcat(); @Override public void run() { Log.i(TAG, "start running"); - runLogcat(thread.getId()); + runLogcat(); Log.i(TAG, "stoped"); } @@ -45,6 +46,7 @@ public class LogcatService implements Runnable { log = modulesLog; } try (var fd = ParcelFileDescriptor.open(log, mode)) { + Log.i(TAG, "New " + (isVerboseLog ? "verbose log" : "modules log") + " file: " + log); return fd.detachFd(); } catch (IOException e) { Log.w(TAG, "someone chattr +i ?", e); @@ -69,11 +71,12 @@ public class LogcatService implements Runnable { } public void startVerbose() { - Log.i(TAG, "!!start_verbose!!" + thread.getId()); + Log.i(TAG, "!!start_verbose!!" + id); } public void stopVerbose() { - Log.i(TAG, "!!stop_verbose!!" + thread.getId()); + Log.i(TAG, "!!stop_verbose!!" + id); + id++; } @Nullable