iris_chat_core/
perflog.rs1use std::time::{SystemTime, UNIX_EPOCH};
7
8#[inline]
9pub fn now_ms() -> u64 {
10 SystemTime::now()
11 .duration_since(UNIX_EPOCH)
12 .map(|d| d.as_millis() as u64)
13 .unwrap_or(0)
14}
15
16#[cfg(target_os = "android")]
17mod sink {
18 use std::ffi::CString;
19
20 const ANDROID_LOG_INFO: i32 = 4;
22
23 extern "C" {
24 fn __android_log_write(prio: i32, tag: *const u8, msg: *const u8) -> i32;
25 }
26
27 pub fn write(tag: &str, msg: &str) {
28 if let (Ok(t), Ok(m)) = (CString::new(tag), CString::new(msg)) {
30 unsafe {
31 __android_log_write(
32 ANDROID_LOG_INFO,
33 t.as_ptr() as *const u8,
34 m.as_ptr() as *const u8,
35 );
36 }
37 }
38 }
39}
40
41#[cfg(not(target_os = "android"))]
42mod sink {
43 pub fn write(tag: &str, msg: &str) {
44 eprintln!("{tag}: {msg}");
45 }
46}
47
48#[macro_export]
50macro_rules! perflog {
51 ($($arg:tt)*) => {
52 $crate::perflog::__write(format_args!($($arg)*))
53 };
54}
55
56#[doc(hidden)]
57pub fn __write(args: std::fmt::Arguments<'_>) {
58 let msg = format!("{} {}", now_ms(), args);
59 sink::write("IrisPerf", &msg);
60}