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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use std::fmt::Write;

#[cfg(not(any(target_arch = "wasm32", target_os = "android")))]
pub mod error_log_desktop;

#[macro_export]
macro_rules!log {
    ( $ ( $ t: tt) *) => {
        $crate::makepad_error_log::log_with_type(file!(), line!(), column!(), line!(), column!() + 4, &format!( $ ( $ t) *), $ crate::makepad_error_log::LogType::Log)
    }
}

#[macro_export]
macro_rules!error {
    ( $ ( $ t: tt) *) => {
        $crate::makepad_error_log::log_with_type(file!(), line!(), column!(), line!(), column!() + 4, &format!( $ ( $ t) *), $ crate::makepad_error_log::LogType::Log)
    }
}

pub enum LogType {
    Error,
    Log,
    Panic
}

impl LogType {
    pub fn make_json(&self, file: &str, line_start: u32, column_start: u32, line_end: u32, column_end: u32, message: &str) -> String {
        let mut out = String::new();
        let _ = write!(out, "{{\"reason\":\"makepad-error-log\",");
        let _ = write!(out, "\"message\":{{\"message\":\"");
        for c in message.chars() {
            match c {
                '\n' => {out.push('\\'); out.push('n');},
                '\r' => {out.push('\\'); out.push('r');},
                '\t' => {out.push('\\'); out.push('t');},
                '\0' => {out.push('\\'); out.push('0');},
                '\\' => {out.push('\\'); out.push('\\');},
                '"' => {out.push('\\'); out.push('"');},
                _ => out.push(c)
            }
        }
        let _ = write!(out, "\",");
        let _ = match self {
            LogType::Error => write!(out, "\"level\":\"error\","),
            LogType::Log => write!(out, "\"level\":\"log\","),
            LogType::Panic => write!(out, "\"level\":\"panic\","),
        };
        let _ = write!(out, "\"spans\":[{{");
        let _ = write!(out, "\"file_name\":\"{}\",", file);
        let _ = write!(out, "\"byte_start\":0,");
        let _ = write!(out, "\"byte_end\":0,");
        let _ = write!(out, "\"line_start\":{},", line_start + 1);
        let _ = write!(out, "\"line_end\":{},", line_end + 1);
        let _ = write!(out, "\"column_start\":{},", column_start);
        let _ = write!(out, "\"column_end\":{},", column_end);
        let _ = write!(out, "\"is_primary\":true,");
        let _ = write!(out, "\"text\":[]");
        let _ = write!(out, "}}],");
        let _ = write!(out, "\"children\":[]");
        let _ = write!(out, "}}");
        let _ = write!(out, "}}");
        out
    }
}

#[cfg(target_os = "android")]
#[macro_use]
pub mod error_log_android;

#[cfg(target_arch = "wasm32")]
#[macro_use]
pub mod error_log_wasm;

#[cfg(not(any(target_arch = "wasm32", target_os = "android")))]
pub use error_log_desktop::*;
#[cfg(not(any(target_arch = "wasm32", target_os = "android")))]
pub use error_log_desktop as makepad_error_log;

#[cfg(target_os = "android")]
pub use error_log_android::*;
#[cfg(target_os = "android")]
pub use error_log_android as makepad_error_log;

#[cfg(target_arch = "wasm32")]
pub use error_log_wasm::*;
#[cfg(target_arch = "wasm32")]
pub use error_log_wasm as makepad_error_log;

use std::time::Instant;

pub fn profile_start() -> Instant {
    Instant::now()
}

#[macro_export]
macro_rules!profile_end {
    ( $ inst: expr) => {
        $crate::makepad_error_log::log_with_type(
            file!(),
            line!(),
            column!(),
            line!(),
            column!() + 4,
            &format!("Profile time {} ms", ( $ inst.elapsed().as_nanos() as f64) / 1000000f64),
            $crate::makepad_error_log::LogType::Log
        )
    }
}