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     /** Copy device log (then delete) to a tradefed output directory on the host.
31      *
32      * @param logs A {@link TestLogData} that needs to be owned by the actual test case.
33      * @param device The device to pull the log file from.
34      * @param remotePath The path on the device.
35      * @param localName Local file name to be copied to.
36      */
archiveLogThenDelete(TestLogData logs, ITestDevice device, String remotePath, String localName)37     public static void archiveLogThenDelete(TestLogData logs, ITestDevice device, String remotePath,
38             String localName) throws DeviceNotAvailableException {
39         File logFile = device.pullFile(remotePath);
40         if (logFile != null) {
41             logs.addTestLog(localName, LogDataType.TEXT, new FileInputStreamSource(logFile));
42             // Delete to avoid confusing logs from a previous run, just in case.
43             device.deleteFile(remotePath);
44         }
45     }
46 }
47