• 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 
16 //! This module is used to Asset service hisysevent.
17 
18 use std::time::Instant;
19 
20 use asset_constants::CallingInfo;
21 use asset_definition::{AssetError, Result};
22 use asset_log::{loge, logi};
23 
24 use hisysevent::{build_number_param, build_str_param, write, EventType, HiSysEventParam};
25 
26 /// System events structure which base on `Hisysevent`.
27 struct SysEvent<'a> {
28     event_type: EventType,
29     params: Vec<HiSysEventParam<'a>>,
30 }
31 
32 impl<'a> SysEvent<'a> {
33     const DOMAIN: &str = "ASSET";
34     const ASSET_FAULT: &str = "SECRET_STORE_OPERATION_FAILED";
35     const ASSET_STATISTIC: &str = "SECRET_STORE_INFO_COLLECTION";
36 
37     pub(crate) const FUNCTION: &str = "FUNCTION";
38     pub(crate) const USER_ID: &str = "USER_ID";
39     pub(crate) const CALLER: &str = "CALLER";
40     pub(crate) const ERROR_CODE: &str = "ERROR_CODE";
41     pub(crate) const RUN_TIME: &str = "RUN_TIME";
42     pub(crate) const EXTRA: &str = "EXTRA";
43 
new(event_type: EventType) -> Self44     fn new(event_type: EventType) -> Self {
45         Self { event_type, params: Vec::new() }
46     }
47 
set_param(mut self, param: HiSysEventParam<'a>) -> Self48     fn set_param(mut self, param: HiSysEventParam<'a>) -> Self {
49         self.params.push(param);
50         self
51     }
52 
write(self)53     fn write(self) {
54         let event_name = match self.event_type {
55             EventType::Fault => Self::ASSET_FAULT,
56             EventType::Statistic => Self::ASSET_STATISTIC,
57             _ => "UNKNOWN_EVENT",
58         };
59         write(Self::DOMAIN, event_name, self.event_type, self.params.as_slice());
60     }
61 }
62 
upload_statistic_system_event(calling_info: &CallingInfo, start_time: Instant, func_name: &str)63 pub(crate) fn upload_statistic_system_event(calling_info: &CallingInfo, start_time: Instant, func_name: &str) {
64     let duration = start_time.elapsed();
65     let owner_info = String::from_utf8_lossy(calling_info.owner_info()).to_string();
66     SysEvent::new(EventType::Statistic)
67         .set_param(build_str_param!(SysEvent::FUNCTION, func_name))
68         .set_param(build_number_param!(SysEvent::USER_ID, calling_info.user_id()))
69         .set_param(build_str_param!(SysEvent::CALLER, owner_info.clone()))
70         .set_param(build_number_param!(SysEvent::RUN_TIME, duration.as_millis() as u32))
71         .set_param(build_str_param!(SysEvent::EXTRA, ""))
72         .write();
73     logi!(
74         "[INFO]Calling fun:[{}], user_id:[{}], caller:[{}], start_time:[{:?}], run_time:[{}]",
75         func_name,
76         calling_info.user_id(),
77         owner_info,
78         start_time,
79         duration.as_millis()
80     )
81 }
82 
upload_fault_system_event( calling_info: &CallingInfo, start_time: Instant, func_name: &str, e: &AssetError, )83 pub(crate) fn upload_fault_system_event(
84     calling_info: &CallingInfo,
85     start_time: Instant,
86     func_name: &str,
87     e: &AssetError,
88 ) {
89     let owner_info = String::from_utf8_lossy(calling_info.owner_info()).to_string();
90     SysEvent::new(EventType::Fault)
91         .set_param(build_str_param!(SysEvent::FUNCTION, func_name))
92         .set_param(build_number_param!(SysEvent::USER_ID, calling_info.user_id()))
93         .set_param(build_str_param!(SysEvent::CALLER, owner_info.clone()))
94         .set_param(build_number_param!(SysEvent::ERROR_CODE, e.code as i32))
95         .set_param(build_str_param!(SysEvent::EXTRA, e.msg.clone()))
96         .write();
97     loge!(
98         "[ERROR]Calling fun:[{}], user_id:[{}], caller:[{}], start_time:[{:?}], error_code:[{}], error_msg:[{}]",
99         func_name,
100         calling_info.user_id(),
101         owner_info,
102         start_time,
103         e.code,
104         e.msg.clone()
105     );
106 }
107 
upload_system_event<T>( result: Result<T>, calling_info: &CallingInfo, start_time: Instant, func_name: &str, ) -> Result<T>108 pub(crate) fn upload_system_event<T>(
109     result: Result<T>,
110     calling_info: &CallingInfo,
111     start_time: Instant,
112     func_name: &str,
113 ) -> Result<T> {
114     match &result {
115         Ok(_) => upload_statistic_system_event(calling_info, start_time, func_name),
116         Err(e) => upload_fault_system_event(calling_info, start_time, func_name, e),
117     }
118     result
119 }
120