Home | History | Annotate | Download | only in bugreport
      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;
     18 
     19 import com.android.bugreport.bugreport.Bugreport;
     20 import com.android.bugreport.bugreport.BugreportParser;
     21 import com.android.bugreport.html.Renderer;
     22 import com.android.bugreport.inspector.Inspector;
     23 import com.android.bugreport.logcat.LogcatParser;
     24 import com.android.bugreport.monkey.MonkeyLogParser;
     25 import com.android.bugreport.util.Lines;
     26 
     27 import java.io.BufferedReader;
     28 import java.io.FileReader;
     29 import java.io.IOException;
     30 
     31 /**
     32  * Main entry point.
     33  */
     34 public class Main {
     35     /**
     36      * Main entry point.
     37      */
     38     public static void main(String[] args) {
     39         System.exit(run(args));
     40     }
     41 
     42     /**
     43      * Parse the args and run it.
     44      *
     45      * @return the process exit code.
     46      */
     47     public static int run(String[] args) {
     48         // Parse args
     49         final Options options = Options.parseArgs(args);
     50         if (options.errorIndex >= 0) {
     51             return usage();
     52         }
     53 
     54         // Run
     55         return run(options);
     56     }
     57 
     58     /**
     59      * Prints the usage message to stderr and returns 1.
     60      */
     61     private static int usage() {
     62         System.err.println("usage: bugreport --monkey MONKEYLOG --html HTML --logcat SYSTEMLOG"
     63                 + " BUGREPORT\n");
     64         return 1;
     65     }
     66 
     67     /**
     68      * Run the tool with the given files.
     69      *
     70      * @return the process exit code.
     71      */
     72     public static int run(Options options) {
     73         Bugreport bugreport = null;
     74 
     75         // Parse bugreport file
     76         try {
     77             final BugreportParser parser = new BugreportParser();
     78             bugreport = parser.parse(Lines.readLines(options.bugreport));
     79         } catch (IOException ex) {
     80             System.err.println("Error reading monkey file: " + options.bugreport);
     81             System.err.println("Error: " + ex.getMessage());
     82             return 1;
     83         }
     84 
     85         // Also parse the monkey log if we have one. That parser will merge
     86         // into the Bugreport we already parsed.
     87         if (options.monkey != null) {
     88             try {
     89                 final MonkeyLogParser parser = new MonkeyLogParser();
     90                 parser.parse(bugreport, Lines.readLines(options.monkey));
     91             } catch (IOException ex) {
     92                 System.err.println("Error reading bugreport file: " + options.bugreport);
     93                 System.err.println("Error: " + ex.getMessage());
     94                 return 1;
     95             }
     96         }
     97 
     98         // Also parse the logcat if we have one. That parser will merge
     99         // into the Bugreport we already parsed.
    100         if (options.logcat != null) {
    101             try {
    102                 final LogcatParser parser = new LogcatParser();
    103                 bugreport.logcat = parser.parse(Lines.readLines(options.logcat));
    104             } catch (IOException ex) {
    105                 System.err.println("Error reading bugreport file: " + options.bugreport);
    106                 System.err.println("Error: " + ex.getMessage());
    107                 return 1;
    108             }
    109         }
    110 
    111         // Inspect the Failure and see if we can figure out what's going on.
    112         // Fills in the additional fields in the Anr object.
    113         Inspector.inspect(bugreport);
    114 
    115         // For now, since all we do is ANRs, just bail out if there wasn't one.
    116         if (bugreport.anr == null) {
    117             System.err.println("No anr!");
    118             return 0;
    119         }
    120 
    121         // Write the html
    122         try {
    123             Renderer renderer = new Renderer();
    124             renderer.render(options.html, bugreport);
    125         } catch (IOException ex) {
    126             System.err.println("Error reading output file: " + options.html);
    127             System.err.println("Error: " + ex.getMessage());
    128             return 1;
    129         }
    130 
    131         return 0;
    132     }
    133 }
    134 
    135