aya-log-ebpf: use destructuring

reviewable/pr1288/r13
Tamir Duberstein 2 days ago
parent 90c2165231
commit 600927d6fc
No known key found for this signature in database

@ -69,22 +69,27 @@ impl Parse for LogArgs {
} }
pub(crate) fn log(args: LogArgs, level: Option<TokenStream>) -> Result<TokenStream> { pub(crate) fn log(args: LogArgs, level: Option<TokenStream>) -> Result<TokenStream> {
let ctx = args.ctx; let LogArgs {
let target = match args.target { ctx,
target,
level: level_expr,
format_string,
formatting_args,
} = args;
let target = match target {
Some(t) => quote! { #t }, Some(t) => quote! { #t },
None => quote! { module_path!() }, None => quote! { module_path!() },
}; };
let lvl: TokenStream = if let Some(l) = level { let level = match level {
l Some(l) => l,
} else if let Some(l) = args.level { None => {
quote! { #l } let l = level_expr.ok_or(Error::new(
} else { format_string.span(),
return Err(Error::new( "missing `level` argument: try passing an `aya_log_ebpf::Level` value",
args.format_string.span(), ))?;
"missing `level` argument: try passing an `aya_log_ebpf::Level` value", quote! { #l }
)); }
}; };
let format_string = args.format_string;
let format_string_val = format_string.value(); let format_string_val = format_string.value();
let fragments = parse(&format_string_val).map_err(|e| { let fragments = parse(&format_string_val).map_err(|e| {
@ -101,7 +106,7 @@ pub(crate) fn log(args: LogArgs, level: Option<TokenStream>) -> Result<TokenStre
match fragment { match fragment {
Fragment::Literal(s) => values.push(quote!(#s)), Fragment::Literal(s) => values.push(quote!(#s)),
Fragment::Parameter(p) => { Fragment::Parameter(p) => {
let arg = match args.formatting_args { let arg = match formatting_args {
Some(ref args) => args[arg_i].clone(), Some(ref args) => args[arg_i].clone(),
None => return Err(Error::new(format_string.span(), "no arguments provided")), None => return Err(Error::new(format_string.span(), "no arguments provided")),
}; };
@ -153,7 +158,7 @@ pub(crate) fn log(args: LogArgs, level: Option<TokenStream>) -> Result<TokenStre
let #size = ::aya_log_ebpf::write_record_header( let #size = ::aya_log_ebpf::write_record_header(
#buf, #buf,
#target, #target,
#lvl, #level,
module_path!(), module_path!(),
file!(), file!(),
line!(), line!(),

Loading…
Cancel
Save