Add log_displayer support for -m and -p.

These options work as advertised in the help message. Also add
a new function in util for converting strings to numbers.

Change-Id: I33ab9add3f55c9139557b30ffe4de633782933e5
diff --git a/aos/linux_code/logging/log_displayer.cc b/aos/linux_code/logging/log_displayer.cc
index 8141cd4..5eb6f8a 100644
--- a/aos/linux_code/logging/log_displayer.cc
+++ b/aos/linux_code/logging/log_displayer.cc
@@ -7,11 +7,14 @@
 #include <inttypes.h>
 
 #include <algorithm>
+#include <memory>
+#include <string>
 
 #include "aos/linux_code/logging/binary_log_file.h"
 #include "aos/common/queue_types.h"
 #include "aos/common/logging/logging_impl.h"
 #include "aos/common/logging/logging_printf_formats.h"
+#include "aos/common/util/string_to_num.h"
 
 using ::aos::logging::linux_code::LogFileMessageHeader;
 
@@ -24,14 +27,14 @@
     "  -n, --name NAME       only display entries from processes named NAME\n"
     "  -l, --level LEVEL     "
       "only display log entries at least as important as LEVEL\n"
-    "  // -p, --pid PID        only display log entries from process PID\n"
+    "  -p, --pid PID        only display log entries from process PID\n"
     "  -f, --follow          "
       "wait when the end of the file is reached (implies --end)\n"
     "  -t, --terminate       stop when the end of file is reached (default)\n"
     "  -b, --beginning       start at the beginning of the file (default)\n"
     "  -e, --end             start at the end of the file\n"
     "  -s, --skip NUMBER     skip NUMBER matching logs\n"
-    "  // -m, --max NUMBER     only display up to NUMBER logs\n"
+    "  -m, --max NUMBER     only display up to NUMBER logs\n"
     "  // -o, --format FORMAT  use FORMAT to display log entries\n"
     "  -h, --help            display this help and exit\n"
     "\n"
@@ -56,6 +59,8 @@
   // Whether we need to skip everything until we get to the end of the file.
   bool skip_to_end = false;
   const char *filename = "aos_log-current";
+  int display_max = 0;
+  int32_t source_pid = -1;
 
   ::aos::logging::Init();
   ::aos::logging::AddImplementation(
@@ -102,7 +107,14 @@
         }
         break;
       case 'p':
-        abort();
+        if (!::aos::util::StringToInteger(::std::string(optarg), &source_pid)) {
+          fprintf(stderr, "ERROR: -p expects a number, not '%s'.\n", optarg);
+          exit(EXIT_FAILURE);
+        }
+        if (source_pid < 0) {
+          fprintf(stderr, "LogDisplayer: invalid pid '%s'\n", optarg);
+          exit(EXIT_FAILURE);
+        }
         break;
       case 'f':
         follow = true;
@@ -118,7 +130,15 @@
         skip_to_end = true;
         break;
       case 'm':
-        abort();
+        if (!::aos::util::StringToInteger(::std::string(optarg), &display_max)) {
+          fprintf(stderr, "ERROR: -m expects a number, not '%s'.\n", optarg);
+          exit(EXIT_FAILURE);
+        }
+        if (display_max <= 0) {
+          fprintf(stderr, "LogDisplayer: invalid max log number '%s'\n",
+              optarg);
+          exit(EXIT_FAILURE);
+        }
         break;
       case 'o':
         abort();
@@ -161,6 +181,7 @@
   }
 
   const LogFileMessageHeader *msg;
+  int displayed = 0;
   do {
     msg = reader.ReadNextMessage(follow);
     if (msg == NULL) {
@@ -168,6 +189,11 @@
       return 0;
     }
 
+    if (source_pid >= 0 && msg->source != source_pid) {
+      // Message is from the wrong process.
+      continue;
+    }
+
     if (msg->type == LogFileMessageHeader::MessageType::kStructType) {
       size_t bytes = msg->message_size;
       ::aos::MessageType *type = ::aos::MessageType::Deserialize(
@@ -196,6 +222,11 @@
       }
     }
 
+    if (display_max && displayed++ >= display_max) {
+      fputs("Not displaying the rest of the messages.\n", stderr);
+      return 0;
+    }
+
     const char *position =
         reinterpret_cast<const char *>(msg + 1) + msg->name_size;
 #define BASE_ARGS                                                           \