Stop parsing between reboot and new log.

Bug: 12104558
Change-Id: Ide5e8938e2452b5b653739480666229ed28203b3
diff --git a/src/com/android/loganalysis/parser/LogcatParser.java b/src/com/android/loganalysis/parser/LogcatParser.java
index 4a709a0..ffd8fb2 100644
--- a/src/com/android/loganalysis/parser/LogcatParser.java
+++ b/src/com/android/loganalysis/parser/LogcatParser.java
@@ -117,6 +117,8 @@
     private Date mStartTime = null;
     private Date mStopTime = null;
 
+    private boolean mIsParsing = true;
+
     /**
      * Constructor for {@link LogcatParser}.
      */
@@ -209,15 +211,27 @@
             tag = tm.group(3);
             pid = Integer.parseInt(tm.group(4));
             msg = tm.group(5);
-        } else {
-            // CLog.w("Failed to parse line '%s'", line);
-            return;
         }
 
-        if (mStartTime == null) {
-            mStartTime = time;
+        if (time != null) {
+            if (mStartTime == null) {
+                mStartTime = time;
+            }
+            mStopTime = time;
         }
-        mStopTime = time;
+
+        // Don't parse any lines after device begins reboot until a new log is detected.
+        if ("I".equals(level) && "ShutdownThread".equals(tag) &&
+                Pattern.matches("Rebooting, reason: .*", msg)) {
+            mIsParsing = false;
+        }
+        if (Pattern.matches(".*--------- beginning of /dev/log/main", line)) {
+            mIsParsing = true;
+        }
+
+        if (!mIsParsing || !(m.matches() || tm.matches())) {
+            return;
+        }
 
         // ANRs are split when START matches a line.  The newest entry is kept in the dataMap
         // for quick lookup while all entries are added to the list.
diff --git a/tests/src/com/android/loganalysis/parser/LogcatParserTest.java b/tests/src/com/android/loganalysis/parser/LogcatParserTest.java
index 4ae1460..bfc9297 100644
--- a/tests/src/com/android/loganalysis/parser/LogcatParserTest.java
+++ b/tests/src/com/android/loganalysis/parser/LogcatParserTest.java
@@ -431,6 +431,50 @@
     }
 
     /**
+     * Test that events while the device is rebooting are ignored.
+     */
+    public void testParse_reboot() throws ParseException {
+        List<String> lines = Arrays.asList(
+                "04-25 09:15:47.799   123  3082 I ShutdownThread: Rebooting, reason: null",
+                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: java.lang.Exception",
+                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: \tat class.method1(Class.java:1)",
+                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: \tat class.method2(Class.java:2)",
+                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: \tat class.method3(Class.java:3)");
+
+        LogcatItem logcat = new LogcatParser("2012").parse(lines);
+        assertNotNull(logcat);
+        assertEquals(parseTime("2012-04-25 09:15:47.799"), logcat.getStartTime());
+        assertEquals(parseTime("2012-04-25 09:55:47.799"), logcat.getStopTime());
+        assertEquals(0, logcat.getEvents().size());
+    }
+
+    /**
+     * Test that events while the device is rebooting are ignored, but devices after the reboot are
+     * captured.
+     */
+    public void testParse_reboot_resume() throws ParseException {
+        List<String> lines = Arrays.asList(
+                "04-25 09:15:47.799   123  3082 I ShutdownThread: Rebooting, reason: null",
+                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: java.lang.Exception",
+                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: \tat class.method1(Class.java:1)",
+                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: \tat class.method2(Class.java:2)",
+                "04-25 09:55:47.799  3064  3082 E AndroidRuntime: \tat class.method3(Class.java:3)",
+                "logcat interrupted. May see duplicated content in log.--------- beginning of /dev/log/main",
+                "04-25 09:59:47.799  3064  3082 E AndroidRuntime: java.lang.Exception2",
+                "04-25 09:59:47.799  3064  3082 E AndroidRuntime: \tat class.method1(Class.java:1)",
+                "04-25 09:59:47.799  3064  3082 E AndroidRuntime: \tat class.method2(Class.java:2)",
+                "04-25 09:59:47.799  3064  3082 E AndroidRuntime: \tat class.method3(Class.java:3)");
+
+
+        LogcatItem logcat = new LogcatParser("2012").parse(lines);
+        assertNotNull(logcat);
+        assertEquals(parseTime("2012-04-25 09:15:47.799"), logcat.getStartTime());
+        assertEquals(parseTime("2012-04-25 09:59:47.799"), logcat.getStopTime());
+        assertEquals(1, logcat.getEvents().size());
+        assertEquals("java.lang.Exception2", logcat.getJavaCrashes().get(0).getException());
+    }
+
+    /**
      * Test that the time logcat format can be parsed.
      */
     public void testParse_time() throws ParseException {