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