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
110
111
112
113
114
115
#[cfg(test)] #[macro_use] extern crate log;
use log::{Log, Level, Record, Metadata, SetLoggerError};

#[cfg(target_arch = "wasm32")]
extern crate sapp_wasm as sapp;

#[cfg(not(target_arch = "wasm32"))]
pub mod console {
    pub fn debug(_msg: &str) {
        panic!("console::debug only implemented for wasm32 target");
    }
    
    pub fn log(_msg: &str) {
        panic!("console::log only implemented for wasm32 target");
    }

    pub fn info(_msg: &str) {
        panic!("console::info only implemented for wasm32 target");
    }

    pub fn warn(_msg: &str) {
        panic!("console::warn only implemented for wasm32 target");
    }
    
    pub fn error(_msg: &str) {
        panic!("console::error only implemented for wasm32 target");
    }
}

#[cfg(target_arch = "wasm32")]
pub mod console {
    pub fn debug(msg: &str) {
        use std::ffi::CString;
        let string = CString::new(msg).unwrap();
        unsafe { sapp::console_debug(string.as_ptr()); }
    }
    
    pub fn log(msg: &str) {
        use std::ffi::CString;
        let string = CString::new(msg).unwrap();
        unsafe { sapp::console_log(string.as_ptr()); }
    }

    pub fn info(msg: &str) {
        use std::ffi::CString;
        let string = CString::new(msg).unwrap();
        unsafe { sapp::console_info(string.as_ptr()); }
    }

    pub fn warn(msg: &str) {
        use std::ffi::CString;
        let string = CString::new(msg).unwrap();
        unsafe { sapp::console_warn(string.as_ptr()); }
    }
    
    pub fn error(msg: &str) {
        use std::ffi::CString;
        let string = CString::new(msg).unwrap();
        unsafe { sapp::console_error(string.as_ptr()); }
    }
}

fn log_record(record: &Record) {
    // pick the console.log() variant for the appropriate logging level
    let console_send = match record.level() {
        Level::Error => console::error,
        Level::Warn => console::warn,
        Level::Info => console::info,
        Level::Debug => console::log,
        Level::Trace => console::debug,
    };

    console_send(&format!("{}", record.args()));
}

static LOGGER: WasmLogger = WasmLogger {};

struct WasmLogger {}

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

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

        log_record(record);
    }

    fn flush(&self) {}
}

/// Initializes the global logger setting `max_log_level` to the given value.
pub fn init_with_level(level: Level) -> Result<(), SetLoggerError> {
    log::set_logger(&LOGGER)?;
    log::set_max_level(level.to_level_filter());
    Ok(())
}

/// Initializes the global logger with `max_log_level` set to `Level::Debug` (the only supported level for now).
pub fn init() -> Result<(), SetLoggerError> {
    init_with_level(Level::Debug)
}

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() -> Result<(), log::SetLoggerError> {
        super::init()?;
        Ok(debug!("test"))
    }
}