Use Tokio and simplelog by default

Replace ctrlc usage with Tokio and simplelog.

Signed-off-by: Michal Rostecki <mrostecki@opensuse.org>
pull/27/head
Michal Rostecki 3 years ago
parent 31a7d0edb4
commit de709c52d0

@ -8,9 +8,11 @@ publish = false
aya = { git = "https://github.com/aya-rs/aya", branch="main" } aya = { git = "https://github.com/aya-rs/aya", branch="main" }
{{project-name}}-common = { path = "../{{project-name}}-common", features=["user"] } {{project-name}}-common = { path = "../{{project-name}}-common", features=["user"] }
anyhow = "1.0.42" anyhow = "1.0.42"
ctrlc = "3.2"
{% if program_type == "uprobe" %}libc = "0.2.102"{% endif %} {% 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]] [[bin]]
name = "{{project-name}}" name = "{{project-name}}"

@ -29,20 +29,11 @@ use aya::{programs::Lsm, Btf};
{%- when "tp_btf" -%} {%- when "tp_btf" -%}
use aya::{programs::BtfTracePoint, Btf}; use aya::{programs::BtfTracePoint, Btf};
{%- endcase %} {%- endcase %}
use std::{ use log::info;
convert::TryInto, use simplelog::{ColorChoice, ConfigBuilder, LevelFilter, TermLogger, TerminalMode};
sync::Arc, use std::convert::TryInto;
sync::atomic::{AtomicBool, Ordering},
thread,
time::Duration,
};
use structopt::StructOpt; use structopt::StructOpt;
use tokio::signal;
fn main() {
if let Err(e) = try_main() {
eprintln!("error: {:#}", e);
}
}
#[derive(Debug, StructOpt)] #[derive(Debug, StructOpt)]
struct Opt { struct Opt {
@ -58,8 +49,20 @@ struct Opt {
{%- endif %} {%- endif %}
} }
fn try_main() -> Result<(), anyhow::Error> { #[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
let opt = Opt::from_args(); 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 // 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 // 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 // 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()?; program.attach()?;
{%- endcase %} {%- endcase %}
let running = Arc::new(AtomicBool::new(true)); info!("Waiting for Ctrl-C...");
let r = running.clone(); signal::ctrl_c().await?;
info!("Exiting...");
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...");
Ok(()) Ok(())
} }

Loading…
Cancel
Save