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
use log::{LevelFilter, SetLoggerError};
//use std::path::PathBuf;

/*pub struct HttpConfig {
    method: reqwest::Method,
    url: String,
    authorization_token: String,
}

pub enum OutputType {
    Json,
    Stdio,
}

pub struct FileSystemConfig {
    file_name: String,
    output_type: OutputType,
    output_path: PathBuf,
}*/

/// The primary logging struct that contains all runtime configuration.
pub struct DioxusLogger {
    //http_config: Option<HttpConfig>,
    //file_system_config: Option<FileSystemConfig>,
    level_filter: LevelFilter,
}

impl DioxusLogger {
    pub fn new(
        level_filter: LevelFilter,
        //file_system_config: Option<FileSystemConfig>,
        //http_config: Option<HttpConfig>,
    ) -> Self {
        Self {
            //http_config,
            //file_system_config,
            level_filter,
        }
    }
}

impl log::Log for DioxusLogger {
    fn enabled(&self, metadata: &log::Metadata) -> bool {
        metadata.level() <= self.level_filter
    }

    fn log(&self, record: &log::Record) {
        if !self.enabled(record.metadata()) {
            return;
        }

        let formatted = format!("[{}] {} - {}", record.level(), record.module_path().unwrap_or(""), record.args());

        #[cfg(all(
            not(target_family = "wasm"),
            not(target_family = "android"),
            not(target_family = "ios")
        ))]
        println!("{formatted}");

        #[cfg(target_family = "wasm")]
        web_sys::console::log_1(&formatted.into());
    }

    fn flush(&self) {}
}

/// Initialize `log` and `dioxus-logger` with a specified filter.
pub fn init(level_filter: LevelFilter) -> Result<(), SetLoggerError> {
    let logger = DioxusLogger::new(level_filter);
    log::set_boxed_logger(Box::new(logger)).map(|()| log::set_max_level(level_filter))
}