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.config.OptionClass; 20 import com.android.tradefed.log.LogUtil.CLog; 21 import com.android.tradefed.result.FileInputStreamSource; 22 import com.android.tradefed.result.InputStreamSource; 23 import com.android.tradefed.result.LogDataType; 24 import com.android.tradefed.util.FileUtil; 25 26 import java.io.File; 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 @OptionClass(alias = "file-puller-log-collector") 34 public class FilePullerLogCollector extends FilePullerDeviceMetricCollector { 35 @Option( 36 name = "log-data-type", 37 description = "Type to assign to pulled logs (default: autodetect from extension)") 38 private String mLogDataType; 39 40 @Override processMetricFile(String key, File metricFile, DeviceMetricData runData)41 public final void processMetricFile(String key, File metricFile, DeviceMetricData runData) { 42 try { 43 postProcessMetricFile(key, metricFile, runData); 44 } finally { 45 try (InputStreamSource source = new FileInputStreamSource(metricFile, true)) { 46 LogDataType type = guessLogDataType(metricFile); 47 48 testLog(FileUtil.getBaseName(metricFile.getName()), type, source); 49 } 50 } 51 } 52 guessLogDataType(File metricFile)53 private LogDataType guessLogDataType(File metricFile) { 54 if (mLogDataType != null && mLogDataType.length() > 0) { 55 try { 56 return LogDataType.valueOf(mLogDataType); 57 } catch (IllegalArgumentException e) { 58 CLog.e("Invalid log-data-type option: " + mLogDataType); 59 } 60 } 61 62 // Try to infer the type. This will be improved eventually, see todo on the class. 63 String ext = FileUtil.getExtension(metricFile.getName()).toLowerCase(); 64 if (".hprof".equals(ext)) { 65 return LogDataType.HPROF; 66 } else if (".mp4".equals(ext)) { 67 return LogDataType.MP4; 68 } else if (".pb".equals(ext)) { 69 return LogDataType.PB; 70 } else if (".png".equals(ext)) { 71 return LogDataType.PNG; 72 } else if (".perfetto-trace".equals(ext)) { 73 return LogDataType.PERFETTO; 74 } else if (".zip".equals(ext)) { 75 return LogDataType.ZIP; 76 } else if (".uix".equals(ext)) { 77 return LogDataType.UIX; 78 } else if (".textproto".equals(ext) 79 && FileUtil.getBaseName(metricFile.getName()).contains("_goldResult")) { 80 return LogDataType.GOLDEN_RESULT_PROTO; 81 } else if (".trace".equals(ext)) { 82 return LogDataType.TRACE; 83 } else if (".log".equals(ext)) { 84 return LogDataType.BT_SNOOP_LOG; 85 } else if (".json".equals(ext)) { 86 return LogDataType.JSON; 87 } else if (FileUtil.getBaseName(metricFile.getName()).contains("winscope") 88 || FileUtil.getBaseName(metricFile.getName()).contains("uiTrace_")) { 89 return LogDataType.WINSCOPE; 90 } 91 return LogDataType.TEXT; 92 } 93 94 @Override processMetricDirectory( String key, File metricDirectory, DeviceMetricData runData)95 public void processMetricDirectory( 96 String key, File metricDirectory, DeviceMetricData runData) { 97 for (File file : metricDirectory.listFiles()) { 98 if (file.isDirectory()) { 99 processMetricDirectory(key, file, runData); 100 } else { 101 processMetricFile(key, file, runData); 102 } 103 } 104 FileUtil.recursiveDelete(metricDirectory); 105 } 106 107 /** 108 * Possible processing of a pulled file to extract some metrics. 109 * 110 * @param key Key of the file pulled 111 * @param metricFile The {@link File} that was pulled. 112 * @param runData The metric storage were to put extracted metrics. 113 */ postProcessMetricFile(String key, File metricFile, DeviceMetricData runData)114 protected void postProcessMetricFile(String key, File metricFile, DeviceMetricData runData) {} 115 } 116