• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.result;
17 
18 import com.android.tradefed.invoker.IInvocationContext;
19 import com.android.tradefed.log.LogUtil.CLog;
20 import com.android.tradefed.metrics.proto.MetricMeasurement.Metric;
21 import com.android.tradefed.result.skipped.SkipReason;
22 import com.android.tradefed.testtype.suite.ModuleDefinition;
23 import com.android.tradefed.util.FileUtil;
24 
25 import java.io.File;
26 import java.io.IOException;
27 import java.util.HashMap;
28 
29 /** Listener that logs all the events it receives into a file */
30 public class EventsLoggerListener implements ILogSaverListener {
31 
32     private File mLog;
33     private TestStatus mTestCaseStatus = null;
34 
EventsLoggerListener(String name)35     public EventsLoggerListener(String name) {
36         try {
37             mLog = FileUtil.createTempFile(name, ".txt");
38         } catch (IOException e) {
39             CLog.e(e);
40             mLog = null;
41         }
42     }
43 
getLoggedEvents()44     public File getLoggedEvents() {
45         return mLog;
46     }
47 
48     @Override
invocationStarted(IInvocationContext context)49     public void invocationStarted(IInvocationContext context) {
50         writeToFile("[invocation started]\n");
51     }
52 
53     @Override
invocationFailed(FailureDescription failure)54     public void invocationFailed(FailureDescription failure) {
55         writeToFile(
56                 String.format(
57                         "[invocation failed: %s|%s|%s]\n",
58                         failure.getFailureStatus(), failure.getErrorIdentifier(), failure));
59     }
60 
61     @Override
invocationSkipped(SkipReason reason)62     public void invocationSkipped(SkipReason reason) {
63         writeToFile(String.format("[invocation skipped: %s]\n", reason));
64     }
65 
66     @Override
invocationEnded(long elapsedTime)67     public void invocationEnded(long elapsedTime) {
68         writeToFile("[invocation ended]\n");
69     }
70 
71     @Override
testModuleStarted(IInvocationContext moduleContext)72     public void testModuleStarted(IInvocationContext moduleContext) {
73         writeToFile(
74                 String.format(
75                         "  [module %s started]\n",
76                         moduleContext
77                                 .getAttributes()
78                                 .getUniqueMap()
79                                 .get(ModuleDefinition.MODULE_ID)));
80     }
81 
82     @Override
testModuleEnded()83     public void testModuleEnded() {
84         writeToFile("  [module ended]\n");
85     }
86 
87     @Override
testRunStarted(String runName, int testCount)88     public void testRunStarted(String runName, int testCount) {
89         testRunStarted(runName, testCount, 0, System.currentTimeMillis());
90     }
91 
92     @Override
testRunStarted(String runName, int testCount, int attemptNumber)93     public void testRunStarted(String runName, int testCount, int attemptNumber) {
94         testRunStarted(runName, testCount, attemptNumber, System.currentTimeMillis());
95     }
96 
97     @Override
testRunStarted(String runName, int testCount, int attemptNumber, long startTime)98     public void testRunStarted(String runName, int testCount, int attemptNumber, long startTime) {
99         writeToFile(
100                 String.format(
101                         "    [run %s (testCount: %s,attempt: %s) started]\n",
102                         runName, testCount, attemptNumber));
103     }
104 
105     @Override
testRunFailed(FailureDescription failure)106     public void testRunFailed(FailureDescription failure) {
107         writeToFile(
108                 String.format(
109                         "        [run failed with %s|%s|%s]\n",
110                         failure.getErrorMessage(),
111                         failure.getFailureStatus(),
112                         failure.getErrorIdentifier()));
113     }
114 
115     @Override
testRunFailed(String errorMessage)116     public void testRunFailed(String errorMessage) {
117         writeToFile(String.format("        [run failed with %s]\n", errorMessage));
118     }
119 
120     @Override
testRunEnded(long elapsedTimeMillis, HashMap<String, Metric> runMetrics)121     public void testRunEnded(long elapsedTimeMillis, HashMap<String, Metric> runMetrics) {
122         writeToFile("    [run ended]\n");
123     }
124 
125     @Override
testStarted(TestDescription test, long startTime)126     public void testStarted(TestDescription test, long startTime) {
127         mTestCaseStatus = TestStatus.PASSED;
128     }
129 
130     @Override
testIgnored(TestDescription test)131     public void testIgnored(TestDescription test) {
132         mTestCaseStatus = TestStatus.IGNORED;
133     }
134 
135     @Override
testFailed(TestDescription test, FailureDescription failure)136     public void testFailed(TestDescription test, FailureDescription failure) {
137         mTestCaseStatus = TestStatus.FAILURE;
138     }
139 
140     @Override
testFailed(TestDescription test, String trace)141     public void testFailed(TestDescription test, String trace) {
142         mTestCaseStatus = TestStatus.FAILURE;
143     }
144 
145     @Override
testAssumptionFailure(TestDescription test, FailureDescription failure)146     public void testAssumptionFailure(TestDescription test, FailureDescription failure) {
147         mTestCaseStatus = TestStatus.ASSUMPTION_FAILURE;
148     }
149 
150     @Override
testAssumptionFailure(TestDescription test, String trace)151     public void testAssumptionFailure(TestDescription test, String trace) {
152         mTestCaseStatus = TestStatus.ASSUMPTION_FAILURE;
153     }
154 
155     @Override
testSkipped(TestDescription test, SkipReason reason)156     public void testSkipped(TestDescription test, SkipReason reason) {
157         mTestCaseStatus = TestStatus.SKIPPED;
158     }
159 
160     @Override
testEnded(TestDescription test, long endTime, HashMap<String, Metric> testMetrics)161     public void testEnded(TestDescription test, long endTime, HashMap<String, Metric> testMetrics) {
162         writeToFile(String.format("      - test: %s (status=%s)\n", test, mTestCaseStatus));
163         mTestCaseStatus = null;
164     }
165 
166     @Override
logAssociation(String dataName, LogFile logFile)167     public void logAssociation(String dataName, LogFile logFile) {
168         String extra = "";
169         if (mTestCaseStatus != null) {
170             extra = "test";
171         }
172         writeToFile(
173                 String.format("[   %s log: %s | path: %s]\n", extra, dataName, logFile.getPath()));
174     }
175 
writeToFile(String text)176     private void writeToFile(String text) {
177         if (mLog == null || !mLog.exists()) {
178             return;
179         }
180         try {
181             FileUtil.writeToFile(text, mLog, true);
182         } catch (IOException e) {
183             CLog.e(e);
184         }
185     }
186 }
187