wiggle: can swap in tracing for args

This commit is contained in:
Pat Hickey
2020-05-30 19:06:37 -07:00
parent 994104d615
commit f89fc0ac57
6 changed files with 203 additions and 22 deletions

View File

@@ -321,6 +321,7 @@ impl Parse for ErrorConfField {
}
/// Configure logging statements in generated code
#[derive(Debug, Clone)]
pub enum LoggingConf {
Log { cfg_feature: Option<String> },
Tracing,

View File

@@ -1,6 +1,7 @@
use proc_macro2::TokenStream;
use quote::quote;
use crate::config::LoggingConf;
use crate::error_transform::ErrorTransform;
use crate::lifetimes::anon_lifetime;
use crate::module_trait::passed_by_reference;
@@ -11,6 +12,7 @@ pub fn define_func(
func: &witx::InterfaceFunc,
trait_name: TokenStream,
errxform: &ErrorTransform,
logging: &LoggingConf,
) -> TokenStream {
let funcname = func.name.as_str();
@@ -161,27 +163,12 @@ pub fn define_func(
quote!()
};
let (placeholders, args): (Vec<_>, Vec<_>) = func
.params
.iter()
.map(|param| {
let name = names.func_param(&param.name);
let fmt = if passed_by_reference(&*param.tref.type_()) {
"{:?}"
} else {
"{}"
};
(format!("{}={}", name.to_string(), fmt), quote!(#name))
})
.unzip();
let trace_fmt = format!("{}({})", ident.to_string(), placeholders.join(","));
let log_args = logging.args(&func, names);
quote!(pub fn #ident(#abi_args) -> #abi_ret {
#(#marshal_args)*
#(#marshal_rets_pre)*
#[cfg(feature = "trace_log")]
{
log::trace!(#trace_fmt, #(#args),*);
}
#log_args
let #trait_bindings = match #trait_name::#ident(ctx, #(#trait_args),*) {
Ok(#trait_bindings) => { #trait_rets },
Err(e) => { #ret_err },
@@ -336,3 +323,51 @@ where
_ => write_val_to_ptr,
}
}
impl LoggingConf {
fn args(&self, func: &witx::InterfaceFunc, names: &Names) -> TokenStream {
match self {
Self::Log { cfg_feature } => {
let (placeholders, args): (Vec<_>, Vec<_>) = func
.params
.iter()
.map(|param| {
let name = names.func_param(&param.name);
let fmt = if passed_by_reference(&*param.tref.type_()) {
"{:?}"
} else {
"{}"
};
(format!("{}={}", name.to_string(), fmt), quote!(#name))
})
.unzip();
let trace_fmt = format!(
"{}({})",
names.func(&func.name).to_string(),
placeholders.join(",")
);
let trace_stmt = quote!(log::trace!(#trace_fmt, #(#args),*););
if let Some(feature) = cfg_feature {
quote! {
#[cfg(feature = #feature)]
{
#trace_stmt
}
}
} else {
trace_stmt
}
}
Self::Tracing => {
let args = func.params.iter().map(|param| {
let name = names.func_param(&param.name);
quote!( #name = #name )
});
let func_name = names.func(&func.name).to_string();
quote! {
tracing::debug!(function = #func_name, #(#args),*, "marshalled arguments");
}
}
}
}
}

View File

@@ -11,14 +11,19 @@ use quote::quote;
use lifetimes::anon_lifetime;
pub use config::Config;
pub use config::{Config, LoggingConf};
pub use error_transform::{ErrorTransform, UserErrorType};
pub use funcs::define_func;
pub use module_trait::define_module_trait;
pub use names::Names;
pub use types::define_datatype;
pub fn generate(doc: &witx::Document, names: &Names, errs: &ErrorTransform) -> TokenStream {
pub fn generate(
doc: &witx::Document,
names: &Names,
errs: &ErrorTransform,
logging: &LoggingConf,
) -> TokenStream {
// TODO at some point config should grow more ability to configure name
// overrides.
let rt = names.runtime_mod();
@@ -52,7 +57,7 @@ pub fn generate(doc: &witx::Document, names: &Names, errs: &ErrorTransform) -> T
let trait_name = names.trait_name(&module.name);
let fs = module
.funcs()
.map(|f| define_func(&names, &f, quote!(#trait_name), &errs));
.map(|f| define_func(&names, &f, quote!(#trait_name), &errs, logging));
let modtrait = define_module_trait(&names, &module, &errs);
let ctx_type = names.ctx_type();
quote!(