1/* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import byTrace from "@ohos.bytrace"; 17import {Log} from "./Log"; 18 19export class Trace { 20 static readonly CORE_METHOD_UNLOCK_SCREEN = "unlockScreen" 21 static readonly CORE_METHOD_CALL_ACCOUNT_SYSTEM = "callAccountSubsystem"; 22 static readonly CORE_METHOD_PASS_ACCOUNT_SYSTEM_RESULT = "passingAccountSubsystemResult"; 23 static readonly CORE_METHOD_HIDE_PSD_PAGE = "hidePsdPage"; 24 static readonly CORE_METHOD_SHOW_LOCK_SCREEN = "showLockScreen"; 25 static readonly CORE_METHOD_SLEEP_TO_LOCK_SCREEN = "sleepToLockScreen" 26 27 private static readonly TRACE_TAG = 'ScreenLock:Trace'; 28 private static readonly RECORD_TRACE = true; 29 private static readonly TRACE_LIMIT = 2000; 30 31 private static readonly TRACE_BASE_INDEX = 10020; 32 33 private static init() { 34 Log.showInfo(this.TRACE_TAG, 'init trace parameters'); 35 globalThis.taskIdMap = new Map<string, number>(); 36 globalThis.traceIndex = Trace.TRACE_BASE_INDEX; 37 } 38 39 /** 40 * start trace method 41 * 42 * @param {string} methodName - methodName for tracing 43 */ 44 static start(methodName: string) { 45 if (!Trace.RECORD_TRACE) return; 46 if (typeof globalThis.taskIdMap === 'undefined' || typeof globalThis.traceIndex === 'undefined') { 47 Trace.init(); 48 } 49 let taskId = globalThis.taskIdMap.get(methodName); 50 if (taskId == undefined) { 51 taskId = globalThis.traceIndex; 52 globalThis.traceIndex++; 53 globalThis.taskIdMap.set(methodName, taskId); 54 } 55 Log.showInfo(this.TRACE_TAG, `start trace ${taskId} for ${methodName}`); 56 byTrace.startTrace(this.TRACE_TAG + methodName, taskId, Trace.TRACE_LIMIT); 57 } 58 59 /** 60 * stop trace method 61 * 62 * @param {string} methodName - methodName for tracing 63 */ 64 static end(methodName: string) { 65 if (!Trace.RECORD_TRACE) return; 66 if (typeof globalThis.taskIdMap === 'undefined') { 67 return; 68 } 69 const taskId = globalThis.taskIdMap.get(methodName); 70 if (taskId == undefined) { 71 Log.showError(this.TRACE_TAG, `fail to end trace name ${methodName}`); 72 return; 73 } 74 Log.showInfo(this.TRACE_TAG, `end trace ${taskId} for ${methodName}`); 75 byTrace.finishTrace(this.TRACE_TAG + methodName, taskId); 76 } 77} 78