• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.annotations.VisibleForTesting;
19 import com.android.tradefed.log.ILeveledLogOutput;
20 import com.android.tradefed.log.LogRegistry;
21 import com.android.tradefed.log.LogUtil.CLog;
22 import com.android.tradefed.result.InputStreamSource;
23 import com.android.tradefed.result.LogDataType;
24 import com.android.tradefed.result.SnapshotInputStreamSource;
25 import com.android.tradefed.result.TestDescription;
26 
27 import java.io.IOException;
28 import java.io.InputStream;
29 
30 /** Collector that will gather and log the host-side logs when a test case failure occurs. */
31 public class DebugHostLogOnFailureCollector extends BaseDeviceMetricCollector {
32 
33     private static final String NAME_FORMAT = "%s-debug-hostlog-on-failure";
34     private static final int THROTTLE_LIMIT_PER_RUN = 10;
35 
36     private int mCurrentCount = 0;
37     private boolean mFirstThrottle = true;
38     private Long offset = null;
39 
40     @Override
onTestRunStart(DeviceMetricData runData)41     public void onTestRunStart(DeviceMetricData runData) {
42         offset = null;
43         mCurrentCount = 0;
44         mFirstThrottle = true;
45         // TODO: Improve the offset from the start of the method instead.
46         try (InputStreamSource source = getLogger().getLog()) {
47             if (source == null) {
48                 CLog.e(
49                         "Could not obtain the host logs for debugging. It won't be available "
50                                 + "in the event of test cases failures.");
51                 return;
52             }
53             offset = source.size();
54         }
55     }
56 
57     @Override
onTestFail(DeviceMetricData testData, TestDescription test)58     public void onTestFail(DeviceMetricData testData, TestDescription test) {
59         if (offset == null) {
60             return;
61         }
62         if (mCurrentCount > THROTTLE_LIMIT_PER_RUN) {
63             if (mFirstThrottle) {
64                 CLog.w("Throttle capture of host_log-on-failure due to too many failures.");
65                 mFirstThrottle = false;
66             }
67             return;
68         }
69         try (InputStreamSource source = getLogger().getLog()) {
70             if (source == null) {
71                 return;
72             }
73             try (InputStream stream = source.createInputStream()) {
74                 stream.skip(offset);
75                 try (InputStreamSource logSource =
76                         new SnapshotInputStreamSource("host-log-failure", stream)) {
77                     super.testLog(
78                             String.format(NAME_FORMAT, test.toString()),
79                             LogDataType.TEXT,
80                             logSource);
81                 }
82             }
83         } catch (IOException e) {
84             CLog.e(e);
85         }
86     }
87 
88     @VisibleForTesting
getLogger()89     ILeveledLogOutput getLogger() {
90         LogRegistry registry = (LogRegistry) LogRegistry.getLogRegistry();
91         return registry.getLogger();
92     }
93 }
94