wiggle: add config options for logging
This commit is contained in:
@@ -18,6 +18,7 @@ pub struct Config {
|
|||||||
pub witx: WitxConf,
|
pub witx: WitxConf,
|
||||||
pub ctx: CtxConf,
|
pub ctx: CtxConf,
|
||||||
pub errors: ErrorConf,
|
pub errors: ErrorConf,
|
||||||
|
pub logging: LoggingConf,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
@@ -25,25 +26,45 @@ pub enum ConfigField {
|
|||||||
Witx(WitxConf),
|
Witx(WitxConf),
|
||||||
Ctx(CtxConf),
|
Ctx(CtxConf),
|
||||||
Error(ErrorConf),
|
Error(ErrorConf),
|
||||||
|
Logging(LoggingConf),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ConfigField {
|
mod kw {
|
||||||
pub fn parse_pair(ident: &str, value: ParseStream, err_loc: Span) -> Result<Self> {
|
syn::custom_keyword!(witx);
|
||||||
match ident {
|
syn::custom_keyword!(witx_literal);
|
||||||
"witx" => Ok(ConfigField::Witx(WitxConf::Paths(value.parse()?))),
|
syn::custom_keyword!(ctx);
|
||||||
"witx_literal" => Ok(ConfigField::Witx(WitxConf::Literal(value.parse()?))),
|
syn::custom_keyword!(logging);
|
||||||
"ctx" => Ok(ConfigField::Ctx(value.parse()?)),
|
syn::custom_keyword!(errors);
|
||||||
"errors" => Ok(ConfigField::Error(value.parse()?)),
|
syn::custom_keyword!(log);
|
||||||
_ => Err(Error::new(err_loc, "expected `witx`, `ctx`, or `errors`")),
|
syn::custom_keyword!(tracing);
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Parse for ConfigField {
|
impl Parse for ConfigField {
|
||||||
fn parse(input: ParseStream) -> Result<Self> {
|
fn parse(input: ParseStream) -> Result<Self> {
|
||||||
let id: Ident = input.parse()?;
|
let lookahead = input.lookahead1();
|
||||||
let _colon: Token![:] = input.parse()?;
|
if lookahead.peek(kw::witx) {
|
||||||
Self::parse_pair(id.to_string().as_ref(), input, id.span())
|
input.parse::<kw::witx>()?;
|
||||||
|
input.parse::<Token![:]>()?;
|
||||||
|
Ok(ConfigField::Witx(WitxConf::Paths(input.parse()?)))
|
||||||
|
} else if lookahead.peek(kw::witx_literal) {
|
||||||
|
input.parse::<kw::witx_literal>()?;
|
||||||
|
input.parse::<Token![:]>()?;
|
||||||
|
Ok(ConfigField::Witx(WitxConf::Literal(input.parse()?)))
|
||||||
|
} else if lookahead.peek(kw::ctx) {
|
||||||
|
input.parse::<kw::ctx>()?;
|
||||||
|
input.parse::<Token![:]>()?;
|
||||||
|
Ok(ConfigField::Ctx(input.parse()?))
|
||||||
|
} else if lookahead.peek(kw::logging) {
|
||||||
|
input.parse::<kw::logging>()?;
|
||||||
|
input.parse::<Token![:]>()?;
|
||||||
|
Ok(ConfigField::Logging(input.parse()?))
|
||||||
|
} else if lookahead.peek(kw::errors) {
|
||||||
|
input.parse::<kw::errors>()?;
|
||||||
|
input.parse::<Token![:]>()?;
|
||||||
|
Ok(ConfigField::Error(input.parse()?))
|
||||||
|
} else {
|
||||||
|
Err(lookahead.error())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,6 +73,7 @@ impl Config {
|
|||||||
let mut witx = None;
|
let mut witx = None;
|
||||||
let mut ctx = None;
|
let mut ctx = None;
|
||||||
let mut errors = None;
|
let mut errors = None;
|
||||||
|
let mut logging = None;
|
||||||
for f in fields {
|
for f in fields {
|
||||||
match f {
|
match f {
|
||||||
ConfigField::Witx(c) => {
|
ConfigField::Witx(c) => {
|
||||||
@@ -72,6 +94,12 @@ impl Config {
|
|||||||
}
|
}
|
||||||
errors = Some(c);
|
errors = Some(c);
|
||||||
}
|
}
|
||||||
|
ConfigField::Logging(c) => {
|
||||||
|
if logging.is_some() {
|
||||||
|
return Err(Error::new(err_loc, "duplicate `logging` field"));
|
||||||
|
}
|
||||||
|
logging = Some(c);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(Config {
|
Ok(Config {
|
||||||
@@ -82,6 +110,7 @@ impl Config {
|
|||||||
.take()
|
.take()
|
||||||
.ok_or_else(|| Error::new(err_loc, "`ctx` field required"))?,
|
.ok_or_else(|| Error::new(err_loc, "`ctx` field required"))?,
|
||||||
errors: errors.take().unwrap_or_default(),
|
errors: errors.take().unwrap_or_default(),
|
||||||
|
logging: logging.take().unwrap_or_default(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -290,3 +319,34 @@ impl Parse for ErrorConfField {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Configure logging statements in generated code
|
||||||
|
pub enum LoggingConf {
|
||||||
|
Log { cfg_feature: Option<String> },
|
||||||
|
Tracing,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for LoggingConf {
|
||||||
|
fn default() -> Self {
|
||||||
|
// For compatibility with existing code. We should probably revise this later.
|
||||||
|
LoggingConf::Log {
|
||||||
|
cfg_feature: Some("trace_log".to_owned()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Parse for LoggingConf {
|
||||||
|
fn parse(input: ParseStream) -> Result<Self> {
|
||||||
|
let lookahead = input.lookahead1();
|
||||||
|
if lookahead.peek(kw::log) {
|
||||||
|
input.parse::<kw::log>()?;
|
||||||
|
// TODO: syntax so the user can specify their own cfg_feature guard here.
|
||||||
|
Ok(LoggingConf::Log { cfg_feature: None })
|
||||||
|
} else if lookahead.peek(kw::tracing) {
|
||||||
|
input.parse::<kw::tracing>()?;
|
||||||
|
Ok(LoggingConf::Tracing)
|
||||||
|
} else {
|
||||||
|
Err(lookahead.error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user