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//! Rust interfaces for crate hisysevent.
17
18mod sys_event_manager;
19mod sys_event;
20mod utils;
21
22#[macro_use]
23pub mod macros;
24
25pub use sys_event_manager::{HiSysEventRecord, Querier, Watcher};
26
27pub use sys_event::{HiSysEventParam, HiSysEventParamType, HiSysEventParamValue, parse_type_len,
28    build_string_arrays};
29
30/// Enumerate system event types.
31#[non_exhaustive]
32#[derive(Copy, Clone)]
33pub enum EventType {
34    /// Fault event.
35    Fault = 1,
36
37    /// Statistic event.
38    Statistic = 2,
39
40    /// Security event.
41    Security = 3,
42
43    /// System behavior event.
44    Behavior = 4,
45}
46
47/// Write system event.
48pub fn write(event_domain: &str, event_name: &str, event_type: EventType,
49    event_params: &[HiSysEventParam]) -> i32 {
50    sys_event::write(event_domain, event_name, event_type as std::ffi::c_int, event_params)
51}
52
53/// Enumerate search system event rule type.
54#[non_exhaustive]
55#[derive(Copy, Clone)]
56pub enum RuleType {
57    /// Whole word match.
58    WholeWord = 1,
59
60    /// Prefix match.
61    Prefix = 2,
62
63    /// Regular match.
64    Regular = 3,
65}
66
67/// Definition arguments for query system event information.
68#[derive(Copy, Clone)]
69pub struct QueryArg {
70    /// Begin time.
71    pub begin_time: i64,
72
73    /// End time.
74    pub end_time: i64,
75
76    /// Max number of receive system event.
77    pub max_events: i32,
78}
79
80/// Definition event for query system event information.
81pub struct QueryRule<'a> {
82    /// The domain of the event.
83    pub domain: &'a str,
84
85    /// List of event name.
86    pub event_list: Vec<&'a str>,
87
88    /// extra condition for event query.
89    pub condition: &'a str,
90}
91
92/// Query system event.
93pub fn query(query_arg: &QueryArg, query_rules: &[QueryRule], querier: &Querier) -> i32 {
94    sys_event_manager::query(query_arg, query_rules, querier)
95}
96
97/// Definition listener rule for system event information.
98#[derive(Copy, Clone)]
99pub struct WatchRule<'a> {
100    /// The domain of the event.
101    pub domain: &'a str,
102
103    /// The name of the event.
104    pub name: &'a str,
105
106    /// The tag of the event.
107    pub tag: &'a str,
108
109    /// The rule of match system event.
110    pub rule_type: RuleType,
111
112    /// The type of match system event.
113    pub event_type: EventType,
114}
115
116/// Add watcher to watch system event.
117pub fn add_watcher(watcher: &Watcher, watch_rules: &[WatchRule]) -> i32 {
118    sys_event_manager::add_watcher(watcher, watch_rules)
119}
120
121/// Remove watcher.
122pub fn remove_watcher(watcher: &Watcher) -> i32 {
123    sys_event_manager::remove_watcher(watcher)
124}
125