Add --count argument to log_cat to limit number of messages

This will print the first count messages only.

Change-Id: If7e0e4e8d631619592e893b26949a91d5d2879d1
Signed-off-by: Tyler Chatow <tyler.chatow@bluerivertech.com>
diff --git a/aos/events/logging/log_cat.cc b/aos/events/logging/log_cat.cc
index 342a491..a26eb49 100644
--- a/aos/events/logging/log_cat.cc
+++ b/aos/events/logging/log_cat.cc
@@ -37,6 +37,10 @@
 DEFINE_bool(print, true,
             "If true, actually print the messages.  If false, discard them, "
             "confirming they can be parsed.");
+DEFINE_uint64(
+    count, 0,
+    "If >0, log_cat will exit after printing this many messages.  This "
+    "includes messages from before the start of the log if --fetch is set.");
 DEFINE_bool(print_parts_only, false,
             "If true, only print out the results of logfile sorting.");
 DEFINE_bool(channels, false,
@@ -247,6 +251,8 @@
 
   bool found_channel = false;
 
+  uint64_t message_print_counter = 0;
+
   for (const aos::Node *node :
        aos::configuration::GetNodes(event_loop_factory.configuration())) {
     std::unique_ptr<aos::EventLoop> printer_event_loop =
@@ -309,10 +315,15 @@
         }
 
         printer_event_loop->MakeRawWatcher(
-            channel, [channel, node_name, &builder](const aos::Context &context,
-                                                    const void * /*message*/) {
+            channel, [channel, node_name, &builder, &event_loop_factory,
+                      &message_print_counter](const aos::Context &context,
+                                               const void * /*message*/) {
               if (FLAGS_print) {
                 PrintMessage(node_name, channel, context, &builder);
+                ++message_print_counter;
+                if (FLAGS_count > 0 && message_print_counter >= FLAGS_count) {
+                  event_loop_factory.Exit();
+                }
               }
             });
         found_channel = true;
@@ -325,6 +336,12 @@
       if (FLAGS_print) {
         PrintMessage(message.node_name, message.fetcher->channel(),
                      message.fetcher->context(), &builder);
+        ++message_print_counter;
+        if (FLAGS_count > 0 && message_print_counter >= FLAGS_count) {
+          // We are done.  Clean up and exit.
+          reader.Deregister();
+          return 0;
+        }
       }
     }
     printer_event_loops.emplace_back(std::move(printer_event_loop));