1 /* 2 * Copyright 2021 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.server.am; 18 19 import android.os.Build; 20 import android.os.Trace; 21 22 import java.util.UUID; 23 24 /** 25 * Adds a unique id to a trace. 26 * 27 * @hide 28 */ 29 public class TraceErrorLogger { 30 private static final String COUNTER_PREFIX = "ErrorId:"; 31 private static final int PLACEHOLDER_VALUE = 1; 32 isAddErrorIdEnabled()33 public boolean isAddErrorIdEnabled() { 34 return Build.IS_DEBUGGABLE; 35 } 36 37 /** 38 * Generates a unique id with which to tag a trace. 39 */ generateErrorId()40 public UUID generateErrorId() { 41 return UUID.randomUUID(); 42 } 43 44 /** 45 * Pushes a counter containing a unique id and a label {@link #COUNTER_PREFIX} so that traces 46 * can be uniquely identified. We also add the same id to the dropbox entry of the error, so 47 * that we can join the trace and the error server-side. 48 * 49 * @param processName The process name to include in the error id. 50 * @param errorId The unique id with which to tag the trace. 51 */ addErrorIdToTrace(String processName, UUID errorId)52 public void addErrorIdToTrace(String processName, UUID errorId) { 53 Trace.traceCounter(Trace.TRACE_TAG_ACTIVITY_MANAGER, 54 COUNTER_PREFIX + processName + "#" + errorId.toString(), 55 PLACEHOLDER_VALUE); 56 } 57 } 58