1 /* 2 * Copyright (C) 2018 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.tradefed.config.Option; 19 import com.android.tradefed.metrics.proto.MetricMeasurement.Metric; 20 import com.android.tradefed.result.FileInputStreamSource; 21 import com.android.tradefed.result.InputStreamSource; 22 import com.android.tradefed.result.LogDataType; 23 import com.android.tradefed.util.FileUtil; 24 25 import java.io.File; 26 import java.util.Map; 27 28 /** 29 * Logger of the file reported by the device-side. This logger is allowed to live inside a module 30 * (AndroidTest.xml). TODO: When device-side reporting gets better, fix the LogDataType to be more 31 * accurate. 32 */ 33 public final class FilePullerLogCollector extends FilePullerDeviceMetricCollector { 34 35 @Option( 36 name = "collect-on-run-ended-only", 37 description = 38 "Attempt to collect the files on test run end only instead of on both test cases " 39 + "and test run ended." 40 ) 41 private boolean mCollectOnRunEndedOnly = false; 42 43 @Override onTestEnd(DeviceMetricData testData, Map<String, Metric> currentTestCaseMetrics)44 public void onTestEnd(DeviceMetricData testData, Map<String, Metric> currentTestCaseMetrics) { 45 if (mCollectOnRunEndedOnly) { 46 return; 47 } 48 super.onTestEnd(testData, currentTestCaseMetrics); 49 } 50 51 @Override processMetricFile(String key, File metricFile, DeviceMetricData runData)52 public void processMetricFile(String key, File metricFile, DeviceMetricData runData) { 53 try (InputStreamSource source = new FileInputStreamSource(metricFile, true)) { 54 // Try to infer the type. This will be improved eventually, see todo on the class. 55 LogDataType type = LogDataType.TEXT; 56 String ext = FileUtil.getExtension(metricFile.getName()).toLowerCase(); 57 if (".png".equals(ext)) { 58 type = LogDataType.PNG; 59 } 60 testLog(metricFile.getName(), type, source); 61 } 62 } 63 64 @Override processMetricDirectory(String key, File metricDirectory, DeviceMetricData runData)65 public void processMetricDirectory(String key, File metricDirectory, DeviceMetricData runData) { 66 for (File f : metricDirectory.listFiles()) { 67 if (f.isDirectory()) { 68 processMetricDirectory(key, f, runData); 69 } else { 70 processMetricFile(key, f, runData); 71 } 72 } 73 } 74 } 75