Enable RAW logging on Android via logcat

PiperOrigin-RevId: 437007814
Change-Id: I9d1efe71adf169d1552c97e4ed39cc33f14adc85
This commit is contained in:
Oliver Kunz 2022-03-24 09:04:06 -07:00 committed by Copybara-Service
parent 44cd37c94e
commit 1e42edc62f
3 changed files with 32 additions and 1 deletions

View File

@ -114,6 +114,7 @@ cc_library(
copts = sapi_platform_copts(), copts = sapi_platform_copts(),
deps = [ deps = [
":strerror", ":strerror",
"//sandboxed_api:config",
"@com_google_absl//absl/base:config", "@com_google_absl//absl/base:config",
"@com_google_absl//absl/base:core_headers", "@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/base:log_severity", "@com_google_absl//absl/base:log_severity",

View File

@ -31,6 +31,10 @@
#include "absl/strings/str_cat.h" #include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h" #include "absl/strings/str_format.h"
#ifdef __ANDROID__
#include "android/log.h"
#endif
static const char kTruncated[] = " ... (message truncated)\n"; static const char kTruncated[] = " ... (message truncated)\n";
// sprintf the format to the buffer, adjusting *buf and *size to reflect the // sprintf the format to the buffer, adjusting *buf and *size to reflect the
@ -77,6 +81,23 @@ bool DoRawLog(char** buf, int* size, const char* format, ...) {
return true; return true;
} }
#ifdef __ANDROID__
android_LogPriority ConvertSeverity(absl::LogSeverity severity) {
switch (severity) {
case absl::LogSeverity::kInfo:
return ANDROID_LOG_INFO;
case absl::LogSeverity::kWarning:
return ANDROID_LOG_WARN;
case absl::LogSeverity::kError:
return ANDROID_LOG_ERROR;
case absl::LogSeverity::kFatal:
return ANDROID_LOG_FATAL;
default:
return ANDROID_LOG_INFO;
}
}
#endif
void RawLogVA(absl::LogSeverity severity, const char* file, int line, void RawLogVA(absl::LogSeverity severity, const char* file, int line,
const char* format, va_list ap) ABSL_PRINTF_ATTRIBUTE(4, 0); const char* format, va_list ap) ABSL_PRINTF_ATTRIBUTE(4, 0);
void RawLogVA(absl::LogSeverity severity, const char* file, int line, void RawLogVA(absl::LogSeverity severity, const char* file, int line,
@ -93,7 +114,13 @@ void RawLogVA(absl::LogSeverity severity, const char* file, int line,
} else { } else {
DoRawLog(&buf, &size, "%s", kTruncated); DoRawLog(&buf, &size, "%s", kTruncated);
} }
#ifndef __ANDROID__
sapi::raw_logging_internal::SafeWriteToStderr(buffer, strlen(buffer)); sapi::raw_logging_internal::SafeWriteToStderr(buffer, strlen(buffer));
#else
// Logs to Android's logcat with the TAG SAPI and the log line containing
// the code location and the log output.
__android_log_print(ConvertSeverity(severity), "SAPI", "%s", buffer);
#endif
// Abort the process after logging a FATAL message, even if the output itself // Abort the process after logging a FATAL message, even if the output itself
// was suppressed. // was suppressed.

View File

@ -41,7 +41,10 @@
#define SAPI_INTERNAL_UNREACHABLE #define SAPI_INTERNAL_UNREACHABLE
#endif #endif
#ifdef ABSL_RAW_LOG // Exclude ABSL_RAW_LOG when running on Android because it will not be visible
// in logcat since Android sends anything written to stdout and stderr to
// /dev/null.
#if defined(ABSL_RAW_LOG) && !(__ANDROID__)
#define SAPI_RAW_LOG ABSL_RAW_LOG #define SAPI_RAW_LOG ABSL_RAW_LOG
#else #else
// This is similar to LOG(severity) << format..., but // This is similar to LOG(severity) << format..., but