[core] Fix logcat (#986)

This commit is contained in:
vvb2060 2021-08-23 21:19:32 +08:00 committed by GitHub
parent befcc90bc8
commit a4abbfa67a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 25 deletions

View File

@ -32,10 +32,8 @@ public:
class Logcat { class Logcat {
public: public:
explicit Logcat(JNIEnv *env, jobject thiz, jmethodID method, jlong logger_id) : explicit Logcat(JNIEnv *env, jobject thiz, jmethodID method) :
env_(env), thiz_(thiz), refresh_fd_method_(method), logger_id_(logger_id), env_(env), thiz_(thiz), refresh_fd_method_(method) {}
stop_verbose_inst_("!!stop_verbose!!" + std::to_string(logger_id_)),
start_verbose_inst_("!!start_verbose!!" + std::to_string(logger_id_)) {}
[[noreturn]] void Run(); [[noreturn]] void Run();
@ -49,7 +47,7 @@ private:
JNIEnv *env_; JNIEnv *env_;
jobject thiz_; jobject thiz_;
jmethodID refresh_fd_method_; jmethodID refresh_fd_method_;
jlong logger_id_; std::string id_ = "0";
UniqueFile modules_file_{}; UniqueFile modules_file_{};
size_t modules_file_part_ = 0; size_t modules_file_part_ = 0;
@ -61,8 +59,8 @@ private:
bool verbose_ = false; bool verbose_ = false;
const std::string stop_verbose_inst_; const std::string start_verbose_inst_ = "!!start_verbose!!";
const std::string start_verbose_inst_; const std::string stop_verbose_inst_ = "!!stop_verbose!!";
}; };
int Logcat::PrintLogLine(const AndroidLogEntry &entry, FILE *out) { 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) { 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) { if (is_verbose) {
verbose_print_count_ = 0; verbose_print_count_ = 0;
fprintf(verbose_file_.get(), stop_info, logger_id_, verbose_file_part_); fprintf(verbose_file_.get(), "----%s-%zu end----\n", id_.data(), verbose_file_part_);
verbose_file_ = UniqueFile(env_->CallIntMethod(thiz_, refresh_fd_method_, JNI_TRUE), "w"); verbose_file_ = UniqueFile(env_->CallIntMethod(thiz_, refresh_fd_method_, JNI_TRUE), "a");
verbose_file_part_++; 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 { } else {
modules_print_count_ = 0; modules_print_count_ = 0;
fprintf(modules_file_.get(), stop_info, logger_id_, modules_file_part_); fprintf(modules_file_.get(), "----%zu end----\n", modules_file_part_);
modules_file_ = UniqueFile(env_->CallIntMethod(thiz_, refresh_fd_method_, JNI_FALSE), "w"); modules_file_ = UniqueFile(env_->CallIntMethod(thiz_, refresh_fd_method_, JNI_FALSE), "a");
modules_file_part_++; 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()); verbose_print_count_ += PrintLogLine(entry, verbose_file_.get());
} }
if (entry.pid == getpid() && tag == "LSPosedLogcat") [[unlikely]] { if (entry.pid == getpid() && tag == "LSPosedLogcat") [[unlikely]] {
if (std::string_view(entry.message) == stop_verbose_inst_) { if (std::string_view(entry.message).starts_with(start_verbose_inst_)) {
verbose_ = false;
} else if (std::string_view(entry.message) == start_verbose_inst_) {
RefreshFd(true);
verbose_ = true; 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" extern "C"
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
// NOLINTNEXTLINE // 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); jclass clazz = env->GetObjectClass(thiz);
jmethodID method = env->GetMethodID(clazz, "refreshFd", "(Z)I"); jmethodID method = env->GetMethodID(clazz, "refreshFd", "(Z)I");
Logcat logcat(env, thiz, method, logger_id); Logcat logcat(env, thiz, method);
logcat.Run(); logcat.Run();
} }

View File

@ -18,6 +18,7 @@ public class LogcatService implements Runnable {
private File modulesLog = null; private File modulesLog = null;
private File verboseLog = null; private File verboseLog = null;
private Thread thread = null; private Thread thread = null;
private int id = 0;
@SuppressLint("UnsafeDynamicallyLoadedCode") @SuppressLint("UnsafeDynamicallyLoadedCode")
public LogcatService() { public LogcatService() {
@ -25,12 +26,12 @@ public class LogcatService implements Runnable {
System.load(libraryPath + "/" + System.mapLibraryName("daemon")); System.load(libraryPath + "/" + System.mapLibraryName("daemon"));
} }
private native void runLogcat(long loggerId); private native void runLogcat();
@Override @Override
public void run() { public void run() {
Log.i(TAG, "start running"); Log.i(TAG, "start running");
runLogcat(thread.getId()); runLogcat();
Log.i(TAG, "stoped"); Log.i(TAG, "stoped");
} }
@ -45,6 +46,7 @@ public class LogcatService implements Runnable {
log = modulesLog; log = modulesLog;
} }
try (var fd = ParcelFileDescriptor.open(log, mode)) { try (var fd = ParcelFileDescriptor.open(log, mode)) {
Log.i(TAG, "New " + (isVerboseLog ? "verbose log" : "modules log") + " file: " + log);
return fd.detachFd(); return fd.detachFd();
} catch (IOException e) { } catch (IOException e) {
Log.w(TAG, "someone chattr +i ?", e); Log.w(TAG, "someone chattr +i ?", e);
@ -69,11 +71,12 @@ public class LogcatService implements Runnable {
} }
public void startVerbose() { public void startVerbose() {
Log.i(TAG, "!!start_verbose!!" + thread.getId()); Log.i(TAG, "!!start_verbose!!" + id);
} }
public void stopVerbose() { public void stopVerbose() {
Log.i(TAG, "!!stop_verbose!!" + thread.getId()); Log.i(TAG, "!!stop_verbose!!" + id);
id++;
} }
@Nullable @Nullable