mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-10-16 09:52:31 +00:00
Directly log to log file
This commit is contained in:
@@ -222,39 +222,6 @@ void unlock_blocks() {
|
||||
}
|
||||
}
|
||||
|
||||
static void collect_logs(bool reset) {
|
||||
static bool running = false;
|
||||
static pthread_mutex_t log_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
{
|
||||
mutex_guard lock(log_lock);
|
||||
if (running)
|
||||
return;
|
||||
int test = exec_command_sync("/system/bin/logcat", "-d", "-f", "/dev/null");
|
||||
chmod("/dev/null", 0666);
|
||||
if (test != 0)
|
||||
return;
|
||||
running = true;
|
||||
}
|
||||
if (reset)
|
||||
rename(LOGFILE, LOGFILE ".bak");
|
||||
// Start a daemon thread and wait indefinitely
|
||||
new_daemon_thread([]{
|
||||
int fd = xopen(LOGFILE, O_WRONLY | O_APPEND | O_CREAT | O_CLOEXEC, 0644);
|
||||
exec_t exec {
|
||||
.fd = fd,
|
||||
.fork = fork_no_zombie
|
||||
};
|
||||
int pid = exec_command(exec, "/system/bin/logcat", "-s", "Magisk");
|
||||
close(fd);
|
||||
if (pid < 0) {
|
||||
mutex_guard lock(log_lock);
|
||||
running = false;
|
||||
} else {
|
||||
waitpid(pid, nullptr, 0);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#define test_bit(bit, array) (array[bit / 8] & (1 << (bit % 8)))
|
||||
|
||||
static bool check_key_combo() {
|
||||
@@ -317,7 +284,7 @@ void post_fs_data(int client) {
|
||||
if (!check_data())
|
||||
goto unblock_init;
|
||||
|
||||
collect_logs(true);
|
||||
setup_logfile(true);
|
||||
|
||||
LOGI("** post-fs-data mode running\n");
|
||||
|
||||
@@ -369,7 +336,7 @@ void late_start(int client) {
|
||||
write_int(client, 0);
|
||||
close(client);
|
||||
|
||||
collect_logs(false);
|
||||
setup_logfile(false);
|
||||
|
||||
if (!pfs_done || safe_mode)
|
||||
return;
|
||||
@@ -384,7 +351,7 @@ void boot_complete(int client) {
|
||||
write_int(client, 0);
|
||||
close(client);
|
||||
|
||||
collect_logs(false);
|
||||
setup_logfile(false);
|
||||
|
||||
if (safe_mode)
|
||||
return;
|
||||
|
@@ -14,6 +14,7 @@
|
||||
#include <db.hpp>
|
||||
#include <resetprop.hpp>
|
||||
#include <flags.hpp>
|
||||
#include <stream.hpp>
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -122,21 +123,77 @@ static void handle_request(int client) {
|
||||
}
|
||||
|
||||
// Create new thread to handle complex requests
|
||||
new_daemon_thread(std::bind(&request_handler, client, req_code, cred));
|
||||
new_daemon_thread([=] { return request_handler(client, req_code, cred); });
|
||||
return;
|
||||
|
||||
shortcut:
|
||||
close(client);
|
||||
}
|
||||
|
||||
#define vlog __android_log_vprint
|
||||
static FILE *log_file;
|
||||
|
||||
bool in_mem_log = true;
|
||||
static char *log_buf;
|
||||
static size_t log_buf_len;
|
||||
|
||||
void setup_logfile(bool reset) {
|
||||
if (!in_mem_log)
|
||||
return;
|
||||
in_mem_log = false;
|
||||
if (reset)
|
||||
rename(LOGFILE, LOGFILE ".bak");
|
||||
|
||||
int fd = xopen(LOGFILE, O_WRONLY | O_APPEND | O_CREAT | O_CLOEXEC, 0644);
|
||||
|
||||
// Dump all logs in memory (if exists)
|
||||
if (log_buf) {
|
||||
write(fd, log_buf, log_buf_len);
|
||||
}
|
||||
|
||||
// Redirect to log file
|
||||
log_file = fdopen(fd, "a");
|
||||
setbuf(log_file, nullptr);
|
||||
|
||||
free(log_buf);
|
||||
}
|
||||
|
||||
static int magisk_log(int prio, const char *fmt, va_list ap) {
|
||||
// Log to logcat
|
||||
__android_log_vprint(prio, "Magisk", fmt, ap);
|
||||
|
||||
char buf[4096];
|
||||
timeval tv;
|
||||
tm tm;
|
||||
char type;
|
||||
switch (prio) {
|
||||
case ANDROID_LOG_DEBUG:
|
||||
type = 'D';
|
||||
break;
|
||||
case ANDROID_LOG_INFO:
|
||||
type = 'I';
|
||||
break;
|
||||
case ANDROID_LOG_WARN:
|
||||
type = 'W';
|
||||
break;
|
||||
default:
|
||||
type = 'E';
|
||||
break;
|
||||
}
|
||||
gettimeofday(&tv, nullptr);
|
||||
localtime_r(&tv.tv_sec, &tm);
|
||||
size_t len = strftime(buf, sizeof(buf), "%m-%d %T", &tm);
|
||||
int ms = tv.tv_usec / 1000;
|
||||
len += sprintf(buf + len, ".%03d %c : ", ms, type);
|
||||
strcpy(buf + len, fmt);
|
||||
return vfprintf(log_file, buf, ap);
|
||||
}
|
||||
|
||||
static void android_logging() {
|
||||
static constexpr char TAG[] = "Magisk";
|
||||
log_cb.d = [](auto fmt, auto ap){ return vlog(ANDROID_LOG_DEBUG, TAG, fmt, ap); };
|
||||
log_cb.i = [](auto fmt, auto ap){ return vlog(ANDROID_LOG_INFO, TAG, fmt, ap); };
|
||||
log_cb.w = [](auto fmt, auto ap){ return vlog(ANDROID_LOG_WARN, TAG, fmt, ap); };
|
||||
log_cb.e = [](auto fmt, auto ap){ return vlog(ANDROID_LOG_ERROR, TAG, fmt, ap); };
|
||||
log_file = make_stream_fp<byte_stream>(log_buf, log_buf_len).release();
|
||||
log_cb.d = [](auto fmt, auto ap){ return magisk_log(ANDROID_LOG_DEBUG, fmt, ap); };
|
||||
log_cb.i = [](auto fmt, auto ap){ return magisk_log(ANDROID_LOG_INFO, fmt, ap); };
|
||||
log_cb.w = [](auto fmt, auto ap){ return magisk_log(ANDROID_LOG_WARN, fmt, ap); };
|
||||
log_cb.e = [](auto fmt, auto ap){ return magisk_log(ANDROID_LOG_ERROR, fmt, ap); };
|
||||
log_cb.ex = nop_ex;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user