Switch logging to use tracing instead of log
Tracing has support for intervals and a great os_log integration.
This commit is contained in:
parent
60cfd95789
commit
e643d9dd41
14 changed files with 297 additions and 8 deletions
|
|
@ -7,9 +7,15 @@ edition = "2021"
|
|||
crate-type = ["lib", "staticlib"]
|
||||
|
||||
[dependencies]
|
||||
tokio = { version = "1.21", features = ["rt", "sync", "io-util", "macros"] }
|
||||
anyhow = "1.0"
|
||||
tokio = { version = "1.21", features = ["rt", "macros", "sync", "io-util"] }
|
||||
tun = { version = "0.1", path = "../tun", features = ["serde"] }
|
||||
clap = { version = "4.3.2", features = ["derive"] }
|
||||
tracing = "0.1"
|
||||
tracing-log = "0.1"
|
||||
tracing-journald = "0.3"
|
||||
tracing-oslog = {git = "https://github.com/Stormshield-robinc/tracing-oslog"}
|
||||
tracing-subscriber = "0.3"
|
||||
env_logger = "0.10"
|
||||
log = "0.4"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
|
|
|
|||
|
|
@ -1,5 +1,8 @@
|
|||
use tracing::instrument;
|
||||
|
||||
// Check capabilities on Linux
|
||||
#[cfg(target_os = "linux")]
|
||||
#[instrument]
|
||||
pub fn ensure_root() {
|
||||
use caps::{has_cap, CapSet, Capability};
|
||||
|
||||
|
|
@ -19,6 +22,7 @@ pub fn ensure_root() {
|
|||
|
||||
// Check for root user on macOS
|
||||
#[cfg(target_vendor = "apple")]
|
||||
#[instrument]
|
||||
pub fn ensure_root() {
|
||||
use nix::unistd::Uid;
|
||||
|
||||
|
|
@ -30,6 +34,7 @@ pub fn ensure_root() {
|
|||
}
|
||||
|
||||
#[cfg(target_family = "windows")]
|
||||
#[instrument]
|
||||
pub fn ensure_root() {
|
||||
todo!()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
#![deny(missing_debug_implementations)]
|
||||
pub mod ensureroot;
|
||||
|
||||
#[cfg(any(target_os = "linux", target_vendor = "apple"))]
|
||||
|
|
|
|||
|
|
@ -1,8 +1,14 @@
|
|||
use anyhow::Context;
|
||||
use std::mem;
|
||||
#[cfg(any(target_os = "linux", target_vendor = "apple"))]
|
||||
use std::os::fd::FromRawFd;
|
||||
|
||||
use clap::{Args, Parser, Subcommand};
|
||||
use tracing::instrument;
|
||||
|
||||
use tracing_log::LogTracer;
|
||||
use tracing_oslog::OsLogger;
|
||||
use tracing_subscriber::{prelude::*, FmtSubscriber};
|
||||
use tokio::io::Result;
|
||||
#[cfg(any(target_os = "linux", target_vendor = "apple"))]
|
||||
use burrow::retrieve;
|
||||
|
|
@ -58,10 +64,21 @@ async fn try_start() -> Result<()> {
|
|||
}
|
||||
|
||||
#[cfg(any(target_os = "linux", target_vendor = "apple"))]
|
||||
#[instrument]
|
||||
async fn try_retrieve() -> Result<()> {
|
||||
LogTracer::init().context("Failed to initialize LogTracer").unwrap();
|
||||
|
||||
if cfg!(target_os = "linux") || cfg!(target_vendor = "apple") {
|
||||
let maybe_layer = system_log().unwrap();
|
||||
if let Some(layer) = maybe_layer {
|
||||
let logger = layer.with_subscriber(FmtSubscriber::new());
|
||||
tracing::subscriber::set_global_default(logger).context("Failed to set the global tracing subscriber").unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
burrow::ensureroot::ensure_root();
|
||||
let iface2 = retrieve();
|
||||
println!("{}", iface2);
|
||||
tracing::info!("{}", iface2);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
@ -89,17 +106,17 @@ async fn try_stop() -> Result<()> {
|
|||
|
||||
#[tokio::main(flavor = "current_thread")]
|
||||
async fn main() -> Result<()> {
|
||||
println!("Platform: {}", std::env::consts::OS);
|
||||
tracing::info!("Platform: {}", std::env::consts::OS);
|
||||
|
||||
let cli = Cli::parse();
|
||||
match &cli.command {
|
||||
Commands::Start(..) => {
|
||||
try_start().await.unwrap();
|
||||
println!("FINISHED");
|
||||
tracing::info!("FINISHED");
|
||||
}
|
||||
Commands::Retrieve(..) => {
|
||||
try_retrieve().await.unwrap();
|
||||
println!("FINISHED");
|
||||
tracing::info!("FINISHED");
|
||||
}
|
||||
Commands::Stop => {
|
||||
try_stop().await.unwrap();
|
||||
|
|
@ -109,3 +126,20 @@ async fn main() -> Result<()> {
|
|||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
fn system_log() -> anyhow::Result<Option<tracing_journald::Layer>> {
|
||||
let maybe_journald = tracing_journald::layer();
|
||||
match maybe_journald {
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
|
||||
tracing::trace!("journald not found");
|
||||
Ok(None)
|
||||
},
|
||||
_ => Ok(Some(maybe_journald?))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_vendor = "apple")]
|
||||
fn system_log() -> anyhow::Result<Option<OsLogger>> {
|
||||
Ok(Some(OsLogger::new("com.hackclub.burrow", "default")))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue