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.OptionClass;
19 import com.android.tradefed.result.FileInputStreamSource;
20 import com.android.tradefed.result.InputStreamSource;
21 import com.android.tradefed.result.LogDataType;
22 import com.android.tradefed.util.FileUtil;
23 
24 import java.io.File;
25 
26 /**
27  * Logger of the file reported by the device-side. This logger is allowed to live inside a module
28  * (AndroidTest.xml). TODO: When device-side reporting gets better, fix the LogDataType to be more
29  * accurate.
30  */
31 @OptionClass(alias = "file-puller-log-collector")
32 public class FilePullerLogCollector extends FilePullerDeviceMetricCollector {
33 
34     @Override
processMetricFile(String key, File metricFile, DeviceMetricData runData)35     public final void processMetricFile(String key, File metricFile, DeviceMetricData runData) {
36         try {
37             postProcessMetricFile(key, metricFile, runData);
38         } finally {
39             try (InputStreamSource source = new FileInputStreamSource(metricFile, true)) {
40                 // Try to infer the type. This will be improved eventually, see todo on the class.
41                 LogDataType type = LogDataType.TEXT;
42                 String ext = FileUtil.getExtension(metricFile.getName()).toLowerCase();
43                 if (".png".equals(ext)) {
44                     type = LogDataType.PNG;
45                 }
46                 if (".pb".equals(ext)) {
47                     type = LogDataType.PB;
48                 }
49                 testLog(metricFile.getName(), type, source);
50             }
51         }
52     }
53 
54     @Override
processMetricDirectory( String key, File metricDirectory, DeviceMetricData runData)55     public final void processMetricDirectory(
56             String key, File metricDirectory, DeviceMetricData runData) {
57         for (File f : metricDirectory.listFiles()) {
58             if (f.isDirectory()) {
59                 processMetricDirectory(key, f, runData);
60             } else {
61                 processMetricFile(key, f, runData);
62             }
63         }
64     }
65 
66     /**
67      * Possible processing of a pulled file to extract some metrics.
68      *
69      * @param key Key of the file pulled
70      * @param metricFile The {@link File} that was pulled.
71      * @param runData The metric storage were to put extracted metrics.
72      */
postProcessMetricFile(String key, File metricFile, DeviceMetricData runData)73     protected void postProcessMetricFile(String key, File metricFile, DeviceMetricData runData) {}
74 }
75