1 /* 2 * Copyright (C) 2022 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.microdroid.test.host; 18 19 import static com.android.tradefed.testtype.DeviceJUnit4ClassRunner.TestLogData; 20 21 import com.android.tradefed.device.DeviceNotAvailableException; 22 import com.android.tradefed.device.ITestDevice; 23 import com.android.tradefed.result.FileInputStreamSource; 24 import com.android.tradefed.result.LogDataType; 25 26 import java.io.File; 27 28 /** A helper class for archiving device log files to the host's tradefed output directory. */ 29 public abstract class LogArchiver { 30 /** 31 * Copy device log (then delete) to a tradefed output directory on the host. 32 * 33 * @param logs A {@link TestLogData} that needs to be owned by the actual test case. 34 * @param device The device to pull the log file from. 35 * @param remotePath The path on the device. 36 * @param localName Local file name to be copied to. 37 */ archiveLogThenDelete( TestLogData logs, ITestDevice device, String remotePath, String localName)38 public static void archiveLogThenDelete( 39 TestLogData logs, ITestDevice device, String remotePath, String localName) 40 throws DeviceNotAvailableException { 41 File logFile = device.pullFile(remotePath); 42 if (logFile != null) { 43 logs.addTestLog(localName, LogDataType.TEXT, new FileInputStreamSource(logFile)); 44 // Delete to avoid confusing logs from a previous run, just in case. 45 device.deleteFile(remotePath); 46 } 47 } 48 } 49