• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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'
18import hiSysEvent from '@ohos.hiSysEvent'
19
20export default class Trace {
21    static readonly STREAM_DISTRIBUTION = 'streamDistribution';
22    static readonly OPEN_CAMERA = 'openCamera';
23    static readonly STOP_RECORDING = 'stopRecording';
24    static readonly UPDATE_PHOTO_THUMBNAIL = 'updatePhotoThumbnail';
25    static readonly TAKE_PICTURE = 'takePicture';
26    static readonly UPDATE_VIDEO_THUMBNAIL = 'updateVideoThumbnail';
27    static readonly APPLICATION_WHOLE_LIFE = 'applicationWholeLife';
28    static readonly APPLICATION_VISIBLE_LIFE = 'applicationVisibleLife';
29    static readonly APPLICATION_FOREGROUND_LIFE = 'applicationForegroundLife';
30    static readonly ABILITY_VISIBLE_LIFE = 'abilityVisibleLife';
31    static readonly ABILITY_FOREGROUND_LIFE = 'abilityForegroundLife';
32    static readonly ABILITY_WHOLE_LIFE = 'abilityWholeLife';
33    static readonly X_COMPONENT_LIFE = 'XComponentLife';
34    static readonly OPEN_FAIL= {
35        domain: "CAMERA_APP",
36        name: "CAMERA_FAULT",
37        eventType: hiSysEvent.EventType.FAULT,
38        params: {
39            FAULT_ID: "OPEN_FAIL",
40            MSG: "打开相机失败"
41        }
42    }
43    static readonly CAMERA_ERROR= {
44        domain: "CAMERA_APP",
45        name: "CAMERA_FAULT",
46        eventType: hiSysEvent.EventType.FAULT,
47        params: {
48            FAULT_ID: "CAMERA_ERROR",
49            MSG: "相机运行时出错"
50        }
51    }
52    static readonly CAPTURE_FAIL= {
53        domain: "CAMERA_APP",
54        name: "CAMERA_FAULT",
55        eventType: hiSysEvent.EventType.FAULT,
56        params: {
57            FAULT_ID: "CAPTURE_FAIL",
58            MSG: "拍照异常"
59        }
60    }
61    static readonly SAVE_FAIL= {
62        domain: "CAMERA_APP",
63        name: "CAMERA_FAULT",
64        eventType: hiSysEvent.EventType.FAULT,
65        params: {
66            FAULT_ID: "SAVE_FAIL",
67            MSG: "媒体落盘异常"
68        }
69    }
70    static readonly SWITCH_TIMEOUT= {
71        domain: "CAMERA_APP",
72        name: "CAMERA_FAULT",
73        eventType: hiSysEvent.EventType.FAULT,
74        params: {
75            FAULT_ID: "SWITCH_TIMEOUT",
76            MSG: "切换摄像头超时"
77        }
78    }
79    static readonly START_RECORD_TIMEOUT= {
80        domain: "CAMERA_APP",
81        name: "CAMERA_FAULT",
82        eventType: hiSysEvent.EventType.FAULT,
83        params: {
84            FAULT_ID: "RECORD_TIMEOUT",
85            MSG: "录像启动超时"
86        }
87    }
88    static readonly FINISH_RECORD_TIMEOUT= {
89        domain: "CAMERA_APP",
90        name: "CAMERA_FAULT",
91        eventType: hiSysEvent.EventType.FAULT,
92        params: {
93            FAULT_ID: "RECORD_TIMEOUT",
94            MSG: "录像停止超时"
95        }
96    }
97    static readonly START_TIMEOUT= {
98        domain: "CAMERA_APP",
99        name: "CAMERA_FAULT",
100        eventType: hiSysEvent.EventType.FAULT,
101        params: {
102            FAULT_ID: "START_TIMEOUT",
103            MSG: "启动超时"
104        }
105    }
106    static readonly CAPTURE_TIMEOUT= {
107        domain: "CAMERA_APP",
108        name: "CAMERA_FAULT",
109        eventType: hiSysEvent.EventType.FAULT,
110        params: {
111            FAULT_ID: "CAPTURE_TIMEOUT",
112            MSG: "拍照超时"
113        }
114    }
115    private static readonly RECORD_TRACE = true;
116    private static readonly TRACE_LIMIT= 2000;
117    private static readonly TRACE_BASE_INDEX= 10000;
118
119    static start(methodName: string) {
120        if (!Trace.RECORD_TRACE) return;
121        if (typeof globalThis.taskIdMap === 'undefined' || typeof globalThis.traceIndex === 'undefined') {
122            Trace.init();
123        }
124        let taskId = globalThis.taskIdMap.get(methodName);
125        if (taskId == undefined) {
126            taskId = globalThis.traceIndex;
127            globalThis.traceIndex++;
128            globalThis.taskIdMap.set(methodName, taskId);
129        }
130        byTrace.startTrace(methodName, taskId, Trace.TRACE_LIMIT);
131    }
132
133    private static init() {
134        globalThis.taskIdMap = new Map<string, number>();
135        globalThis.traceIndex = Trace.TRACE_BASE_INDEX;
136    }
137
138    static end(methodName: string) {
139        if (!Trace.RECORD_TRACE) return;
140        if (typeof globalThis.taskIdMap === 'undefined') {
141            Trace.init();
142        }
143        const taskId = globalThis.taskIdMap.get(methodName);
144        if (taskId == undefined) {
145            Log.error(`fail to end trace name ${methodName}`)
146            return;
147        }
148        byTrace.finishTrace(methodName, taskId);
149    }
150
151    static write(event: any) {
152        if (event) {
153            hiSysEvent.write(event, (err, val) => {
154                if (err) {
155                    Log.error(`fail to return hiSysEvent.`)
156                    return;
157                }
158            })
159        }
160    }
161}