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                 } else if (".pb".equals(ext)) {
46                     type = LogDataType.PB;
47                 } else if (".mp4".equals(ext)) {
48                     type = LogDataType.MP4;
49                 }
50                 testLog(metricFile.getName(), type, source);
51             }
52         }
53     }
54 
55     @Override
processMetricDirectory( String key, File metricDirectory, DeviceMetricData runData)56     public final void processMetricDirectory(
57             String key, File metricDirectory, DeviceMetricData runData) {
58         for (File f : metricDirectory.listFiles()) {
59             if (f.isDirectory()) {
60                 processMetricDirectory(key, f, runData);
61             } else {
62                 processMetricFile(key, f, runData);
63             }
64         }
65     }
66 
67     /**
68      * Possible processing of a pulled file to extract some metrics.
69      *
70      * @param key Key of the file pulled
71      * @param metricFile The {@link File} that was pulled.
72      * @param runData The metric storage were to put extracted metrics.
73      */
postProcessMetricFile(String key, File metricFile, DeviceMetricData runData)74     protected void postProcessMetricFile(String key, File metricFile, DeviceMetricData runData) {}
75 }
76