1use std::fmt::Write;
2
3#[cfg(not(any(target_arch = "wasm32", target_os = "android")))]
4pub mod error_log_desktop;
5
6#[macro_export]
7macro_rules!log {
8 ( $ ( $ t: tt) *) => {
9 $crate::makepad_error_log::log_with_type(file!(), line!(), column!(), line!(), column!() + 4, &format!( $ ( $ t) *), $ crate::makepad_error_log::LogType::Log)
10 }
11}
12
13#[macro_export]
14macro_rules!error {
15 ( $ ( $ t: tt) *) => {
16 $crate::makepad_error_log::log_with_type(file!(), line!(), column!(), line!(), column!() + 4, &format!( $ ( $ t) *), $ crate::makepad_error_log::LogType::Log)
17 }
18}
19
20pub enum LogType {
21 Error,
22 Log,
23 Panic
24}
25
26impl LogType {
27 pub fn make_json(&self, file: &str, line_start: u32, column_start: u32, line_end: u32, column_end: u32, message: &str) -> String {
28 let mut out = String::new();
29 let _ = write!(out, "{{\"reason\":\"makepad-error-log\",");
30 let _ = write!(out, "\"message\":{{\"message\":\"");
31 for c in message.chars() {
32 match c {
33 '\n' => {out.push('\\'); out.push('n');},
34 '\r' => {out.push('\\'); out.push('r');},
35 '\t' => {out.push('\\'); out.push('t');},
36 '\0' => {out.push('\\'); out.push('0');},
37 '\\' => {out.push('\\'); out.push('\\');},
38 '"' => {out.push('\\'); out.push('"');},
39 _ => out.push(c)
40 }
41 }
42 let _ = write!(out, "\",");
43 let _ = match self {
44 LogType::Error => write!(out, "\"level\":\"error\","),
45 LogType::Log => write!(out, "\"level\":\"log\","),
46 LogType::Panic => write!(out, "\"level\":\"panic\","),
47 };
48 let _ = write!(out, "\"spans\":[{{");
49 let _ = write!(out, "\"file_name\":\"{}\",", file);
50 let _ = write!(out, "\"byte_start\":0,");
51 let _ = write!(out, "\"byte_end\":0,");
52 let _ = write!(out, "\"line_start\":{},", line_start + 1);
53 let _ = write!(out, "\"line_end\":{},", line_end + 1);
54 let _ = write!(out, "\"column_start\":{},", column_start);
55 let _ = write!(out, "\"column_end\":{},", column_end);
56 let _ = write!(out, "\"is_primary\":true,");
57 let _ = write!(out, "\"text\":[]");
58 let _ = write!(out, "}}],");
59 let _ = write!(out, "\"children\":[]");
60 let _ = write!(out, "}}");
61 let _ = write!(out, "}}");
62 out
63 }
64}
65
66#[cfg(target_os = "android")]
67#[macro_use]
68pub mod error_log_android;
69
70#[cfg(target_arch = "wasm32")]
71#[macro_use]
72pub mod error_log_wasm;
73
74#[cfg(not(any(target_arch = "wasm32", target_os = "android")))]
75pub use error_log_desktop::*;
76#[cfg(not(any(target_arch = "wasm32", target_os = "android")))]
77pub use error_log_desktop as makepad_error_log;
78
79#[cfg(target_os = "android")]
80pub use error_log_android::*;
81#[cfg(target_os = "android")]
82pub use error_log_android as makepad_error_log;
83
84#[cfg(target_arch = "wasm32")]
85pub use error_log_wasm::*;
86#[cfg(target_arch = "wasm32")]
87pub use error_log_wasm as makepad_error_log;
88
89use std::time::Instant;
90
91pub fn profile_start() -> Instant {
92 Instant::now()
93}
94
95#[macro_export]
96macro_rules!profile_end {
97 ( $ inst: expr) => {
98 $crate::makepad_error_log::log_with_type(
99 file!(),
100 line!(),
101 column!(),
102 line!(),
103 column!() + 4,
104 &format!("Profile time {} ms", ( $ inst.elapsed().as_nanos() as f64) / 1000000f64),
105 $crate::makepad_error_log::LogType::Log
106 )
107 }
108}
109