use crate::{
context::CTX,
level::Level,
facades::FacadeVariant,
options::Options,
util::read_var_from_env,
Scope, LogKey, ScopeKey, ContextKey, Result,
};
use std::fmt::{self, Display};
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub enum InternalLogKeys {
Internal,
LogCompat,
}
impl Display for InternalLogKeys {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Self::Internal => f.write_str("hclog"),
Self::LogCompat => f.write_str("logcompat"),
}
}
}
impl Scope for InternalLogKeys {
fn logscope() -> ScopeKey { ScopeKey::CLog }
fn init<S: Display>(
name: S, level: Level, facade: FacadeVariant, options: Options
) -> Result<()> {
let mut level = level;
let mut facade = facade;
if let Ok(Some(f)) = read_var_from_env::<u8>("CLOG_DEBUG") {
facade = FacadeVariant::StdOut;
level = Level::debug_level(f);
}
{
CTX::get_mut()?.init_mod::<Self, S>(name, level, facade, options)?
.add_submodule(Self::Internal)?;
}
Ok(())
}
}
impl LogKey for InternalLogKeys {
fn log_key(&self) -> ContextKey { *self as usize }
}
pub (crate) mod test {
use crate::{Scope, LogKey, ContextKey, Result, Level, FacadeVariant, options::Options};
use std::fmt::{self, Display};
#[derive(Copy, Clone, Debug)]
pub enum TestKeys {
LIBTESTFOO,
LIBTESTBAR,
}
use TestKeys::*;
impl LogKey for TestKeys {
fn log_key(&self) -> ContextKey { *self as usize }
}
impl Scope for TestKeys {
fn init<S: Display>(
name: S, level: Level, facade: FacadeVariant, options: Options
) -> Result<()> {
crate::init::<Self, S>(name, level, facade, options)?;
crate::add_submodules(&[LIBTESTFOO, LIBTESTBAR])
}
}
impl Display for TestKeys {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Self::LIBTESTFOO => write!(f, "libtestfoo"),
Self::LIBTESTBAR => write!(f, "libtestbar"),
}
}
}
}