1 /* 2 * Copyright (C) 2014 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 17 package com.android.compatibility.common.util; 18 19 import android.app.Instrumentation; 20 import android.os.Bundle; 21 import android.os.Environment; 22 import android.util.Log; 23 24 import com.android.compatibility.common.util.ReportLog; 25 26 import org.xmlpull.v1.XmlPullParserException; 27 28 import java.io.File; 29 import java.io.IOException; 30 import java.util.List; 31 32 /** 33 * Handles adding results to the report for device side tests. 34 * 35 * NOTE: tests MUST call {@link #submit(Instrumentation)} if and only if the test passes in order to 36 * send the results to the runner. 37 */ 38 public class DeviceReportLog extends ReportLog { 39 private static final String TAG = DeviceReportLog.class.getSimpleName(); 40 private static final String RESULT = "COMPATIBILITY_TEST_RESULT"; 41 private static final int INST_STATUS_ERROR = -1; 42 private static final int INST_STATUS_IN_PROGRESS = 2; 43 44 private ReportLogDeviceInfoStore store; 45 46 public DeviceReportLog(String reportLogName, String streamName) { 47 super(reportLogName, streamName); 48 try { 49 // dir value must match the src-dir value configured in ReportLogCollector target 50 // preparer in cts/tools/cts-tradefed/res/config/cts-preconditions.xml 51 final File dir = new File(Environment.getExternalStorageDirectory(), "report-log-files"); 52 if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { 53 throw new IOException("External storage is not mounted"); 54 } else if (!dir.mkdirs() && !dir.isDirectory()) { 55 throw new IOException("Cannot create directory for device info files"); 56 } else { 57 File jsonFile = new File(dir, mReportLogName + ".reportlog.json"); 58 store = new ReportLogDeviceInfoStore(jsonFile, mStreamName); 59 store.open(); 60 } 61 } catch (Exception e) { 62 Log.e(TAG, "Could not create report log file.", e); 63 } 64 } 65 66 /** 67 * Adds a double metric to the report. 68 */ 69 @Override 70 public void addValue(String source, String message, double value, ResultType type, 71 ResultUnit unit) { 72 super.addValue(source, message, value, type, unit); 73 try { 74 store.addResult(message, value); 75 } catch (IOException e) { 76 Log.e(TAG, "Could not log metric.", e); 77 } 78 } 79 80 /** 81 * Adds a double metric to the report. 82 */ 83 @Override 84 public void addValue(String message, double value, ResultType type, ResultUnit unit) { 85 super.addValue(message, value, type, unit); 86 try { 87 store.addResult(message, value); 88 } catch (IOException e) { 89 Log.e(TAG, "Could not log metric.", e); 90 } 91 } 92 93 /** 94 * Adds a double array of metrics to the report. 95 */ 96 @Override 97 public void addValues(String source, String message, double[] values, ResultType type, 98 ResultUnit unit) { 99 super.addValues(source, message, values, type, unit); 100 try { 101 store.addArrayResult(message, values); 102 } catch (IOException e) { 103 Log.e(TAG, "Could not log metric.", e); 104 } 105 } 106 107 /** 108 * Adds a double array of metrics to the report. 109 */ 110 @Override 111 public void addValues(String message, double[] values, ResultType type, ResultUnit unit) { 112 super.addValues(message, values, type, unit); 113 try { 114 store.addArrayResult(message, values); 115 } catch (IOException e) { 116 Log.e(TAG, "Could not log metric.", e); 117 } 118 } 119 120 /** 121 * Adds an int metric to the report. 122 */ 123 @Override 124 public void addValue(String message, int value, ResultType type, ResultUnit unit) { 125 try { 126 store.addResult(message, value); 127 } catch (IOException e) { 128 Log.e(TAG, "Could not log metric.", e); 129 } 130 } 131 132 /** 133 * Adds a long metric to the report. 134 */ 135 @Override 136 public void addValue(String message, long value, ResultType type, ResultUnit unit) { 137 try { 138 store.addResult(message, value); 139 } catch (IOException e) { 140 Log.e(TAG, "Could not log metric.", e); 141 } 142 } 143 144 /** 145 * Adds a float metric to the report. 146 */ 147 @Override 148 public void addValue(String message, float value, ResultType type, ResultUnit unit) { 149 try { 150 store.addResult(message, value); 151 } catch (IOException e) { 152 Log.e(TAG, "Could not log metric.", e); 153 } 154 } 155 156 /** 157 * Adds a boolean metric to the report. 158 */ 159 @Override 160 public void addValue(String message, boolean value, ResultType type, ResultUnit unit) { 161 try { 162 store.addResult(message, value); 163 } catch (IOException e) { 164 Log.e(TAG, "Could not log metric.", e); 165 } 166 } 167 168 /** 169 * Adds a String metric to the report. 170 */ 171 @Override 172 public void addValue(String message, String value, ResultType type, ResultUnit unit) { 173 try { 174 store.addResult(message, value); 175 } catch (IOException e) { 176 Log.e(TAG, "Could not log metric.", e); 177 } 178 } 179 180 /** 181 * Adds an int array of metrics to the report. 182 */ 183 @Override 184 public void addValues(String message, int[] values, ResultType type, ResultUnit unit) { 185 try { 186 store.addArrayResult(message, values); 187 } catch (IOException e) { 188 Log.e(TAG, "Could not log metric.", e); 189 } 190 } 191 192 /** 193 * Adds a long array of metrics to the report. 194 */ 195 @Override 196 public void addValues(String message, long[] values, ResultType type, ResultUnit unit) { 197 try { 198 store.addArrayResult(message, values); 199 } catch (IOException e) { 200 Log.e(TAG, "Could not log metric.", e); 201 } 202 } 203 204 /** 205 * Adds a float array of metrics to the report. 206 */ 207 @Override 208 public void addValues(String message, float[] values, ResultType type, ResultUnit unit) { 209 try { 210 store.addArrayResult(message, values); 211 } catch (IOException e) { 212 Log.e(TAG, "Could not log metric.", e); 213 } 214 } 215 216 /** 217 * Adds a boolean array of metrics to the report. 218 */ 219 @Override 220 public void addValues(String message, boolean[] values, ResultType type, ResultUnit unit) { 221 try { 222 store.addArrayResult(message, values); 223 } catch (IOException e) { 224 Log.e(TAG, "Could not log metric.", e); 225 } 226 } 227 228 /** 229 * Adds a String List of metrics to the report. 230 */ 231 @Override 232 public void addValues(String message, List<String> values, ResultType type, ResultUnit unit) { 233 try { 234 store.addListResult(message, values); 235 } catch (IOException e) { 236 Log.e(TAG, "Could not log metric.", e); 237 } 238 } 239 240 /** 241 * Sets the summary double metric of the report. 242 * 243 * NOTE: messages over {@value Metric#MAX_MESSAGE_LENGTH} chars will be trimmed. 244 */ 245 @Override 246 public void setSummary(String message, double value, ResultType type, ResultUnit unit) { 247 super.setSummary(message, value, type, unit); 248 try { 249 store.addResult(message, value); 250 } catch (IOException e) { 251 Log.e(TAG, "Could not log metric.", e); 252 } 253 } 254 255 /** 256 * Closes report file and submits report to instrumentation. 257 */ 258 public void submit(Instrumentation instrumentation) { 259 Log.i(TAG, "Submit"); 260 try { 261 store.close(); 262 Bundle output = new Bundle(); 263 output.putString(RESULT, serialize(this)); 264 instrumentation.sendStatus(INST_STATUS_IN_PROGRESS, output); 265 } catch (IllegalArgumentException | IllegalStateException | XmlPullParserException 266 | IOException e) { 267 Log.e(TAG, "Submit Failed", e); 268 instrumentation.sendStatus(INST_STATUS_ERROR, null); 269 } 270 } 271 272 /** 273 * Closes report file. Static functions that do not have access to instrumentation can 274 * use this to close report logs. Summary, if present, is not reported to instrumentation, hence 275 * does not appear in the result XML. 276 */ 277 public void submit() { 278 Log.i(TAG, "Submit"); 279 try { 280 store.close(); 281 } catch (IOException e) { 282 Log.e(TAG, "Submit Failed", e); 283 } 284 } 285 } 286