fix: unify additional debug output (#1050)

This commit is contained in:
Sergiu Deitsch 2024-01-08 16:26:41 +01:00 committed by GitHub
parent 7af231e6bf
commit c469cc23a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 38 deletions

View File

@ -88,10 +88,6 @@
# include <syslog.h>
#endif
#ifdef __ANDROID__
# include <android/log.h>
#endif
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#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();
}
}

View File

@ -38,11 +38,13 @@
#include <csignal>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include "base/googleinit.h"
#include "config.h"
#ifdef __ANDROID__
# include <android/log.h>
#endif
#ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
#endif
@ -60,9 +62,6 @@
#ifdef HAVE_PWD_H
# include <pwd.h>
#endif
#ifdef __ANDROID__
# include <android/log.h>
#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) {

View File

@ -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();