Skip to content
This repository was archived by the owner on Jun 30, 2025. It is now read-only.

Commit 64650ef

Browse files
authored
Connect glog to Andorid logging API
In LogMessage::Flush(), write to __android_log_write() so that the messages can be seen via "adb logcat". This is already a patch in the Google MediaPipe's repo: https://github.com/google/mediapipe/blob/master/third_party/com_github_glog_glog_9779e5ea6ef59562b030248947f787d1256132ae.diff.
1 parent 0a2e593 commit 64650ef

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

src/logging.cc

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@
7373
# include "stacktrace.h"
7474
#endif
7575

76+
#ifdef __ANDROID__
77+
#include <android/log.h>
78+
#endif
79+
7680
using std::string;
7781
using std::vector;
7882
using std::setw;
@@ -1485,6 +1489,23 @@ ostream& LogMessage::stream() {
14851489
return data_->stream_;
14861490
}
14871491

1492+
namespace {
1493+
#if defined(__ANDROID__)
1494+
int AndroidLogLevel(const int severity) {
1495+
switch (severity) {
1496+
case 3:
1497+
return ANDROID_LOG_FATAL;
1498+
case 2:
1499+
return ANDROID_LOG_ERROR;
1500+
case 1:
1501+
return ANDROID_LOG_WARN;
1502+
default:
1503+
return ANDROID_LOG_INFO;
1504+
}
1505+
}
1506+
#endif // defined(__ANDROID__)
1507+
} // namespace
1508+
14881509
// Flush buffered message, called by the destructor, or any other function
14891510
// that needs to synchronize the log.
14901511
void LogMessage::Flush() {
@@ -1518,7 +1539,13 @@ void LogMessage::Flush() {
15181539
++num_messages_[static_cast<int>(data_->severity_)];
15191540
}
15201541
LogDestination::WaitForSinks(data_);
1521-
1542+
1543+
#if defined(__ANDROID__)
1544+
const int level = AndroidLogLevel((int)data_->severity_);
1545+
const std::string text = std::string(data_->message_text_);
1546+
__android_log_write(level, "native", text.substr(0,data_->num_chars_to_log_).c_str());
1547+
#endif // !defined(__ANDROID__)
1548+
15221549
if (append_newline) {
15231550
// Fix the ostrstream back how it was before we screwed with it.
15241551
// It's 99.44% certain that we don't need to worry about doing this.

0 commit comments

Comments
 (0)