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