Magisk/native/jni/utils/logging.cpp

68 lines
1.3 KiB
C++
Raw Normal View History

#include <cstdio>
#include <cstdlib>
2018-09-27 07:11:10 +00:00
2020-03-09 08:50:30 +00:00
#include <logging.hpp>
#include <flags.hpp>
2018-09-27 07:11:10 +00:00
using namespace std;
int nop_log(const char *, va_list) { return 0; }
2018-09-27 07:11:10 +00:00
void nop_ex(int) {}
2018-09-27 07:11:10 +00:00
log_callback log_cb = {
2018-09-27 07:11:10 +00:00
.d = nop_log,
.i = nop_log,
.w = nop_log,
.e = nop_log,
.ex = nop_ex
};
void no_logging() {
log_cb.d = nop_log;
log_cb.i = nop_log;
log_cb.w = nop_log;
log_cb.e = nop_log;
log_cb.ex = nop_ex;
}
static int vprintfe(const char *fmt, va_list ap) {
2018-09-27 07:11:10 +00:00
return vfprintf(stderr, fmt, ap);
}
void cmdline_logging() {
log_cb.d = vprintfe;
2018-09-27 07:11:10 +00:00
log_cb.i = vprintf;
log_cb.w = vprintfe;
log_cb.e = vprintfe;
2018-09-27 07:11:10 +00:00
log_cb.ex = exit;
}
template <int type>
void log_handler(const char *fmt, ...) {
2018-09-27 07:11:10 +00:00
va_list argv;
va_start(argv, fmt);
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);
2020-05-10 07:48:41 +00:00
log_cb.ex(1);
2018-09-27 07:11:10 +00:00
}
va_end(argv);
}
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