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 private Long offset = null; 36 37 @Override onTestRunStart(DeviceMetricData runData)38 public void onTestRunStart(DeviceMetricData runData) { 39 offset = null; 40 // TODO: Improve the offset from the start of the method instead. 41 try (InputStreamSource source = getLogger().getLog()) { 42 if (source == null) { 43 CLog.e( 44 "Could not obtain the host logs for debugging. It won't be available " 45 + "in the event of test cases failures."); 46 return; 47 } 48 offset = source.size(); 49 } 50 } 51 52 @Override onTestFail(DeviceMetricData testData, TestDescription test)53 public void onTestFail(DeviceMetricData testData, TestDescription test) { 54 if (offset == null) { 55 return; 56 } 57 try (InputStreamSource source = getLogger().getLog()) { 58 if (source == null) { 59 return; 60 } 61 try (InputStream stream = source.createInputStream()) { 62 stream.skip(offset); 63 try (InputStreamSource logSource = 64 new SnapshotInputStreamSource("host-log-failure", stream)) { 65 super.testLog( 66 String.format(NAME_FORMAT, test.toString()), 67 LogDataType.TEXT, 68 logSource); 69 } 70 } 71 } catch (IOException e) { 72 CLog.e(e); 73 } 74 } 75 76 @VisibleForTesting getLogger()77 ILeveledLogOutput getLogger() { 78 LogRegistry registry = (LogRegistry) LogRegistry.getLogRegistry(); 79 return registry.getLogger(); 80 } 81 } 82