mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-10-16 17:00:39 +00:00
Start unifying with log monitor
This commit is contained in:
32
jni/utils/log_monitor.c
Normal file
32
jni/utils/log_monitor.c
Normal file
@@ -0,0 +1,32 @@
|
||||
/* log_monitor.c - New thread to monitor logcat
|
||||
*
|
||||
* Open a new thread to call logcat and get logs with tag "Magisk"
|
||||
* Also, write the logs to a log file for debugging purpose
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
static void *logger_thread(void *args) {
|
||||
rename("/cache/magisk.log", "/cache/last_magisk.log");
|
||||
FILE *logfile = xfopen("/cache/magisk.log", "w");
|
||||
// Disable buffering
|
||||
setbuf(logfile, NULL);
|
||||
// Start logcat
|
||||
FILE *p = popen("logcat -s Magisk", "r");
|
||||
while (fgets(magiskbuf, BUF_SIZE, p)) {
|
||||
fprintf(logfile, "%s", magiskbuf);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Start a new thread to monitor logcat and dump to file */
|
||||
void monitor_logs() {
|
||||
pthread_t log_monitor;
|
||||
pthread_create(&log_monitor, NULL, logger_thread, NULL);
|
||||
printf("Hello :)\n");
|
||||
pthread_join(log_monitor, NULL);
|
||||
}
|
21
jni/utils/utils.h
Normal file
21
jni/utils/utils.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#ifndef _UTILS_H_
|
||||
#define _UTILS_H_
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "magisk.h"
|
||||
|
||||
// xwrap.c
|
||||
|
||||
FILE *xfopen(const char *pathname, const char *mode);
|
||||
|
||||
// vector.c
|
||||
|
||||
#include "vector.h"
|
||||
|
||||
// log_monitor.c
|
||||
|
||||
void monitor_logs();
|
||||
|
||||
#endif
|
34
jni/utils/vector.c
Normal file
34
jni/utils/vector.c
Normal file
@@ -0,0 +1,34 @@
|
||||
/* vector.c - A simple vector implementation in c
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "vector.h"
|
||||
|
||||
void vec_init(struct vector *v) {
|
||||
vec_size(v) = 0;
|
||||
vec_cap(v) = 1;
|
||||
vec_entry(v) = malloc(sizeof(void*));
|
||||
}
|
||||
|
||||
void vec_push_back(struct vector *v, void *p) {
|
||||
if (v == NULL) return;
|
||||
if (vec_size(v) == vec_cap(v)) {
|
||||
vec_cap(v) *= 2;
|
||||
vec_entry(v) = realloc(vec_entry(v), sizeof(void*) * vec_cap(v));
|
||||
}
|
||||
vec_entry(v)[vec_size(v)] = p;
|
||||
++vec_size(v);
|
||||
}
|
||||
|
||||
void vec_sort(struct vector *v, int (*compar)(const void *, const void *)) {
|
||||
qsort(vec_entry(v), vec_size(v), sizeof(void*), compar);
|
||||
}
|
||||
|
||||
void vec_destroy(struct vector *v) {
|
||||
// Will not free each entry!
|
||||
// Manually free each entry, then call this function
|
||||
vec_size(v) = 0;
|
||||
vec_cap(v) = 0;
|
||||
free(v->data);
|
||||
}
|
26
jni/utils/vector.h
Normal file
26
jni/utils/vector.h
Normal file
@@ -0,0 +1,26 @@
|
||||
/* vector.h - A simple vector implementation in c
|
||||
*/
|
||||
|
||||
#ifndef _VECTOR_H_
|
||||
#define _VECTOR_H_
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
struct vector {
|
||||
size_t size;
|
||||
size_t cap;
|
||||
void **data;
|
||||
};
|
||||
void vec_init(struct vector *v);
|
||||
void vec_push_back(struct vector *v, void *p);
|
||||
void vec_sort(struct vector *v, int (*compar)(const void *, const void *));
|
||||
void vec_destroy(struct vector *v);
|
||||
#define vec_size(v) (v)->size
|
||||
#define vec_cap(v) (v)->cap
|
||||
#define vec_entry(v) (v)->data
|
||||
/* Usage: vec_for_each(vector *v, void *e) */
|
||||
#define vec_for_each(v, e) \
|
||||
e = (v)->data[0]; \
|
||||
for (size_t _ = 0; _ < (v)->size; ++_, e = (v)->data[_])
|
||||
|
||||
#endif
|
21
jni/utils/xwrap.c
Normal file
21
jni/utils/xwrap.c
Normal file
@@ -0,0 +1,21 @@
|
||||
/* xwrap.c - wrappers around existing library functions.
|
||||
*
|
||||
* Functions with the x prefix are wrappers that either succeed or kill the
|
||||
* program with an error message, but never return failure. They usually have
|
||||
* the same arguments and return value as the function they wrap.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
FILE *xfopen(const char *pathname, const char *mode) {
|
||||
FILE *fp = fopen(pathname, mode);
|
||||
if (fp == NULL) {
|
||||
PLOGE("fopen");
|
||||
exit(1);
|
||||
}
|
||||
return fp;
|
||||
}
|
Reference in New Issue
Block a user