logcat: fix -D when using filters

`logcat init:E *:F -D` should print only error messages and above from
init and only fatal messages from other tags.  It should print a
divider (`--------- switch to kernel` for example) when switching to a
new log buffer and printing a message.

However, currently the `--------- switch to kernel` messages are
printed before using the filters, therefore the output is spammed with
extraneous loops of

--------- switch to kernel
--------- switch to main
--------- switch to kernel
--------- switch to main
...

This change fixes this by printing dividers immediately before
printing a log message.

Test: `logcat init:V CompatConfig:V *:F -D` works correctly
Test: `logcat` works correctly
Change-Id: Icf207a126f4f1ef79acc4a12aaf134493529a8e5
diff --git a/logcat/logcat.cpp b/logcat/logcat.cpp
index 6b7e016..67ee676 100644
--- a/logcat/logcat.cpp
+++ b/logcat/logcat.cpp
@@ -101,6 +101,7 @@
     bool print_it_anyways_ = false;
 
     // For PrintDividers()
+    bool print_dividers_ = false;
     log_id_t last_printed_id_ = LOG_ID_MAX;
     bool printed_start_[LOG_ID_MAX] = {};
 
@@ -215,6 +216,8 @@
 
         print_count_ += match;
         if (match || print_it_anyways_) {
+            PrintDividers(buf->id(), print_dividers_);
+
             bytesWritten = android_log_printLogLine(logformat_.get(), output_fd_.get(), &entry);
 
             if (bytesWritten < 0) {
@@ -231,7 +234,7 @@
 }
 
 void Logcat::PrintDividers(log_id_t log_id, bool print_dividers) {
-    if (log_id == last_printed_id_ || print_binary_) {
+    if (log_id == last_printed_id_) {
         return;
     }
     if (!printed_start_[log_id] || print_dividers) {
@@ -530,7 +533,6 @@
     bool getLogSize = false;
     bool getPruneList = false;
     bool printStatistics = false;
-    bool printDividers = false;
     unsigned long setLogSize = 0;
     const char* setPruneList = nullptr;
     const char* setId = nullptr;
@@ -696,7 +698,7 @@
                 break;
 
             case 'D':
-                printDividers = true;
+                print_dividers_ = true;
                 break;
 
             case 'e':
@@ -1193,8 +1195,6 @@
             continue;
         }
 
-        PrintDividers(log_msg.id(), printDividers);
-
         if (print_binary_) {
             if (!WriteFully(output_fd_, &log_msg, log_msg.len())) {
                 error(EXIT_FAILURE, errno, "Failed to write to output fd");