1 /* 2 * Copyright (C) 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.car.util; 18 19 /** 20 * An utility class to dump transition events across different car service components. 21 * The output will be of the form 22 * <p> 23 * "Time <svc name>: [optional context information] changed from <from state> to <to state>" 24 * This can be used in conjunction with the dump() method to dump this information through 25 * adb shell dumpsys activity service com.android.car 26 * <p> 27 * A specific service in CarService can choose to use a circular buffer of N records to keep 28 * track of the last N transitions. 29 */ 30 public final class TransitionLog { 31 private String mServiceName; // name of the service or tag 32 private Object mFromState; // old state 33 private Object mToState; // new state 34 private long mTimestampMs; // System.currentTimeMillis() 35 private String mExtra; // Additional information as a String 36 TransitionLog(String name, Object fromState, Object toState, long timestamp, String extra)37 public TransitionLog(String name, Object fromState, Object toState, long timestamp, 38 String extra) { 39 this(name, fromState, toState, timestamp); 40 mExtra = extra; 41 } 42 TransitionLog(String name, Object fromState, Object toState, long timeStamp)43 public TransitionLog(String name, Object fromState, Object toState, long timeStamp) { 44 mServiceName = name; 45 mFromState = fromState; 46 mToState = toState; 47 mTimestampMs = timeStamp; 48 } 49 timeToLog(long timestamp)50 private CharSequence timeToLog(long timestamp) { 51 return android.text.format.DateFormat.format("MM-dd HH:mm:ss", timestamp); 52 } 53 54 @Override toString()55 public String toString() { 56 return timeToLog(mTimestampMs) + " " + mServiceName + ": " 57 + (mExtra != null ? mExtra + " " : "") 58 + "changed from " + mFromState + " to " + mToState; 59 } 60 } 61