aya-log-ebpf: tidy up `macro_support`

Move top level items into and remove unused items from `macro_support`.
reviewable/pr1288/r6
Tamir Duberstein 3 weeks ago
parent 517e05118a
commit 251623e600
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> {
let ctx = args.ctx;
let target = match args.target {
let LogArgs {
ctx,
target,
level: level_expr,
format_string,
formatting_args,
} = args;
let target = match target {
Some(t) => quote! { #t },
None => quote! { module_path!() },
};
let lvl: TokenStream = if let Some(l) = level {
l
} else if let Some(l) = args.level {
quote! { #l }
} else {
return Err(Error::new(
args.format_string.span(),
let level = match level {
Some(l) => l,
None => {
let l = level_expr.ok_or(Error::new(
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 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 {
Fragment::Literal(s) => values.push(quote!(#s)),
Fragment::Parameter(p) => {
let arg = match args.formatting_args {
let arg = match formatting_args {
Some(ref args) => args[arg_i].clone(),
None => return Err(Error::new(format_string.span(), "no arguments provided")),
};
@ -146,14 +151,14 @@ pub(crate) fn log(args: LogArgs, level: Option<TokenStream>) -> Result<TokenStre
let len = Ident::new("len", Span::mixed_site());
let record = Ident::new("record", Span::mixed_site());
Ok(quote! {
match ::aya_log_ebpf::AYA_LOG_BUF.get_ptr_mut(0).and_then(|ptr| unsafe { ptr.as_mut() }) {
match ::aya_log_ebpf::macro_support::AYA_LOG_BUF.get_ptr_mut(0).and_then(|ptr| unsafe { ptr.as_mut() }) {
None => {},
Some(::aya_log_ebpf::LogBuf { buf: #buf }) => {
Some(::aya_log_ebpf::macro_support::LogBuf { buf: #buf }) => {
let _: Option<()> = (|| {
let #size = ::aya_log_ebpf::write_record_header(
let #size = ::aya_log_ebpf::macro_support::write_record_header(
#buf,
#target,
#lvl,
#level,
module_path!(),
file!(),
line!(),
@ -163,12 +168,12 @@ pub(crate) fn log(args: LogArgs, level: Option<TokenStream>) -> Result<TokenStre
#(
{
let #buf = #buf.get_mut(#size..)?;
let #len = ::aya_log_ebpf::WriteToBuf::write(#values_iter, #buf)?;
let #len = ::aya_log_ebpf::macro_support::WriteToBuf::write(#values_iter, #buf)?;
#size += #len.get();
}
)*
let #record = #buf.get(..#size)?;
::aya_log_ebpf::AYA_LOGS.output(#ctx, #record, 0);
::aya_log_ebpf::macro_support::AYA_LOGS.output(#ctx, #record, 0);
Some(())
})();
}

@ -1,39 +1,35 @@
#![no_std]
#![warn(clippy::cast_lossless, clippy::cast_sign_loss)]
#[cfg(target_arch = "bpf")]
use aya_ebpf::macros::map;
use aya_ebpf::maps::{PerCpuArray, PerfEventByteArray};
pub use aya_log_common::{LOG_BUF_CAPACITY, Level, WriteToBuf, write_record_header};
pub use aya_log_ebpf_macros::{debug, error, info, log, trace, warn};
#[doc(hidden)]
#[repr(C)]
pub struct LogBuf {
pub buf: [u8; LOG_BUF_CAPACITY],
}
#[doc(hidden)]
// This cfg_attr prevents compilation failures on macOS where the generated section name doesn't
// meet mach-o's requirements. We wouldn't ordinarily build this crate for macOS, but we do so
// because the integration-test crate depends on this crate transitively. See comment in
// test/integration-test/Cargo.toml.
#[cfg_attr(target_arch = "bpf", map)]
pub static AYA_LOG_BUF: PerCpuArray<LogBuf> = PerCpuArray::with_max_entries(1, 0);
#[doc(hidden)]
// This cfg_attr prevents compilation failures on macOS where the generated section name doesn't
// meet mach-o's requirements. We wouldn't ordinarily build this crate for macOS, but we do so
// because the integration-test crate depends on this crate transitively. See comment in
// test/integration-test/Cargo.toml.
#[cfg_attr(target_arch = "bpf", map)]
pub static AYA_LOGS: PerfEventByteArray = PerfEventByteArray::new(0);
#[doc(hidden)]
pub mod macro_support {
#[cfg(target_arch = "bpf")]
use aya_ebpf::macros::map;
use aya_ebpf::maps::{PerCpuArray, PerfEventByteArray};
use aya_log_common::LOG_BUF_CAPACITY;
pub use aya_log_common::{
DefaultFormatter, DisplayHint, IpFormatter, LOG_BUF_CAPACITY, Level, LowerHexFormatter,
LowerMacFormatter, UpperHexFormatter, UpperMacFormatter,
DefaultFormatter, DisplayHint, IpFormatter, Level, LowerHexFormatter, LowerMacFormatter,
UpperHexFormatter, UpperMacFormatter, WriteToBuf, write_record_header,
};
pub use aya_log_ebpf_macros::log;
#[repr(C)]
pub struct LogBuf {
pub buf: [u8; LOG_BUF_CAPACITY],
}
// This cfg_attr prevents compilation failures on macOS where the generated section name doesn't
// meet mach-o's requirements. We wouldn't ordinarily build this crate for macOS, but we do so
// because the integration-test crate depends on this crate transitively. See comment in
// test/integration-test/Cargo.toml.
#[cfg_attr(target_arch = "bpf", map)]
pub static AYA_LOG_BUF: PerCpuArray<LogBuf> = PerCpuArray::with_max_entries(1, 0);
// This cfg_attr prevents compilation failures on macOS where the generated section name doesn't
// meet mach-o's requirements. We wouldn't ordinarily build this crate for macOS, but we do so
// because the integration-test crate depends on this crate transitively. See comment in
// test/integration-test/Cargo.toml.
#[cfg_attr(target_arch = "bpf", map)]
pub static AYA_LOGS: PerfEventByteArray = PerfEventByteArray::new(0);
}

@ -1,7 +1,4 @@
pub mod aya_log_ebpf
pub use aya_log_ebpf::LOG_BUF_CAPACITY
pub use aya_log_ebpf::Level
pub use aya_log_ebpf::WriteToBuf
pub use aya_log_ebpf::debug
pub use aya_log_ebpf::error
pub use aya_log_ebpf::info

Loading…
Cancel
Save