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
use crate::Logger;
use crate::Result;
use log::{Metadata, Record};
use wapc_guest::host_call;
use wascc_codec::logging::*;
use wascc_codec::serialize;

/// The reserved capability ID for the logging functionality
pub const CAPID_LOGGING: &str = "wascc:logging";

const ERROR: usize = 1;
const WARN: usize = 2;
const INFO: usize = 3;
const DEBUG: usize = 4;
const TRACE: usize = 5;

pub struct AutomaticLogger {}

impl AutomaticLogger {
    pub fn new() -> Self {
        Self::default()
    }
    fn _log(&self, req: WriteLogRequest) {
        let _ = host_call(CAPID_LOGGING, OP_LOG, &serialize(req).unwrap());
    }
}

impl Default for AutomaticLogger {
    fn default() -> Self {
        AutomaticLogger {}
    }
}

impl log::Log for AutomaticLogger {
    fn enabled(&self, _metadata: &Metadata) -> bool {
        true
    }

    fn log(&self, record: &Record) {
        if self.enabled(record.metadata()) {
            let l = WriteLogRequest {
                level: record.level() as _,
                body: format!("{}", record.args()),
            };
            self._log(l)
        }
    }

    fn flush(&self) {}
}
impl Logger for AutomaticLogger {
    fn log(&self, level: usize, body: &str) -> Result<()> {
        let l = WriteLogRequest {
            level: level,
            body: body.to_string(),
        };
        let _ = host_call(CAPID_LOGGING, OP_LOG, &serialize(l)?);
        Ok(())
    }
    fn error(&self, body: &str) -> Result<()> {
        let l = WriteLogRequest {
            level: ERROR,
            body: body.to_string(),
        };
        let _ = host_call(CAPID_LOGGING, OP_LOG, &serialize(l)?);
        Ok(())
    }

    fn warn(&self, body: &str) -> Result<()> {
        let l = WriteLogRequest {
            level: WARN,
            body: body.to_string(),
        };
        let _ = host_call(CAPID_LOGGING, OP_LOG, &serialize(l)?);
        Ok(())
    }

    fn info(&self, body: &str) -> Result<()> {
        let l = WriteLogRequest {
            level: INFO,
            body: body.to_string(),
        };
        let _ = host_call(CAPID_LOGGING, OP_LOG, &serialize(l)?);
        Ok(())
    }

    fn debug(&self, body: &str) -> Result<()> {
        let l = WriteLogRequest {
            level: DEBUG,
            body: body.to_string(),
        };
        let _ = host_call(CAPID_LOGGING, OP_LOG, &serialize(l)?);
        Ok(())
    }

    fn trace(&self, body: &str) -> Result<()> {
        let l = WriteLogRequest {
            level: TRACE,
            body: body.to_string(),
        };
        let _ = host_call(CAPID_LOGGING, OP_LOG, &serialize(l)?);
        Ok(())
    }
}