1 /* 2 * Copyright (C) 2019 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.metric; 17 18 import com.android.annotations.VisibleForTesting; 19 import com.android.tradefed.log.ILeveledLogOutput; 20 import com.android.tradefed.log.LogRegistry; 21 import com.android.tradefed.log.LogUtil.CLog; 22 import com.android.tradefed.result.InputStreamSource; 23 import com.android.tradefed.result.LogDataType; 24 import com.android.tradefed.result.SnapshotInputStreamSource; 25 import com.android.tradefed.result.TestDescription; 26 27 import java.io.IOException; 28 import java.io.InputStream; 29 30 /** Collector that will gather and log the host-side logs when a test case failure occurs. */ 31 public class DebugHostLogOnFailureCollector extends BaseDeviceMetricCollector { 32 33 private static final String NAME_FORMAT = "%s-debug-hostlog-on-failure"; 34 35 public Long offset = null; 36 37 @Override onTestRunStart(DeviceMetricData runData)38 public void onTestRunStart(DeviceMetricData runData) { 39 // TODO: Improve the offset from the start of the method instead. 40 offset = getLogger().getLog().size(); 41 } 42 43 @Override onTestFail(DeviceMetricData testData, TestDescription test)44 public void onTestFail(DeviceMetricData testData, TestDescription test) { 45 try (InputStreamSource source = getLogger().getLog(); 46 InputStream stream = source.createInputStream()) { 47 stream.skip(offset); 48 try (InputStreamSource logSource = 49 new SnapshotInputStreamSource("host-log-failure", stream)) { 50 super.testLog( 51 String.format(NAME_FORMAT, test.toString()), LogDataType.TEXT, logSource); 52 } 53 } catch (IOException e) { 54 CLog.e(e); 55 } 56 } 57 58 @VisibleForTesting getLogger()59 ILeveledLogOutput getLogger() { 60 LogRegistry registry = (LogRegistry) LogRegistry.getLogRegistry(); 61 return registry.getLogger(); 62 } 63 } 64