Improve logging.

Change-Id: I768615168b863461313a1f07eab9a038ae7f1216
diff --git a/src/main/java/com/google/gwt/benchmark/BenchmarkingHarness.java b/src/main/java/com/google/gwt/benchmark/BenchmarkingHarness.java
index 1e48941..9d11b84 100644
--- a/src/main/java/com/google/gwt/benchmark/BenchmarkingHarness.java
+++ b/src/main/java/com/google/gwt/benchmark/BenchmarkingHarness.java
@@ -35,7 +35,11 @@
 import java.nio.file.Paths;
 import java.util.Date;
 import java.util.List;
+import java.util.logging.FileHandler;
+import java.util.logging.Handler;
+import java.util.logging.Level;
 import java.util.logging.Logger;
+import java.util.logging.SimpleFormatter;
 
 /**
  * Basic benchmarking harness.
@@ -46,8 +50,20 @@
     int numberOfRuns = DEFAULT_NUMBER_OF_RUNS;
     Path remoteGitMirrorPath;
     String startingRevisionId;
+    int revisionsToTest;
     Path toolsPath;
     Path workDirPath;
+
+    /* (non-Javadoc)
+     * @see java.lang.Object#toString()
+     */
+    @Override
+    public String toString() {
+      return "Parameters [numberOfRuns=" + numberOfRuns + ", remoteGitMirrorPath="
+          + remoteGitMirrorPath + ", startingRevisionId=" + startingRevisionId
+          + ", revisionsToTest=" + revisionsToTest + ", toolsPath=" + toolsPath + ", workDirPath="
+          + workDirPath + "]";
+    }
   }
 
   private static final int DEFAULT_NUMBER_OF_RUNS = 3;
@@ -70,16 +86,24 @@
       RunnerException, URISyntaxException {
 
     Parameters parameters = validateParameters(args);
-    benchmark(parameters.workDirPath, parameters.remoteGitMirrorPath, parameters.toolsPath,
-        parameters.numberOfRuns, parameters.startingRevisionId);
+    log.info(parameters.toString());
+    benchmark(parameters);
   }
 
   /**
    * Runs the benchmarks on all projects.
    */
-  private static void benchmark(Path workDirPath, Path mainRepoPath, Path svnToolsRepoPath,
-      int numberOfRuns, String startFromHash) throws IOException, GitAPIException, GitException,
-      RunnerException {
+  private static void benchmark(Parameters parameters) throws IOException, GitAPIException,
+      GitException, RunnerException {
+
+    Path workDirPath = parameters.workDirPath;
+    Path mainRepoPath = parameters.remoteGitMirrorPath;
+    Path svnToolsRepoPath = parameters.toolsPath;
+    int numberOfRuns = parameters.numberOfRuns;
+    String startFromHash = parameters.startingRevisionId;
+    int revisionsToTest = parameters.revisionsToTest;
+
+    configureGlobalLogger(workDirPath);
 
     Path scratchDirPath = workDirPath.resolve(SCRATCH_DIR);
     Path targetRepoPath = scratchDirPath.resolve(REPO_DIR);
@@ -100,6 +124,9 @@
     List<RevisionInfo> revisionsToBenchmark =
         GitInterface.getRevisionsAffectingTargets(targetRepoPath, ImmutableList.of("dev/core"),
             ImmutableList.of(".java"));
+    if (revisionsToTest > 0) {
+      revisionsToBenchmark = revisionsToBenchmark.subList(0, revisionsToTest);
+    }
     for (RevisionInfo revisionInfo : revisionsToBenchmark) {
       makePristineCheckout(mainRepoPath, targetRepoPath);
       GitInterface.checkout(targetRepoPath, revisionInfo.getCommitId());
@@ -108,6 +135,26 @@
   }
 
   /**
+   * @param workDirPath
+   * @throws IOException
+   * @throws SecurityException
+   */
+  private static void configureGlobalLogger(Path workDirPath) throws IOException, SecurityException {
+    // Record the log to a file.
+
+    Path logDirPath = workDirPath.resolve("logs");
+    logDirPath.toFile().mkdirs();
+    Handler logToFileHandler =
+        new FileHandler(logDirPath.resolve(
+            "test-" + String.format(TIME_FORMAT, new Date()) + ".log").toString(), false);
+    logToFileHandler.setFormatter(new SimpleFormatter());
+    // Get the top level logger to set global properties
+    Logger topLevelLogger = Logger.getGlobal().getParent();
+    topLevelLogger.addHandler(logToFileHandler);
+    topLevelLogger.setLevel(Level.INFO);
+  }
+
+  /**
    * Runs the benchmarks on all projects at the current revision.
    */
   private static void benchmarkAtCurrentRevision(Path workDirPath, Path svnToolsRepoPath,
@@ -163,7 +210,7 @@
           writeArtifactSizeStats(revInfo, compileStartTime, benchmarkStatsResultsFilePath,
               benchStartTime, stats);
         } catch (Exception e) {
-          log.severe("Benchmark was not successfull:" + e);
+          log.log(Level.SEVERE, "Benchmark was not successfull: " + e.getMessage(), e);
         }
       }
     }
@@ -214,7 +261,8 @@
    */
   private static void usageExit() {
     System.out
-        .println("usage: benchmark workdir gwt-remote-mirror svn-tools [#runs] [starting commit]");
+        .println("usage: benchmark workdir gwt-remote-mirror svn-tools [#runs] [starting commit]"
+            + " [revisions to test]");
     System.exit(1);
   }
 
@@ -223,7 +271,7 @@
    */
   private static Parameters validateParameters(String... args) {
     Parameters parameters = new Parameters();
-    if (args.length < 3 || args.length > 5) {
+    if (args.length < 3 || args.length > 6) {
       usageExit();
     }
 
@@ -237,6 +285,9 @@
       if (args.length > 4) {
         parameters.startingRevisionId = args[4];
       }
+      if (args.length > 5) {
+        parameters.revisionsToTest = Integer.parseInt(args[5]);
+      }
     } catch (Exception e) {
       usageExit();
     }