Magisk/native/jni/base/logging.cpp

74 lines
1.9 KiB
C++
Raw Normal View History

#include <cstdio>
#include <cstdlib>
2018-09-27 07:11:10 +00:00
#include <flags.h>
2022-07-01 11:53:41 +00:00
#include <base.hpp>
2018-09-27 07:11:10 +00:00
2022-07-01 11:53:41 +00:00
// Just need to include it somewhere
#include <base-rs.cpp>
2021-08-12 10:26:54 +00:00
using namespace std;
2022-07-01 11:53:41 +00:00
void nop_log(const char *, va_list) {}
2018-09-27 07:11:10 +00:00
log_callback log_cb = {
.d = nop_log,
.i = nop_log,
.w = nop_log,
.e = nop_log,
2018-09-27 07:11:10 +00:00
};
2022-07-01 11:53:41 +00:00
static bool EXIT_ON_ERROR = false;
2018-09-27 07:11:10 +00:00
2022-07-01 11:53:41 +00:00
static void fmt_and_log_with_rs(LogLevel level, const char *fmt, va_list ap) {
char buf[4096];
int len = vsnprintf(buf, sizeof(buf), fmt, ap);
if (len > 0 && buf[len - 1] == '\n') {
// It's unfortunate that all logging on the C++ side always manually include
// a newline at the end due to how it was originally implemented.
// The logging infrastructure on the rust side does NOT expect a newline
// at the end, so we will have to strip it out before sending it over.
buf[len - 1] = '\0';
}
log_with_rs(level, buf);
2018-09-27 07:11:10 +00:00
}
2022-07-01 11:53:41 +00:00
#define rlog(prio) [](auto fmt, auto ap) { fmt_and_log_with_rs(LogLevel::prio, fmt, ap); }
static void forward_logging_to_rs() {
log_cb.d = rlog(Debug);
log_cb.i = rlog(Info);
log_cb.w = rlog(Warn);
log_cb.e = rlog(Error);
2018-09-27 07:11:10 +00:00
}
void cmdline_logging() {
2022-07-01 11:53:41 +00:00
rs::logging::cmdline_logging();
forward_logging_to_rs();
}
void exit_on_error(bool b) {
rs::logging::exit_on_error(b);
EXIT_ON_ERROR = b;
2018-09-27 07:11:10 +00:00
}
2021-08-12 10:26:54 +00:00
#define LOG_BODY(prio) { \
va_list argv; \
va_start(argv, fmt); \
log_cb.prio(fmt, argv); \
va_end(argv); \
2018-09-27 07:11:10 +00:00
}
2021-08-12 10:26:54 +00:00
// LTO will optimize out the NOP function
#if MAGISK_DEBUG
2021-08-12 10:26:54 +00:00
void LOGD(const char *fmt, ...) { LOG_BODY(d) }
#else
2021-08-12 10:26:54 +00:00
void LOGD(const char *fmt, ...) {}
#endif
2021-08-12 10:26:54 +00:00
void LOGI(const char *fmt, ...) { LOG_BODY(i) }
void LOGW(const char *fmt, ...) { LOG_BODY(w) }
2022-07-01 11:53:41 +00:00
void LOGE(const char *fmt, ...) { LOG_BODY(e); if (EXIT_ON_ERROR) exit(EXIT_FAILURE); }
2022-05-12 09:03:42 +00:00
// Export raw symbol to fortify compat
2022-07-01 11:53:41 +00:00
extern "C" void __vloge(const char* fmt, va_list ap) {
log_cb.e(fmt, ap);
2022-05-12 09:03:42 +00:00
}