summaryrefslogtreecommitdiff
path: root/libs/debugstore/rust/src/lib.rs
blob: f2195c05295847a76af931ba16fefe53dc1285ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//! # Debug Store Crate
/// The Debug Store Crate provides functionalities for storing debug events.
/// It allows logging and retrieval of debug events, and associated data.
mod core;
mod event;
mod event_type;
mod storage;

pub use core::*;
pub use event::*;

use cxx::{CxxString, CxxVector};

#[cxx::bridge(namespace = "android::debugstore")]
#[allow(unsafe_op_in_unsafe_fn)]
mod cxxffi {
    extern "Rust" {
        fn debug_store_to_string() -> String;
        fn debug_store_record(name: &CxxString, data: &CxxVector<CxxString>);
        fn debug_store_begin(name: &CxxString, data: &CxxVector<CxxString>) -> u64;
        fn debug_store_end(id: u64, data: &CxxVector<CxxString>);
    }

    #[namespace = "android"]
    unsafe extern "C++" {
        include!("utils/SystemClock.h");
        fn uptimeMillis() -> i64;
    }
}

fn debug_store_to_string() -> String {
    DebugStore::get_instance().to_string()
}

fn debug_store_record(name: &CxxString, data: &CxxVector<CxxString>) {
    DebugStore::get_instance().record(name.to_string_lossy().into_owned(), cxx_vec_to_pairs(data));
}

fn debug_store_begin(name: &CxxString, data: &CxxVector<CxxString>) -> u64 {
    DebugStore::get_instance().begin(name.to_string_lossy().into_owned(), cxx_vec_to_pairs(data))
}

fn debug_store_end(id: u64, data: &CxxVector<CxxString>) {
    DebugStore::get_instance().end(id, cxx_vec_to_pairs(data));
}

fn cxx_vec_to_pairs(vec: &CxxVector<CxxString>) -> Vec<(String, String)> {
    vec.iter()
        .map(|s| s.to_string_lossy().into_owned())
        .collect::<Vec<_>>()
        .chunks(2)
        .filter_map(|chunk| match chunk {
            [k, v] => Some((k.clone(), v.clone())),
            _ => None,
        })
        .collect()
}