mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-10-19 16:21:53 +00:00
Better logging system
Use C++ magic to strip out debug logs at compile time
This commit is contained in:
@@ -1,15 +1,16 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
|
||||
#include <logging.hpp>
|
||||
#include <flags.hpp>
|
||||
|
||||
int nop_log(const char *fmt, va_list ap) {
|
||||
return 0;
|
||||
}
|
||||
using namespace std;
|
||||
|
||||
void nop_ex(int i) {}
|
||||
int nop_log(const char *, va_list) { return 0; }
|
||||
|
||||
struct log_callback log_cb = {
|
||||
void nop_ex(int) {}
|
||||
|
||||
log_callback log_cb = {
|
||||
.d = nop_log,
|
||||
.i = nop_log,
|
||||
.w = nop_log,
|
||||
@@ -25,37 +26,42 @@ void no_logging() {
|
||||
log_cb.ex = nop_ex;
|
||||
}
|
||||
|
||||
static int vprinte(const char *fmt, va_list ap) {
|
||||
static int vprintfe(const char *fmt, va_list ap) {
|
||||
return vfprintf(stderr, fmt, ap);
|
||||
}
|
||||
|
||||
void cmdline_logging() {
|
||||
log_cb.d = vprinte;
|
||||
log_cb.d = vprintfe;
|
||||
log_cb.i = vprintf;
|
||||
log_cb.w = vprinte;
|
||||
log_cb.e = vprinte;
|
||||
log_cb.w = vprintfe;
|
||||
log_cb.e = vprintfe;
|
||||
log_cb.ex = exit;
|
||||
}
|
||||
|
||||
int log_handler(log_type t, const char *fmt, ...) {
|
||||
template <int type>
|
||||
void log_handler(const char *fmt, ...) {
|
||||
va_list argv;
|
||||
int ret = 0;
|
||||
va_start(argv, fmt);
|
||||
switch (t) {
|
||||
case L_DEBUG:
|
||||
ret = log_cb.d(fmt, argv);
|
||||
break;
|
||||
case L_INFO:
|
||||
ret = log_cb.i(fmt, argv);
|
||||
break;
|
||||
case L_WARN:
|
||||
ret = log_cb.w(fmt, argv);
|
||||
break;
|
||||
case L_ERR:
|
||||
ret = log_cb.e(fmt, argv);
|
||||
if constexpr (type == L_DEBUG) {
|
||||
log_cb.d(fmt, argv);
|
||||
} else if constexpr (type == L_INFO) {
|
||||
log_cb.i(fmt, argv);
|
||||
} else if constexpr (type == L_WARN) {
|
||||
log_cb.w(fmt, argv);
|
||||
} else if constexpr (type == L_ERR) {
|
||||
log_cb.e(fmt, argv);
|
||||
log_cb.ex(1);
|
||||
break;
|
||||
}
|
||||
va_end(argv);
|
||||
return ret;
|
||||
}
|
||||
|
||||
template void log_handler<L_INFO>(const char *fmt, ...);
|
||||
template void log_handler<L_WARN>(const char *fmt, ...);
|
||||
template void log_handler<L_ERR>(const char *fmt, ...);
|
||||
|
||||
#ifdef MAGISK_DEBUG
|
||||
template void log_handler<L_DEBUG>(const char *fmt, ...);
|
||||
#else
|
||||
// Strip debug logging for release builds
|
||||
template <> void log_handler<L_DEBUG>(const char *fmt, ...) {}
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user