Skip to content

Commit

Permalink
fix: unify additional debug output
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiud committed Jan 8, 2024
1 parent aaefca7 commit c4634b1
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 @@ -35,11 +35,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 @@ -57,9 +59,6 @@
#ifdef HAVE_PWD_H
# include <pwd.h>
#endif
#ifdef __ANDROID__
# include <android/log.h>
#endif

using std::string;

Expand All @@ -75,6 +74,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 @@ -99,12 +121,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 c4634b1

Please sign in to comment.