From de709c52d0349924b65baa8cc50719162ca68801 Mon Sep 17 00:00:00 2001 From: Michal Rostecki Date: Sun, 16 Jan 2022 11:09:37 +0100 Subject: [PATCH] Use Tokio and simplelog by default Replace ctrlc usage with Tokio and simplelog. Signed-off-by: Michal Rostecki --- {{project-name}}/Cargo.toml | 6 +++-- {{project-name}}/src/main.rs | 46 ++++++++++++++++-------------------- 2 files changed, 24 insertions(+), 28 deletions(-) diff --git a/{{project-name}}/Cargo.toml b/{{project-name}}/Cargo.toml index c91a00e..a10a7c0 100644 --- a/{{project-name}}/Cargo.toml +++ b/{{project-name}}/Cargo.toml @@ -8,9 +8,11 @@ publish = false aya = { git = "https://github.com/aya-rs/aya", branch="main" } {{project-name}}-common = { path = "../{{project-name}}-common", features=["user"] } anyhow = "1.0.42" -ctrlc = "3.2" {% if program_type == "uprobe" %}libc = "0.2.102"{% endif %} -structopt = { version = "0.3"} +log = "0.4" +simplelog = "0.11" +structopt = { version = "0.3" } +tokio = { version = "1.5.0", features = ["macros", "rt", "rt-multi-thread", "net", "signal"] } [[bin]] name = "{{project-name}}" diff --git a/{{project-name}}/src/main.rs b/{{project-name}}/src/main.rs index 342cdc6..06c8641 100644 --- a/{{project-name}}/src/main.rs +++ b/{{project-name}}/src/main.rs @@ -29,20 +29,11 @@ use aya::{programs::Lsm, Btf}; {%- when "tp_btf" -%} use aya::{programs::BtfTracePoint, Btf}; {%- endcase %} -use std::{ - convert::TryInto, - sync::Arc, - sync::atomic::{AtomicBool, Ordering}, - thread, - time::Duration, -}; +use log::info; +use simplelog::{ColorChoice, ConfigBuilder, LevelFilter, TermLogger, TerminalMode}; +use std::convert::TryInto; use structopt::StructOpt; - -fn main() { - if let Err(e) = try_main() { - eprintln!("error: {:#}", e); - } -} +use tokio::signal; #[derive(Debug, StructOpt)] struct Opt { @@ -58,8 +49,20 @@ struct Opt { {%- endif %} } -fn try_main() -> Result<(), anyhow::Error> { +#[tokio::main] +async fn main() -> Result<(), anyhow::Error> { let opt = Opt::from_args(); + + TermLogger::init( + LevelFilter::Debug, + ConfigBuilder::new() + .set_target_level(LevelFilter::Error) + .set_location_level(LevelFilter::Error) + .build(), + TerminalMode::Mixed, + ColorChoice::Auto, + )?; + // This will include youe eBPF object file as raw bytes at compile-time and load it at // runtime. This approach is recommended for most real-world use cases. If you would // like to specify the eBPF program at runtime rather than at compile-time, you can @@ -133,18 +136,9 @@ fn try_main() -> Result<(), anyhow::Error> { program.attach()?; {%- endcase %} - let running = Arc::new(AtomicBool::new(true)); - let r = running.clone(); - - ctrlc::set_handler(move || { - r.store(false, Ordering::SeqCst); - }).expect("Error setting Ctrl-C handler"); - - println!("Waiting for Ctrl-C..."); - while running.load(Ordering::SeqCst) { - thread::sleep(Duration::from_millis(500)) - } - println!("Exiting..."); + info!("Waiting for Ctrl-C..."); + signal::ctrl_c().await?; + info!("Exiting..."); Ok(()) }