Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output to logcat on Android #553

Merged
merged 1 commit into from
Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,10 @@ if (gflags_FOUND)
endif (NOT BUILD_SHARED_LIBS)
endif (gflags_FOUND)

if (ANDROID)
target_link_libraries (glog PUBLIC log)
endif()

set_target_properties (glog PROPERTIES VERSION ${PROJECT_VERSION})
set_target_properties (glog PROPERTIES SOVERSION ${PROJECT_VERSION_MAJOR})

Expand Down
26 changes: 21 additions & 5 deletions src/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@
# include "stacktrace.h"
#endif

#ifdef __ANDROID__
#include <android/log.h>
#endif

using std::string;
using std::vector;
using std::setw;
Expand Down Expand Up @@ -500,7 +504,7 @@ class LogDestination {
// Take a log message of a particular severity and log it to stderr
// iff it's of a high enough severity to deserve it.
static void MaybeLogToStderr(LogSeverity severity, const char* message,
size_t len);
size_t message_len, size_t prefix_len);

// Take a log message of a particular severity and log it to email
// iff it's of a high enough severity to deserve it.
Expand Down Expand Up @@ -735,12 +739,23 @@ static void WriteToStderr(const char* message, size_t len) {
}

inline void LogDestination::MaybeLogToStderr(LogSeverity severity,
const char* message, size_t len) {
const char* message, size_t message_len, size_t prefix_len) {
if ((severity >= FLAGS_stderrthreshold) || FLAGS_alsologtostderr) {
ColoredWriteToStderr(severity, message, len);
ColoredWriteToStderr(severity, message, message_len);
#ifdef OS_WINDOWS
// On Windows, also output to the debugger
::OutputDebugStringA(string(message,len).c_str());
::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);
#endif
}
}
Expand Down Expand Up @@ -1597,7 +1612,8 @@ void LogMessage::SendToLog() EXCLUSIVE_LOCKS_REQUIRED(log_mutex) {
data_->num_chars_to_log_);

LogDestination::MaybeLogToStderr(data_->severity_, data_->message_text_,
data_->num_chars_to_log_);
data_->num_chars_to_log_,
data_->num_prefix_chars_);
LogDestination::MaybeLogToEmail(data_->severity_, data_->message_text_,
data_->num_chars_to_log_);
LogDestination::LogToSinks(data_->severity_,
Expand Down