Home | History | Annotate | Download | only in device
      1 /*
      2  * Copyright (C) 2012 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 package com.android.tradefed.device;
     17 
     18 import com.android.tradefed.result.InputStreamSource;
     19 
     20 import com.google.errorprone.annotations.MustBeClosed;
     21 
     22 /**
     23  * Class that collects logcat in background. Continues to capture logcat even if device goes
     24  * offline then online.
     25  */
     26 public class LogcatReceiver implements ILogcatReceiver {
     27     private BackgroundDeviceAction mDeviceAction;
     28     private LargeOutputReceiver mReceiver;
     29 
     30     static final String LOGCAT_CMD = "logcat -v threadtime";
     31     private static final String LOGCAT_DESC = "logcat";
     32 
     33     /**
     34      * Creates an instance with any specified logcat command
     35      * @param device the device to start logcat on
     36      * @param logcatCmd the logcat command to run (including 'logcat' part), see details on
     37      *        available options in logcat help message
     38      * @param maxFileSize maximum file size, earlier lines will be discarded once size is reached
     39      * @param logStartDelay the delay to wait after the device becomes online
     40      */
     41     public LogcatReceiver(ITestDevice device, String logcatCmd,
     42             long maxFileSize, int logStartDelay) {
     43 
     44         mReceiver = new LargeOutputReceiver(LOGCAT_DESC, device.getSerialNumber(),
     45                 maxFileSize);
     46         // FIXME: remove mLogStartDelay. Currently delay starting logcat, as starting
     47         // immediately after a device comes online has caused adb instability
     48         mDeviceAction = new BackgroundDeviceAction(logcatCmd, LOGCAT_DESC, device,
     49                 mReceiver, logStartDelay);
     50     }
     51 
     52     /**
     53      * Creates an instance with default logcat 'threadtime' format
     54      * @param device the device to start logcat on
     55      * @param maxFileSize maximum file size, earlier lines will be discarded once size is reached
     56      * @param logStartDelay the delay to wait after the device becomes online
     57      */
     58     public LogcatReceiver(ITestDevice device, long maxFileSize, int logStartDelay) {
     59         this(device, LOGCAT_CMD, maxFileSize, logStartDelay);
     60     }
     61 
     62     @Override
     63     public void start() {
     64         mDeviceAction.start();
     65     }
     66 
     67     @Override
     68     public void stop() {
     69         mDeviceAction.cancel();
     70         mReceiver.cancel();
     71         mReceiver.delete();
     72     }
     73 
     74     @MustBeClosed
     75     @Override
     76     public InputStreamSource getLogcatData() {
     77         return mReceiver.getData();
     78     }
     79 
     80     @MustBeClosed
     81     @Override
     82     public InputStreamSource getLogcatData(int maxBytes) {
     83         return mReceiver.getData(maxBytes);
     84     }
     85 
     86     @Override
     87     public void clear() {
     88         mReceiver.clear();
     89     }
     90 }
     91