xref: /aosp_15_r20/frameworks/native/libs/debugstore/rust/src/lib.rs (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 //! # Debug Store Crate
18 /// The Debug Store Crate provides functionalities for storing debug events.
19 /// It allows logging and retrieval of debug events, and associated data.
20 mod core;
21 mod event;
22 mod event_type;
23 mod storage;
24 
25 pub use core::*;
26 pub use event::*;
27 
28 use cxx::{CxxString, CxxVector};
29 
30 #[cxx::bridge(namespace = "android::debugstore")]
31 #[allow(unsafe_op_in_unsafe_fn)]
32 mod cxxffi {
33     extern "Rust" {
debug_store_to_string() -> String34         fn debug_store_to_string() -> String;
debug_store_record(name: &CxxString, data: &CxxVector<CxxString>)35         fn debug_store_record(name: &CxxString, data: &CxxVector<CxxString>);
debug_store_begin(name: &CxxString, data: &CxxVector<CxxString>) -> u6436         fn debug_store_begin(name: &CxxString, data: &CxxVector<CxxString>) -> u64;
debug_store_end(id: u64, data: &CxxVector<CxxString>)37         fn debug_store_end(id: u64, data: &CxxVector<CxxString>);
38     }
39 
40     #[namespace = "android"]
41     unsafe extern "C++" {
42         include!("utils/SystemClock.h");
uptimeMillis() -> i6443         fn uptimeMillis() -> i64;
44     }
45 }
46 
debug_store_to_string() -> String47 fn debug_store_to_string() -> String {
48     DebugStore::get_instance().to_string()
49 }
50 
debug_store_record(name: &CxxString, data: &CxxVector<CxxString>)51 fn debug_store_record(name: &CxxString, data: &CxxVector<CxxString>) {
52     DebugStore::get_instance().record(name.to_string_lossy().into_owned(), cxx_vec_to_pairs(data));
53 }
54 
debug_store_begin(name: &CxxString, data: &CxxVector<CxxString>) -> u6455 fn debug_store_begin(name: &CxxString, data: &CxxVector<CxxString>) -> u64 {
56     DebugStore::get_instance().begin(name.to_string_lossy().into_owned(), cxx_vec_to_pairs(data))
57 }
58 
debug_store_end(id: u64, data: &CxxVector<CxxString>)59 fn debug_store_end(id: u64, data: &CxxVector<CxxString>) {
60     DebugStore::get_instance().end(id, cxx_vec_to_pairs(data));
61 }
62 
cxx_vec_to_pairs(vec: &CxxVector<CxxString>) -> Vec<(String, String)>63 fn cxx_vec_to_pairs(vec: &CxxVector<CxxString>) -> Vec<(String, String)> {
64     vec.iter()
65         .map(|s| s.to_string_lossy().into_owned())
66         .collect::<Vec<_>>()
67         .chunks(2)
68         .filter_map(|chunk| match chunk {
69             [k, v] => Some((k.clone(), v.clone())),
70             _ => None,
71         })
72         .collect()
73 }
74