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
//! Minimal logging library that uses explicit and configurable endpoints.
#![warn(missing_docs)]

use crate::logger::Logger;
use std::{collections::HashMap, sync::Mutex};

mod builders;
mod endpoint;
mod log;
mod logger;
mod scoped;

pub use builders::{EndpointBuilder, LoggerBuilder};
pub use endpoint::EndpointSuper;
pub use log::{impl_log, LogEntry};
pub use logger::KeepAlive;
pub use scoped::{impl_slog, scoped};

lazy_static::lazy_static! {
    static ref ENDPOINT_TYPE: Mutex<Option<std::any::TypeId>> = Mutex::new(None);
    static ref LOGGER: Mutex<Option<Logger>> = Mutex::new(None);
}

/// Log to the endpoint that is currently in scope. Panics if there's no scope active.
#[macro_export]
macro_rules! slog {
    ($($arg:tt)*) => {
        aether::impl_slog(format!($($arg)*))
    };
}

/// Log to the specified endpoint.
#[macro_export]
macro_rules! log {
    ($target:expr, $($arg:tt)*) => {
        aether::impl_log($target, format!($($arg)*))
    };
}

/// Constructs a builder for the logger.
pub fn init<EP: EndpointSuper>() -> LoggerBuilder<EP> {
    let mut guard = ENDPOINT_TYPE.lock().unwrap();
    if guard.replace(std::any::TypeId::of::<EP>()).is_some() {
        panic!("Logger has already been (at least partially) initialized!");
    }

    LoggerBuilder {
        base_path: None,
        fmt: |log| {
            format!(
                "{} [{:?}] {}",
                log.time.format("%T.%3f"),
                log.endpoint,
                log.text
            )
        },
        endpoints: HashMap::new(),
    }
}