Merge "Simpleperf: add --log option to ease debugging."
diff --git a/simpleperf/cmd_help.cpp b/simpleperf/cmd_help.cpp
index 75df732..cc66376 100644
--- a/simpleperf/cmd_help.cpp
+++ b/simpleperf/cmd_help.cpp
@@ -55,10 +55,16 @@
 }
 
 void HelpCommand::PrintShortHelp() {
-  printf("Usage: simpleperf [--help] subcommand [args_for_subcommand]\n\n");
+  printf(
+      "Usage: simpleperf [common options] subcommand [args_for_subcommand]\n"
+      "common options:\n"
+      "    -h/--help     Print this help information.\n"
+      "    --log <severity> Set the minimum severity of logging. Possible severities\n"
+      "                     include debug, warning, error, fatal. Default is error.\n"
+      "subcommands:\n");
   for (auto& cmd_name : GetAllCommandNames()) {
     std::unique_ptr<Command> cmd = CreateCommandInstance(cmd_name);
-    printf("%-20s%s\n", cmd_name.c_str(), cmd->ShortHelpString().c_str());
+    printf("    %-20s%s\n", cmd_name.c_str(), cmd->ShortHelpString().c_str());
   }
 }
 
diff --git a/simpleperf/main.cpp b/simpleperf/main.cpp
index 93c52e5..7cc04b8 100644
--- a/simpleperf/main.cpp
+++ b/simpleperf/main.cpp
@@ -15,6 +15,7 @@
  */
 
 #include <string.h>
+#include <map>
 #include <string>
 #include <vector>
 
@@ -22,9 +23,17 @@
 
 #include "command.h"
 
+static std::map<std::string, android::base::LogSeverity> log_severity_map = {
+    {"debug", android::base::DEBUG},
+    {"warning", android::base::WARNING},
+    {"error", android::base::ERROR},
+    {"fatal", android::base::FATAL},
+};
+
 int main(int argc, char** argv) {
   InitLogging(argv, android::base::StderrLogger);
   std::vector<std::string> args;
+  android::base::LogSeverity log_severity = android::base::ERROR;
 
   if (argc == 1) {
     args.push_back("help");
@@ -32,11 +41,26 @@
     for (int i = 1; i < argc; ++i) {
       if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) {
         args.insert(args.begin(), "help");
+      } else if (strcmp(argv[i], "--log") == 0) {
+        if (i + 1 < argc) {
+          ++i;
+          auto it = log_severity_map.find(argv[i]);
+          if (it != log_severity_map.end()) {
+            log_severity = it->second;
+          } else {
+            LOG(ERROR) << "Unknown log severity: " << argv[i];
+            return 1;
+          }
+        } else {
+          LOG(ERROR) << "Missing argument for --log option.\n";
+          return 1;
+        }
       } else {
         args.push_back(argv[i]);
       }
     }
   }
+  android::base::ScopedLogSeverity severity(log_severity);
 
   std::unique_ptr<Command> command = CreateCommandInstance(args[0]);
   if (command == nullptr) {