apps: Last logcat section should be skipped when processing bugreport

Android bugreports may contain a "LAST LOGCAT" section.  This should be
skipped and the most recent logcat section should be processed.

BUG=chromium:591926
TEST=manually with bugreports
TEST=ran 'grunt test'

Change-Id: I4cd79c0c2cd185d202b4d74c21d969b637767e05
diff --git a/log_helper.js b/log_helper.js
index b8f2a45..b2b6ffd 100644
--- a/log_helper.js
+++ b/log_helper.js
@@ -51,7 +51,11 @@
  */
 logHelper.ANDROID_SECTION_END = /\[logcat: \d+.\d+s elapsed\]/;
 
-
+/**
+ * RegExp for start of "LAST LOGCAT" section.
+ * @type {RegExp}
+ */
+logHelper.ANDROID_LAST_LOGCAT_SECTION = / LAST LOGCAT /;
 
 /**
  * RegExp for start of Android bug report file
@@ -160,7 +164,13 @@
     handler: function(logHolder, logLines, i) {
       logHolder.fileType = 'android_log';
       while (i < logLines.length) {
-        if (logHelper.ANDROID_MAIN_SECTION.test(logLines[i])) {
+        if (logHelper.ANDROID_LAST_LOGCAT_SECTION.test(logLines[i])) {
+          // this is the "LAST LOGCAT" section...  skip.
+          var lastLogcatSectionLength = logHelper.getSubLog(logLines, i).length;
+          console.log('skipping ' + lastLogcatSectionLength +
+              ' line(s) of \"LAST LOGCAT\" section');
+          i = i + lastLogcatSectionLength;
+        } else if (logHelper.ANDROID_MAIN_SECTION.test(logLines[i])) {
           // found start of main log
           console.log('found start of main section');
           break;
diff --git a/test/spec/LogHelperSpec.js b/test/spec/LogHelperSpec.js
index de93554..5eef40d 100644
--- a/test/spec/LogHelperSpec.js
+++ b/test/spec/LogHelperSpec.js
@@ -124,6 +124,39 @@
     expect(logHolder.brillolog).not.toBeDefined();
   });
 
+  it('should ignore \"LAST LOGCAT\" section of android bug report file',
+      function() {
+    var androidBugReport = [
+        '========================================================',
+        '== dumpstate: 2015-11-20 12:11:35',
+        '========================================================',
+        '',
+        'Build: bullhead-userdebug 6.0.1 MMB29B 2387154 dev-keys',
+        '11-20 12:08:02.926   903  2909 D ConnectivityService: ' +
+            ' sendStickyBroadcast: action=android.net.conn.CONNECTIVITY_CHANGE',
+        '------ LAST LOGCAT (logcat -L -v threadtime -b all -d *:v) ------',
+        '--------- beginning of main',
+        '05-26 01:34:12.948   313   313 W libsuspend: Error writing \'on\'' +
+            ' to /sys/power/state: Invalid argument',
+        '[logcat: 0.015s elapsed]',
+        '--------- beginning of main',
+        '11-20 12:08:06.463   489   897 D CommandListener: Setting iface cfg',
+        '11-20 12:08:06.521   903  2909 D ConnectivityService: Adding iface ' +
+            'wlan0 to network 169',
+        '[logcat: 0.530s elapsed]',
+        '',
+        '------ EVENT LOG (logcat -b events -v threadtime -d *:v) -----',
+        '11-20 12:01:45.958  3432  9804 I gtalk_conn_close: [276,8]'
+    ];
+    var logHolder = logHelper.detectFileType(androidBugReport);
+    expect(logHolder.fileType).toEqual('android_log');
+    expect(logHolder.androidlog.length).toEqual(2);
+    expect(logHolder.netlog).not.toBeDefined();
+    expect(logHolder.syslog).not.toBeDefined();
+    expect(logHolder.brillolog).not.toBeDefined();
+  });
+
+
   it('should ignore info outside main subsection of android bug report file',
      function() {
     var androidBugReport = [