Avoid logging extra empty lines in android DCHECK_IS_ON builds

The string split and multiple __android_log_write calls added in
https://chromium-review.googlesource.com/c/1281883 caused unnecessary
empty log lines to be output after each LOG(). The log line gets a
newline appended to str_newline at the beginning of ~LogMessage, and
that makes StringSplit return an empty string as the last element,
which then causes an empty log line.

Fix by skipping the last empty element since it's always empty.

Change-Id: Ib486d1bfdc8e98a5272b8b7dc214a995c625c85b
Reviewed-on: https://chromium-review.googlesource.com/c/1469862
Commit-Queue: Tomasz Śniatowski <[email protected]>
Reviewed-by: danakj <[email protected]>
Cr-Commit-Position: refs/heads/master@{#632546}
diff --git a/base/logging.cc b/base/logging.cc
index fb64437..58f3d15f 100644
--- a/base/logging.cc
+++ b/base/logging.cc
@@ -768,15 +768,21 @@
         priority = ANDROID_LOG_FATAL;
         break;
     }
+    const char kAndroidLogTag[] = "chromium";
 #if DCHECK_IS_ON()
     // Split the output by new lines to prevent the Android system from
     // truncating the log.
-    for (const auto& line : base::SplitString(
-             str_newline, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL))
-      __android_log_write(priority, "chromium", line.c_str());
+    std::vector<std::string> lines = base::SplitString(
+        str_newline, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
+    // str_newline has an extra newline appended to it (at the top of this
+    // function), so skip the last split element to avoid needlessly
+    // logging an empty string.
+    lines.pop_back();
+    for (const auto& line : lines)
+      __android_log_write(priority, kAndroidLogTag, line.c_str());
 #else
     // The Android system may truncate the string if it's too long.
-    __android_log_write(priority, "chromium", str_newline.c_str());
+    __android_log_write(priority, kAndroidLogTag, str_newline.c_str());
 #endif
 #endif  // OS_ANDROID
     ignore_result(fwrite(str_newline.data(), str_newline.size(), 1, stderr));