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 package com.android.tradefed.log; 17 18 import com.android.tradefed.device.BackgroundDeviceAction; 19 import com.android.tradefed.device.ITestDevice; 20 import com.android.tradefed.device.LargeOutputReceiver; 21 import com.android.tradefed.result.ITestInvocationListener; 22 import com.android.tradefed.result.InputStreamSource; 23 import com.android.tradefed.result.LogDataType; 24 import com.android.tradefed.util.StreamUtil; 25 26 public class LogReceiver { 27 private BackgroundDeviceAction mDeviceAction; 28 private LargeOutputReceiver mReceiver; 29 private String mDesc; 30 31 private static final int DELAY = 5; 32 private static final int LOG_SIZE = 20 * 1024 * 1024; 33 34 public LogReceiver(ITestDevice device, String cmd, String desc) { 35 this(device, cmd, LOG_SIZE, DELAY, desc); 36 } 37 38 public LogReceiver(ITestDevice device, String cmd, String desc, 39 long logcat_size,int delay_secs) { 40 this(device, cmd, logcat_size, delay_secs, desc); 41 } 42 43 private LogReceiver(ITestDevice device, String cmd, 44 long maxFileSize, int logStartDelay, String desc) { 45 46 mReceiver = new LargeOutputReceiver(desc, device.getSerialNumber(), 47 maxFileSize); 48 mDeviceAction = new BackgroundDeviceAction(cmd, desc, device, 49 mReceiver, logStartDelay); 50 mDesc = desc; 51 } 52 53 public String getDescriptor() { 54 return mDesc; 55 } 56 57 public void start() { 58 mDeviceAction.start(); 59 } 60 61 public void stop() { 62 mDeviceAction.cancel(); 63 mReceiver.cancel(); 64 mReceiver.delete(); 65 } 66 67 public InputStreamSource getData() { 68 return mReceiver.getData(); 69 } 70 71 public InputStreamSource getLogcatData(int maxBytes) { 72 return mReceiver.getData(maxBytes); 73 } 74 75 public void clear() { 76 mReceiver.clear(); 77 } 78 79 public void postLog(ITestInvocationListener listener) { 80 InputStreamSource stream = getData(); 81 try { 82 listener.testLog(getDescriptor(), LogDataType.TEXT, getData()); 83 } finally { 84 StreamUtil.cancel(stream); 85 } 86 } 87 }