1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.bugreport.logcat;
18 
19 import com.android.bugreport.util.Line;
20 import com.android.bugreport.util.Lines;
21 import com.android.bugreport.util.Utils;
22 
23 import java.util.regex.Pattern;
24 import java.util.regex.Matcher;
25 
26 /**
27  * Parses a stream of text as a logcat.
28  */
29 public class LogcatParser {
30 
31     public static final Pattern BUFFER_BEGIN_RE = Pattern.compile(
32             "--------- beginning of (.*)");
33     private static final Pattern LOG_LINE_RE = Pattern.compile(
34             "(" + Utils.DATE_TIME_MS_PATTERN
35                 + "\\s+(\\d+)\\s+(\\d+)\\s+(.)\\s+)(.*?):\\s(.*)");
36 
37     private final Matcher mBufferBeginRe = BUFFER_BEGIN_RE.matcher("");
38     private final Matcher mLogLineRe = LOG_LINE_RE.matcher("");
39 
40     /**
41      * Constructor
42      */
LogcatParser()43     public LogcatParser() {
44     }
45 
46     /**
47      * Parse the logcat lines, returning a Logcat object.
48      */
parse(Lines<? extends Line> lines)49     public Logcat parse(Lines<? extends Line> lines) {
50         final Logcat result = new Logcat();
51 
52         Matcher m;
53         int lineno = 0;
54 
55         while (lines.hasNext()) {
56             final Line line = lines.next();
57             final String text = line.text;
58 
59             if ((m = Utils.match(mBufferBeginRe, text)) != null) {
60                 // Beginning of buffer marker
61                 final LogLine ll = new LogLine();
62 
63                 ll.lineno = lineno++;
64                 ll.rawText = text;
65                 ll.bufferBegin = m.group(1);
66 
67                 result.lines.add(ll);
68             } else if ((m = Utils.match(mLogLineRe, text)) != null) {
69                 // Matched line
70                 final LogLine ll = new LogLine();
71 
72                 ll.lineno = lineno++;
73                 ll.rawText = text;
74                 ll.header = m.group(1);
75                 ll.time = Utils.parseCalendar(m, 2, true);
76                 ll.pid = Integer.parseInt(m.group(9));
77                 ll.tid = Integer.parseInt(m.group(10));
78                 ll.level = m.group(11).charAt(0);
79                 ll.tag = m.group(12);
80                 ll.text = m.group(13);
81 
82                 result.lines.add(ll);
83 
84                 if (false) {
85                     System.out.println("LogLine: time=" + ll.time + " pid=" + ll.pid
86                             + " tid=" + ll.tid + " level=" + ll.level + " tag=" + ll.tag
87                             + " text=" + ll.text);
88                 }
89             } else {
90                 if (false) {
91                     System.out.println("\nUNMATCHED: [" + text + "]");
92                 }
93             }
94         }
95 
96         return result;
97     }
98 
99 }
100