1/*
2 * Copyright (c) 2023 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 HiLog from '@ohos.hilog';
17import hiTraceMeter from '@ohos.hiTraceMeter';
18
19const DOMAIN = 0x0200;
20const TAG: string = 'CameraApp';
21
22const RECORD_TRACE = true;
23const TRACE_BASE_INDEX = 10000;
24
25const LEVEL_DEBUG = 0;
26const LEVEL_LOG = 1;
27const LEVEL_INFO = 2;
28const LEVEL_WARN = 3;
29const LEVEL_ERROR = 4;
30const LOG_LEVEL = LEVEL_DEBUG;
31
32const TRACE_LOG_BEGIN: string = ' begin ';
33const TRACE_LOG_END: string = ' end ';
34
35export class Log {
36  public static readonly STREAM_DISTRIBUTION: string = 'streamDistribution';
37  public static readonly OPEN_CAMERA: string = 'openCamera';
38  public static readonly STOP_RECORDING: string = 'stopRecording';
39  public static readonly UPDATE_PHOTO_THUMBNAIL: string = 'updatePhotoThumbnail';
40  public static readonly TAKE_PICTURE: string = 'takePicture';
41  public static readonly UPDATE_VIDEO_THUMBNAIL: string = 'updateVideoThumbnail';
42  public static readonly APPLICATION_WHOLE_LIFE: string = 'applicationWholeLife';
43  public static readonly ABILITY_VISIBLE_LIFE: string = 'abilityVisibleLife';
44  public static readonly ABILITY_FOREGROUND_LIFE: string = 'abilityForegroundLife';
45  public static readonly ABILITY_WHOLE_LIFE: string = 'abilityWholeLife';
46  public static readonly X_COMPONENT_LIFE: string = 'XComponentLife';
47
48  public static debug(message: string): void {
49    if (LOG_LEVEL <= LEVEL_DEBUG) {
50      HiLog.debug(DOMAIN, TAG, message);
51    }
52  }
53
54  public static log(message: string): void {
55    if (LOG_LEVEL <= LEVEL_LOG) {
56      HiLog.info(DOMAIN, TAG, message);
57    }
58  }
59
60  public static info(message: string): void {
61    if (LOG_LEVEL <= LEVEL_INFO) {
62      HiLog.info(DOMAIN, TAG, message);
63    }
64  }
65
66  public static warn(message: string): void {
67    if (LOG_LEVEL <= LEVEL_WARN) {
68      HiLog.warn(DOMAIN, TAG, message);
69    }
70  }
71
72  public static error(message: string): void {
73    if (LOG_LEVEL <= LEVEL_ERROR) {
74      HiLog.error(DOMAIN, TAG, message);
75    }
76  }
77
78  static start(methodName: string): void {
79    this.info(methodName + TRACE_LOG_BEGIN);
80    if (!RECORD_TRACE) {
81      return;
82    }
83    if (typeof globalThis.taskIdMap === 'undefined' || typeof globalThis.traceIndex === 'undefined') {
84      this.init();
85    }
86    let taskId = globalThis.taskIdMap.get(methodName);
87    if (taskId == undefined) {
88      taskId = globalThis.traceIndex;
89      globalThis.traceIndex++;
90      globalThis.taskIdMap.set(methodName, taskId);
91    }
92    hiTraceMeter.startTrace(methodName, taskId);
93  }
94
95  private static init(): void {
96    globalThis.taskIdMap = new Map<string, number>();
97    globalThis.traceIndex = TRACE_BASE_INDEX;
98  }
99
100  static end(methodName: string): void {
101    this.info(methodName + TRACE_LOG_END);
102    if (!RECORD_TRACE) {
103      return;
104    }
105    if (typeof globalThis.taskIdMap === 'undefined') {
106      this.init();
107    }
108    const taskId = globalThis.taskIdMap.get(methodName);
109    if (taskId == undefined) {
110      Log.error(`fail to end trace name ${methodName}`);
111      return;
112    }
113    hiTraceMeter.finishTrace(methodName, taskId);
114  }
115}