Skip to content

Commit

Permalink
fix: unify additional debug output (#1050)
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiud authored Jan 8, 2024
1 parent 7af231e commit c469cc2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 38 deletions.
34 changes: 6 additions & 28 deletions src/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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();
}
}
Expand Down
38 changes: 28 additions & 10 deletions src/utilities.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions src/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit c469cc2

Please sign in to comment.