|
|
@ -13,21 +13,23 @@
|
|
|
|
//!
|
|
|
|
//!
|
|
|
|
//! ```no_run
|
|
|
|
//! ```no_run
|
|
|
|
//! # let mut bpf = aya::Bpf::load(&[], None)?;
|
|
|
|
//! # let mut bpf = aya::Bpf::load(&[], None)?;
|
|
|
|
//! use aya_log::BpfLogger;
|
|
|
|
|
|
|
|
//! use simplelog::{ColorChoice, ConfigBuilder, LevelFilter, TermLogger, TerminalMode};
|
|
|
|
//! use simplelog::{ColorChoice, ConfigBuilder, LevelFilter, TermLogger, TerminalMode};
|
|
|
|
|
|
|
|
//! use aya_log::BpfLogger;
|
|
|
|
//!
|
|
|
|
//!
|
|
|
|
//! BpfLogger::init(
|
|
|
|
//! // initialize simplelog::TermLogger as the default logger
|
|
|
|
//! &mut bpf,
|
|
|
|
//! TermLogger::init(
|
|
|
|
//! TermLogger::new(
|
|
|
|
//! LevelFilter::Debug,
|
|
|
|
//! LevelFilter::Trace,
|
|
|
|
|
|
|
|
//! ConfigBuilder::new()
|
|
|
|
//! ConfigBuilder::new()
|
|
|
|
//! .set_target_level(LevelFilter::Error)
|
|
|
|
//! .set_target_level(LevelFilter::Error)
|
|
|
|
//! .set_location_level(LevelFilter::Error)
|
|
|
|
//! .set_location_level(LevelFilter::Error)
|
|
|
|
//! .build(),
|
|
|
|
//! .build(),
|
|
|
|
//! TerminalMode::Mixed,
|
|
|
|
//! TerminalMode::Mixed,
|
|
|
|
//! ColorChoice::Auto,
|
|
|
|
//! ColorChoice::Auto,
|
|
|
|
//! ),
|
|
|
|
//! )
|
|
|
|
//! ).unwrap();
|
|
|
|
//! .unwrap();
|
|
|
|
|
|
|
|
//!
|
|
|
|
|
|
|
|
//! // start reading aya-log records and log them using the default logger
|
|
|
|
|
|
|
|
//! BpfLogger::init(&mut bpf).unwrap();
|
|
|
|
//! ```
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//!
|
|
|
|
//! With the following eBPF code:
|
|
|
|
//! 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 aya_log_common::{RecordField, LOG_BUF_CAPACITY, LOG_FIELDS};
|
|
|
|
use bytes::BytesMut;
|
|
|
|
use bytes::BytesMut;
|
|
|
|
use log::{Level, Log, Record};
|
|
|
|
use log::{logger, Level, Log, Record};
|
|
|
|
use thiserror::Error;
|
|
|
|
use thiserror::Error;
|
|
|
|
|
|
|
|
|
|
|
|
use aya::{
|
|
|
|
use aya::{
|
|
|
@ -79,9 +81,18 @@ use aya::{
|
|
|
|
pub struct BpfLogger;
|
|
|
|
pub struct BpfLogger;
|
|
|
|
|
|
|
|
|
|
|
|
impl 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, Error> {
|
|
|
|
|
|
|
|
BpfLogger::init_with_logger(bpf, DefaultLogger {})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Starts reading log records created with `aya-log-ebpf` and logs them
|
|
|
|
/// Starts reading log records created with `aya-log-ebpf` and logs them
|
|
|
|
/// with the given logger.
|
|
|
|
/// with the given logger.
|
|
|
|
pub fn init<T: Log + 'static>(bpf: &mut Bpf, logger: T) -> Result<BpfLogger, Error> {
|
|
|
|
pub fn init_with_logger<T: Log + 'static>(
|
|
|
|
|
|
|
|
bpf: &mut Bpf,
|
|
|
|
|
|
|
|
logger: T,
|
|
|
|
|
|
|
|
) -> Result<BpfLogger, Error> {
|
|
|
|
let logger = Arc::new(logger);
|
|
|
|
let logger = Arc::new(logger);
|
|
|
|
let mut logs: AsyncPerfEventArray<_> = bpf.map_mut("AYA_LOGS")?.try_into()?;
|
|
|
|
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)]
|
|
|
|
#[derive(Error, Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
pub enum Error {
|
|
|
|
#[error("error opening log event array")]
|
|
|
|
#[error("error opening log event array")]
|
|
|
|