From c469cc23a145a928431127f4e5f2430c578dd47e Mon Sep 17 00:00:00 2001 From: Sergiu Deitsch Date: Mon, 8 Jan 2024 16:26:41 +0100 Subject: [PATCH] fix: unify additional debug output (#1050) --- src/logging.cc | 34 ++++++---------------------------- src/utilities.cc | 38 ++++++++++++++++++++++++++++---------- src/utilities.h | 3 +++ 3 files changed, 37 insertions(+), 38 deletions(-) diff --git a/src/logging.cc b/src/logging.cc index ac0946134..a83ac697f 100644 --- a/src/logging.cc +++ b/src/logging.cc @@ -88,10 +88,6 @@ # include #endif -#ifdef __ANDROID__ -# include -#endif - #ifdef HAVE_SYS_TYPES_H # include #endif @@ -787,24 +783,9 @@ inline void LogDestination::MaybeLogToStderr(LogSeverity severity, size_t prefix_len) { if ((severity >= FLAGS_stderrthreshold) || FLAGS_alsologtostderr) { ColoredWriteToStderr(severity, message, message_len); -#ifdef GLOG_OS_WINDOWS - (void)prefix_len; - // On Windows, also output to the debugger - ::OutputDebugStringA(message); -#elif defined(__ANDROID__) - // On Android, also output to logcat - const int android_log_levels[NUM_SEVERITIES] = { - ANDROID_LOG_INFO, - ANDROID_LOG_WARN, - ANDROID_LOG_ERROR, - ANDROID_LOG_FATAL, - }; - __android_log_write(android_log_levels[severity], - glog_internal_namespace_::ProgramInvocationShortName(), - message + prefix_len); -#else - (void)prefix_len; -#endif + AlsoErrorWrite(severity, + glog_internal_namespace_::ProgramInvocationShortName(), + message + prefix_len); } } @@ -1850,12 +1831,9 @@ void LogMessage::SendToLog() EXCLUSIVE_LOCKS_REQUIRED(log_mutex) { if (write(STDERR_FILENO, message, strlen(message)) < 0) { // Ignore errors. } -#if defined(__ANDROID__) - // ANDROID_LOG_FATAL as this message is of FATAL severity. - __android_log_write(ANDROID_LOG_FATAL, - glog_internal_namespace_::ProgramInvocationShortName(), - message); -#endif + AlsoErrorWrite(GLOG_FATAL, + glog_internal_namespace_::ProgramInvocationShortName(), + message); Fail(); } } diff --git a/src/utilities.cc b/src/utilities.cc index 09a5050bc..aa71e9bc7 100644 --- a/src/utilities.cc +++ b/src/utilities.cc @@ -38,11 +38,13 @@ #include #include #include -#include #include "base/googleinit.h" #include "config.h" +#ifdef __ANDROID__ +# include +#endif #ifdef HAVE_SYS_TIME_H # include #endif @@ -60,9 +62,6 @@ #ifdef HAVE_PWD_H # include #endif -#ifdef __ANDROID__ -# include -#endif #if defined(HAVE___PROGNAME) extern char* __progname; @@ -82,6 +81,29 @@ inline namespace glog_internal_namespace_ { constexpr int FileDescriptor::InvalidHandle; +void AlsoErrorWrite(LogSeverity severity, const char* tag, + const char* message) noexcept { +#if defined(GLOG_OS_WINDOWS) + (void)severity; + (void)tag; + // On Windows, also output to the debugger + ::OutputDebugStringA(message); +#elif defined(__ANDROID__) + constexpr int android_log_levels[] = { + ANDROID_LOG_INFO, + ANDROID_LOG_WARN, + ANDROID_LOG_ERROR, + ANDROID_LOG_FATAL, + }; + + __android_log_write(android_log_levels[severity], tag, message); +#else + (void)severity; + (void)tag; + (void)message; +#endif +} + } // namespace glog_internal_namespace_ } // namespace google @@ -106,12 +128,8 @@ static void DebugWriteToStderr(const char* data, void*) { if (write(STDERR_FILENO, data, strlen(data)) < 0) { // Ignore errors. } -# if defined(__ANDROID__) - // ANDROID_LOG_FATAL as fatal error occurred and now is dumping call stack. - __android_log_write(ANDROID_LOG_FATAL, - glog_internal_namespace_::ProgramInvocationShortName(), - data); -# endif + AlsoErrorWrite(GLOG_FATAL, + glog_internal_namespace_::ProgramInvocationShortName(), data); } static void DebugWriteToString(const char* data, void* arg) { diff --git a/src/utilities.h b/src/utilities.h index 0d4ebfc0d..ea6344c00 100644 --- a/src/utilities.h +++ b/src/utilities.h @@ -186,6 +186,9 @@ inline namespace glog_internal_namespace_ { # define ATTRIBUTE_NOINLINE #endif +void AlsoErrorWrite(LogSeverity severity, const char* tag, + const char* message) noexcept; + const char* ProgramInvocationShortName(); int32 GetMainThreadPid();