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 
16 //! This module is used to Asset service hitrace.
17 
18 use hitrace_meter_rust::{finish_trace, start_trace};
19 
20 /// Hitrace adapter which provides timing capability.
21 ///
22 /// The timing will end automatically when the structure drops. Users should
23 /// take care that the lifetime of this structure.
24 pub(crate) struct TraceScope {
25     label: u64,
26 }
27 
28 impl TraceScope {
29     const HITRACE_TAG_SECURITY: u64 = 1u64 << 7;
30 
31     /// Starts tracing.
trace(value: &str) -> Self32     pub(crate) fn trace(value: &str) -> Self {
33         let trace = Self { label: Self::HITRACE_TAG_SECURITY };
34         start_trace(trace.label, value);
35         trace
36     }
37 }
38 
39 impl Drop for TraceScope {
40     // Finish tracing. The timing will end automatically when the structure drops.
drop(&mut self)41     fn drop(&mut self) {
42         finish_trace(self.label);
43     }
44 }
45