From 9ab9c80183edcb23297a644d0e63f7c1f28cd968 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alessandro=C2=A0Decina?= Date: Fri, 15 Oct 2021 09:03:12 +0000 Subject: [PATCH] Simplify BpfLogger::init Make BpfLogger::init(bpf) log using the default logger. Add BpfLoger::init_with_logger(bpf, logger) for logging using a custom logger instance. --- aya-log/README.md | 27 +++++++++--------- aya-log/aya-log/src/lib.rs | 58 ++++++++++++++++++++++++++++---------- 2 files changed, 57 insertions(+), 28 deletions(-) diff --git a/aya-log/README.md b/aya-log/README.md index 0cb3b167..5e1245ca 100644 --- a/aya-log/README.md +++ b/aya-log/README.md @@ -33,21 +33,22 @@ to log eBPF messages to the terminal. ### User space code ```rust -use aya_log::BpfLogger; use simplelog::{ColorChoice, ConfigBuilder, LevelFilter, TermLogger, TerminalMode}; +use aya_log::BpfLogger; -BpfLogger::init( - &mut bpf, - TermLogger::new( - LevelFilter::Trace, - ConfigBuilder::new() - .set_target_level(LevelFilter::Error) - .set_location_level(LevelFilter::Error) - .build(), - TerminalMode::Mixed, - ColorChoice::Auto, - ), -).unwrap(); +TermLogger::init( + LevelFilter::Debug, + ConfigBuilder::new() + .set_target_level(LevelFilter::Error) + .set_location_level(LevelFilter::Error) + .build(), + TerminalMode::Mixed, + ColorChoice::Auto, +) +.unwrap(); + +// Will log using the default logger, which is TermLogger in this case +BpfLogger::init(&mut bpf).unwrap(); ``` ### eBPF code diff --git a/aya-log/aya-log/src/lib.rs b/aya-log/aya-log/src/lib.rs index 838fe5a5..d50fd860 100644 --- a/aya-log/aya-log/src/lib.rs +++ b/aya-log/aya-log/src/lib.rs @@ -13,21 +13,23 @@ //! //! ```no_run //! # let mut bpf = aya::Bpf::load(&[], None)?; -//! use aya_log::BpfLogger; //! use simplelog::{ColorChoice, ConfigBuilder, LevelFilter, TermLogger, TerminalMode}; +//! use aya_log::BpfLogger; //! -//! BpfLogger::init( -//! &mut bpf, -//! TermLogger::new( -//! LevelFilter::Trace, -//! ConfigBuilder::new() -//! .set_target_level(LevelFilter::Error) -//! .set_location_level(LevelFilter::Error) -//! .build(), -//! TerminalMode::Mixed, -//! ColorChoice::Auto, -//! ), -//! ).unwrap(); +//! // initialize simplelog::TermLogger as the default logger +//! TermLogger::init( +//! LevelFilter::Debug, +//! ConfigBuilder::new() +//! .set_target_level(LevelFilter::Error) +//! .set_location_level(LevelFilter::Error) +//! .build(), +//! TerminalMode::Mixed, +//! ColorChoice::Auto, +//! ) +//! .unwrap(); +//! +//! // start reading aya-log records and log them using the default logger +//! BpfLogger::init(&mut bpf).unwrap(); //! ``` //! //! With the following eBPF code: @@ -61,7 +63,7 @@ use std::{convert::TryInto, io, mem, ptr, sync::Arc}; use aya_log_common::{RecordField, LOG_BUF_CAPACITY, LOG_FIELDS}; use bytes::BytesMut; -use log::{Level, Log, Record}; +use log::{logger, Level, Log, Record}; use thiserror::Error; use aya::{ @@ -79,9 +81,18 @@ use aya::{ pub struct BpfLogger; impl BpfLogger { + /// Starts reading log records created with `aya-log-ebpf` and logs them + /// with the default logger. See [log::logger]. + pub fn init(bpf: &mut Bpf) -> Result { + BpfLogger::init_with_logger(bpf, DefaultLogger {}) + } + /// Starts reading log records created with `aya-log-ebpf` and logs them /// with the given logger. - pub fn init(bpf: &mut Bpf, logger: T) -> Result { + pub fn init_with_logger( + bpf: &mut Bpf, + logger: T, + ) -> Result { let logger = Arc::new(logger); let mut logs: AsyncPerfEventArray<_> = bpf.map_mut("AYA_LOGS")?.try_into()?; @@ -110,6 +121,23 @@ impl BpfLogger { } } +#[derive(Copy, Clone, Debug)] +struct DefaultLogger; + +impl Log for DefaultLogger { + fn enabled(&self, metadata: &log::Metadata) -> bool { + log::logger().enabled(metadata) + } + + fn log(&self, record: &Record) { + log::logger().log(record) + } + + fn flush(&self) { + log::logger().flush() + } +} + #[derive(Error, Debug)] pub enum Error { #[error("error opening log event array")]