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
//! Logging utilities

use std::str;

static mut DEBUG_HOOK: Option<&dyn DebugSink> = None;
static mut LOG_HOOK: Option<&dyn LoggingSink> = None;

pub trait DebugSink {
    fn debug(&self, topic: &str, comment: &str);
}

pub trait LoggingSink {
    fn log(&self, comment: &str);
}

pub unsafe fn add_debug_hook(hook: &'static dyn DebugSink) {
    match DEBUG_HOOK {
        None => DEBUG_HOOK = Some(hook),
        Some(_) => (),
    };
}

pub fn debug(topic: &str, content: &str) {
    unsafe {
        match DEBUG_HOOK {
            None => eprintln!("{} {}", topic, content),
            Some(dh) => dh.debug(topic, content),
        }
    };
}

pub unsafe fn add_logging_hook(hook: &'static dyn LoggingSink) {
    match LOG_HOOK {
        None => LOG_HOOK = Some(hook),
        Some(_) => (),
    };
}

pub fn log(content: &str) {
    unsafe {
        match LOG_HOOK {
            None => println!("{}", content),
            Some(lh) => lh.log(content),
        }
    };
}