142365ec6Sopenharmony_ci/*
242365ec6Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
342365ec6Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
442365ec6Sopenharmony_ci * you may not use this file except in compliance with the License.
542365ec6Sopenharmony_ci * You may obtain a copy of the License at
642365ec6Sopenharmony_ci *
742365ec6Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
842365ec6Sopenharmony_ci *
942365ec6Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1042365ec6Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1142365ec6Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1242365ec6Sopenharmony_ci * See the License for the specific language governing permissions and
1342365ec6Sopenharmony_ci * limitations under the License.
1442365ec6Sopenharmony_ci */
1542365ec6Sopenharmony_ci
1642365ec6Sopenharmony_ciimport HiLog from '@ohos.hilog';
1742365ec6Sopenharmony_ciimport hiTraceMeter from '@ohos.hiTraceMeter';
1842365ec6Sopenharmony_ci
1942365ec6Sopenharmony_ciconst DOMAIN = 0x0200;
2042365ec6Sopenharmony_ciconst TAG: string = 'CameraApp';
2142365ec6Sopenharmony_ci
2242365ec6Sopenharmony_ciconst RECORD_TRACE = true;
2342365ec6Sopenharmony_ciconst TRACE_BASE_INDEX = 10000;
2442365ec6Sopenharmony_ci
2542365ec6Sopenharmony_ciconst LEVEL_DEBUG = 0;
2642365ec6Sopenharmony_ciconst LEVEL_LOG = 1;
2742365ec6Sopenharmony_ciconst LEVEL_INFO = 2;
2842365ec6Sopenharmony_ciconst LEVEL_WARN = 3;
2942365ec6Sopenharmony_ciconst LEVEL_ERROR = 4;
3042365ec6Sopenharmony_ciconst LOG_LEVEL = LEVEL_DEBUG;
3142365ec6Sopenharmony_ci
3242365ec6Sopenharmony_ciconst TRACE_LOG_BEGIN: string = ' begin ';
3342365ec6Sopenharmony_ciconst TRACE_LOG_END: string = ' end ';
3442365ec6Sopenharmony_ci
3542365ec6Sopenharmony_ciexport class Log {
3642365ec6Sopenharmony_ci  public static readonly STREAM_DISTRIBUTION: string = 'streamDistribution';
3742365ec6Sopenharmony_ci  public static readonly OPEN_CAMERA: string = 'openCamera';
3842365ec6Sopenharmony_ci  public static readonly STOP_RECORDING: string = 'stopRecording';
3942365ec6Sopenharmony_ci  public static readonly UPDATE_PHOTO_THUMBNAIL: string = 'updatePhotoThumbnail';
4042365ec6Sopenharmony_ci  public static readonly TAKE_PICTURE: string = 'takePicture';
4142365ec6Sopenharmony_ci  public static readonly UPDATE_VIDEO_THUMBNAIL: string = 'updateVideoThumbnail';
4242365ec6Sopenharmony_ci  public static readonly APPLICATION_WHOLE_LIFE: string = 'applicationWholeLife';
4342365ec6Sopenharmony_ci  public static readonly ABILITY_VISIBLE_LIFE: string = 'abilityVisibleLife';
4442365ec6Sopenharmony_ci  public static readonly ABILITY_FOREGROUND_LIFE: string = 'abilityForegroundLife';
4542365ec6Sopenharmony_ci  public static readonly ABILITY_WHOLE_LIFE: string = 'abilityWholeLife';
4642365ec6Sopenharmony_ci  public static readonly X_COMPONENT_LIFE: string = 'XComponentLife';
4742365ec6Sopenharmony_ci
4842365ec6Sopenharmony_ci  public static debug(message: string): void {
4942365ec6Sopenharmony_ci    if (LOG_LEVEL <= LEVEL_DEBUG) {
5042365ec6Sopenharmony_ci      HiLog.debug(DOMAIN, TAG, message);
5142365ec6Sopenharmony_ci    }
5242365ec6Sopenharmony_ci  }
5342365ec6Sopenharmony_ci
5442365ec6Sopenharmony_ci  public static log(message: string): void {
5542365ec6Sopenharmony_ci    if (LOG_LEVEL <= LEVEL_LOG) {
5642365ec6Sopenharmony_ci      HiLog.info(DOMAIN, TAG, message);
5742365ec6Sopenharmony_ci    }
5842365ec6Sopenharmony_ci  }
5942365ec6Sopenharmony_ci
6042365ec6Sopenharmony_ci  public static info(message: string): void {
6142365ec6Sopenharmony_ci    if (LOG_LEVEL <= LEVEL_INFO) {
6242365ec6Sopenharmony_ci      HiLog.info(DOMAIN, TAG, message);
6342365ec6Sopenharmony_ci    }
6442365ec6Sopenharmony_ci  }
6542365ec6Sopenharmony_ci
6642365ec6Sopenharmony_ci  public static warn(message: string): void {
6742365ec6Sopenharmony_ci    if (LOG_LEVEL <= LEVEL_WARN) {
6842365ec6Sopenharmony_ci      HiLog.warn(DOMAIN, TAG, message);
6942365ec6Sopenharmony_ci    }
7042365ec6Sopenharmony_ci  }
7142365ec6Sopenharmony_ci
7242365ec6Sopenharmony_ci  public static error(message: string): void {
7342365ec6Sopenharmony_ci    if (LOG_LEVEL <= LEVEL_ERROR) {
7442365ec6Sopenharmony_ci      HiLog.error(DOMAIN, TAG, message);
7542365ec6Sopenharmony_ci    }
7642365ec6Sopenharmony_ci  }
7742365ec6Sopenharmony_ci
7842365ec6Sopenharmony_ci  static start(methodName: string): void {
7942365ec6Sopenharmony_ci    this.info(methodName + TRACE_LOG_BEGIN);
8042365ec6Sopenharmony_ci    if (!RECORD_TRACE) {
8142365ec6Sopenharmony_ci      return;
8242365ec6Sopenharmony_ci    }
8342365ec6Sopenharmony_ci    if (typeof globalThis.taskIdMap === 'undefined' || typeof globalThis.traceIndex === 'undefined') {
8442365ec6Sopenharmony_ci      this.init();
8542365ec6Sopenharmony_ci    }
8642365ec6Sopenharmony_ci    let taskId = globalThis.taskIdMap.get(methodName);
8742365ec6Sopenharmony_ci    if (taskId == undefined) {
8842365ec6Sopenharmony_ci      taskId = globalThis.traceIndex;
8942365ec6Sopenharmony_ci      globalThis.traceIndex++;
9042365ec6Sopenharmony_ci      globalThis.taskIdMap.set(methodName, taskId);
9142365ec6Sopenharmony_ci    }
9242365ec6Sopenharmony_ci    hiTraceMeter.startTrace(methodName, taskId);
9342365ec6Sopenharmony_ci  }
9442365ec6Sopenharmony_ci
9542365ec6Sopenharmony_ci  private static init(): void {
9642365ec6Sopenharmony_ci    globalThis.taskIdMap = new Map<string, number>();
9742365ec6Sopenharmony_ci    globalThis.traceIndex = TRACE_BASE_INDEX;
9842365ec6Sopenharmony_ci  }
9942365ec6Sopenharmony_ci
10042365ec6Sopenharmony_ci  static end(methodName: string): void {
10142365ec6Sopenharmony_ci    this.info(methodName + TRACE_LOG_END);
10242365ec6Sopenharmony_ci    if (!RECORD_TRACE) {
10342365ec6Sopenharmony_ci      return;
10442365ec6Sopenharmony_ci    }
10542365ec6Sopenharmony_ci    if (typeof globalThis.taskIdMap === 'undefined') {
10642365ec6Sopenharmony_ci      this.init();
10742365ec6Sopenharmony_ci    }
10842365ec6Sopenharmony_ci    const taskId = globalThis.taskIdMap.get(methodName);
10942365ec6Sopenharmony_ci    if (taskId == undefined) {
11042365ec6Sopenharmony_ci      Log.error(`fail to end trace name ${methodName}`);
11142365ec6Sopenharmony_ci      return;
11242365ec6Sopenharmony_ci    }
11342365ec6Sopenharmony_ci    hiTraceMeter.finishTrace(methodName, taskId);
11442365ec6Sopenharmony_ci  }
11542365ec6Sopenharmony_ci}