Base: Add AbortFunction for logging
Add a hook to modify FATAL behavior. Add a test.
Bug: 31338270
Test: m
Test: mmma system/core/base && $ANDROID_HOST_OUT/nativetest64/libbase_test/libbase_test64
Change-Id: I966da319cda613738b3f2ccdac8c21900d6a5c72
diff --git a/base/logging.cpp b/base/logging.cpp
index 86d3b4d..6e1dd9c 100644
--- a/base/logging.cpp
+++ b/base/logging.cpp
@@ -173,6 +173,8 @@
static auto& gLogger = *new LogFunction(StderrLogger);
#endif
+static auto& gAborter = *new AbortFunction(DefaultAborter);
+
static bool gInitialized = false;
static LogSeverity gMinimumLogSeverity = INFO;
static auto& gProgramInvocationName = *new std::unique_ptr<std::string>();
@@ -247,6 +249,15 @@
severity_char, timestamp, getpid(), GetThreadId(), file, line, message);
}
+void DefaultAborter(const char* abort_message) {
+#ifdef __ANDROID__
+ android_set_abort_message(abort_message);
+#else
+ UNUSED(abort_message);
+#endif
+ abort();
+}
+
#ifdef __ANDROID__
LogdLogger::LogdLogger(LogId default_log_id) : default_log_id_(default_log_id) {
@@ -284,12 +295,10 @@
}
#endif
-void InitLogging(char* argv[], LogFunction&& logger) {
+void InitLogging(char* argv[], LogFunction&& logger, AbortFunction&& aborter) {
SetLogger(std::forward<LogFunction>(logger));
- InitLogging(argv);
-}
+ SetAborter(std::forward<AbortFunction>(aborter));
-void InitLogging(char* argv[]) {
if (gInitialized) {
return;
}
@@ -349,6 +358,11 @@
gLogger = std::move(logger);
}
+void SetAborter(AbortFunction&& aborter) {
+ lock_guard<mutex> lock(logging_lock);
+ gAborter = std::move(aborter);
+}
+
static const char* GetFileBasename(const char* file) {
// We can't use basename(3) even on Unix because the Mac doesn't
// have a non-modifying basename.
@@ -450,10 +464,7 @@
// Abort if necessary.
if (data_->GetSeverity() == FATAL) {
-#ifdef __ANDROID__
- android_set_abort_message(msg.c_str());
-#endif
- abort();
+ gAborter(msg.c_str());
}
}