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
19/**
20 * Add method trace. Modify RECORD_TRACE before using.
21 */
22export class Trace {
23  static readonly CORE_METHOD_START_APP_CENTER = 'startAppCenter';
24  static readonly CORE_METHOD_START_RECENTS = 'startRecents';
25  static readonly CORE_METHOD_START_SETTINGS = 'startSettings';
26  static readonly CORE_METHOD_OPEN_FOLDER = 'openFolder';
27  static readonly CORE_METHOD_OPEN_FOLDER_DIALOG = 'openFolderDialog';
28  static readonly CORE_METHOD_CLEAR_ALL_MISSIONS = 'clearAllMissions';
29  static readonly CORE_METHOD_START_APP_ANIMATION = 'startAppAnimation';
30  static readonly CORE_METHOD_CLOSE_APP_ANIMATION = 'closeAppAnimation';
31
32  private static readonly TRACE_TAG = 'L:Trace';
33  private static readonly RECORD_TRACE = true;
34  private static readonly TRACE_LIMIT = 2000;
35
36  private static readonly TRACE_BASE_INDEX = 10000;
37
38  /**
39    * start trace method
40    *
41    * @param {string} methodName - methodName for tracing
42    */
43  static start(methodName: string) {
44    if (!Trace.RECORD_TRACE) return;
45    if (typeof globalThis.taskIdMap === 'undefined' || typeof globalThis.traceIndex === 'undefined') {
46      Trace.init();
47    }
48    let taskId = globalThis.taskIdMap.get(methodName);
49    if (taskId == undefined) {
50      taskId = globalThis.traceIndex;
51      globalThis.traceIndex++;
52      globalThis.taskIdMap.set(methodName, taskId);
53    }
54    Log.showDebug(this.TRACE_TAG, `start trace taskId: ${taskId}`);
55    byTrace.startTrace(this.TRACE_TAG + methodName, taskId, Trace.TRACE_LIMIT);
56  }
57
58  private static init() {
59    Log.showDebug(this.TRACE_TAG, 'init trace parameters');
60    globalThis.taskIdMap = new Map<string, number>();
61    globalThis.traceIndex = Trace.TRACE_BASE_INDEX;
62  }
63
64  /**
65    * stop trace method
66    *
67    * @param {string} methodName - methodName for tracing
68    */
69  static end(methodName: string) {
70    if (!Trace.RECORD_TRACE) return;
71    if (typeof globalThis.taskIdMap === 'undefined') {
72      return;
73    }
74    const taskId = globalThis.taskIdMap.get(methodName);
75    if (taskId == undefined) {
76      Log.showDebug(this.TRACE_TAG, `fail to end trace name:${methodName}`);
77      return;
78    }
79    Log.showDebug(this.TRACE_TAG, `end trace taskId: ${taskId}`);
80    byTrace.finishTrace(this.TRACE_TAG + methodName, taskId);
81  }
82}