summaryrefslogtreecommitdiff
path: root/logging_android.cc
diff options
context:
space:
mode:
authorYifan Hong <elsk@google.com>2020-04-08 23:45:47 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2020-04-08 23:45:47 +0000
commitb0981c83d264312902030af103ffeb73ac7316a0 (patch)
tree2709c0d960f486f1c80cf52db48d1a24dc430447 /logging_android.cc
parentcba9c46108cc230c20ecdebfd7d48b597a4e3faf (diff)
parenta73445f65eb8b639a279ceb58a067b916f59a1e9 (diff)
Merge changes from topic "sideload_logging" into rvc-dev
* changes: sideload: fix duplicated logging Revert "Setup android-base logging in sideload."
Diffstat (limited to 'logging_android.cc')
-rw-r--r--logging_android.cc58
1 files changed, 51 insertions, 7 deletions
diff --git a/logging_android.cc b/logging_android.cc
index d5aac6df..88b068bc 100644
--- a/logging_android.cc
+++ b/logging_android.cc
@@ -39,6 +39,12 @@
using std::string;
+#ifdef _UE_SIDELOAD
+constexpr bool kSideload = true;
+#else
+constexpr bool kSideload = false;
+#endif
+
namespace chromeos_update_engine {
namespace {
@@ -141,13 +147,11 @@ class FileLogger {
return;
}
- // libchrome add a newline character to |message|. Strip it.
- std::string_view message_no_newline =
+ std::string_view message_str =
log_message->message != nullptr ? log_message->message : "";
- ignore_result(android::base::ConsumeSuffix(&message_no_newline, "\n"));
WriteToFd(GetPrefix(log_message));
- WriteToFd(message_no_newline);
+ WriteToFd(message_str);
WriteToFd("\n");
}
@@ -187,7 +191,13 @@ class CombinedLogger {
public:
CombinedLogger(bool log_to_system, bool log_to_file) {
if (log_to_system) {
- loggers_.push_back(__android_log_logd_logger);
+ if (kSideload) {
+ // No logd in sideload. Use stdout.
+ // recovery has already redirected stdio properly.
+ loggers_.push_back(__android_log_stderr_logger);
+ } else {
+ loggers_.push_back(__android_log_logd_logger);
+ }
}
if (log_to_file) {
loggers_.push_back(std::move(FileLogger(SetupLogFile(kSystemLogsRoot))));
@@ -203,6 +213,39 @@ class CombinedLogger {
std::vector<LoggerFunction> loggers_;
};
+// Redirect all libchrome logs to liblog using our custom handler that does
+// not call __android_log_write and explicitly write to stderr at the same
+// time. The preset CombinedLogger already writes to stderr properly.
+bool RedirectToLiblog(int severity,
+ const char* file,
+ int line,
+ size_t message_start,
+ const std::string& str_newline) {
+ android_LogPriority priority =
+ (severity < 0) ? ANDROID_LOG_VERBOSE : ANDROID_LOG_UNKNOWN;
+ switch (severity) {
+ case logging::LOG_INFO:
+ priority = ANDROID_LOG_INFO;
+ break;
+ case logging::LOG_WARNING:
+ priority = ANDROID_LOG_WARN;
+ break;
+ case logging::LOG_ERROR:
+ priority = ANDROID_LOG_ERROR;
+ break;
+ case logging::LOG_FATAL:
+ priority = ANDROID_LOG_FATAL;
+ break;
+ }
+ std::string_view sv = str_newline;
+ ignore_result(android::base::ConsumeSuffix(&sv, "\n"));
+ std::string str(sv.data(), sv.size());
+ // This will eventually be redirected to CombinedLogger.
+ // |tag| is ignored by CombinedLogger, so just leave it empty.
+ __android_log_write(priority, "" /* tag */, str.c_str());
+ return true;
+}
+
} // namespace
void SetupLogging(bool log_to_system, bool log_to_file) {
@@ -219,14 +262,15 @@ void SetupLogging(bool log_to_system, bool log_to_file) {
// libchrome logging should not log to file.
logging::LoggingSettings log_settings;
log_settings.lock_log = logging::DONT_LOCK_LOG_FILE;
- log_settings.logging_dest = static_cast<logging::LoggingDestination>(
- logging::LOG_TO_SYSTEM_DEBUG_LOG);
+ log_settings.logging_dest =
+ static_cast<logging::LoggingDestination>(logging::LOG_NONE);
log_settings.log_file = nullptr;
logging::InitLogging(log_settings);
logging::SetLogItems(false /* enable_process_id */,
false /* enable_thread_id */,
false /* enable_timestamp */,
false /* enable_tickcount */);
+ logging::SetLogMessageHandler(&RedirectToLiblog);
}
} // namespace chromeos_update_engine