Skip to content

Commit

Permalink
feat: Support syslog
Browse files Browse the repository at this point in the history
Fixes #2.
  • Loading branch information
lu-zero committed Dec 31, 2024
1 parent a1d6eab commit fc1f477
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 22 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ version = "0.2.0"
edition = "2021"
description = "mini-udev workalike"

[features]
default = ["syslog"]
syslog = ["dep:syslog-tracing"]

[dependencies]
anyhow = "1.0.95"
bytes = "1.9.0"
Expand All @@ -14,6 +18,7 @@ kobject-uevent = "0.2.0"
mdev-parser = "0.1.1"
netlink-sys = { version = "0.8.7", features = ["tokio_socket"] }
nix = { version = "0.29.0", features = ["user", "fs"] }
syslog-tracing = { version = "0.3.1", optional = true }
thiserror = "2.0.9"
tokio = { version = "1.42.0", features = [
"macros",
Expand Down
8 changes: 5 additions & 3 deletions src/bin/mdev-coldplug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ struct Opt {
}

impl Opt {
fn setup_log(&self) -> anyhow::Result<()> {
setup_log(self.verbose)
fn setup_log(&self) {
use tracing_subscriber::{fmt, prelude::*};
let fmt_layer = fmt::layer().with_target(false);
setup_log(self.verbose).with(fmt_layer).init();
}
}

fn main() -> anyhow::Result<()> {
let opt = Opt::parse();

opt.setup_log()?;
opt.setup_log();

let classdir = WalkDir::new(opt.sysfs_mount.join("class"))
.follow_links(true)
Expand Down
35 changes: 28 additions & 7 deletions src/bin/mdev.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
collections::HashMap,
ffi::OsStr,
ffi::{CString, OsStr},
path::{Path, PathBuf},
};

Expand All @@ -13,6 +13,7 @@ use nix::{
sys::stat::{makedev, mknod, Mode, SFlag},
unistd::{chown, unlink},
};
use syslog_tracing::Syslog;
use tokio::{fs, join};
use tracing::{debug, info, warn};
use walkdir::WalkDir;
Expand Down Expand Up @@ -253,16 +254,36 @@ impl Opt {
Ok(())
}

fn setup_log(&self) -> anyhow::Result<()> {
fn setup_log(&self) {
use tracing_subscriber::{fmt, prelude::*};
if self.daemon && !self.foreground && !self.syslog {
return Ok(());
return;
}

let registry = setup_log(self.verbose);
let fmt_layer = fmt::layer().with_target(false);

if self.syslog {
todo!("Wire in syslog somehow");
// SAFETY: They are strings that do not contain a null byte
let identity = std::env::args()
.next()
.map_or_else(|| CString::new("mdev"), |name| CString::new(name))
.unwrap();
let syslog = Syslog::new(
identity,
syslog_tracing::Options::LOG_PID,
syslog_tracing::Facility::Daemon,
)
.unwrap();
let fmt_layer = fmt_layer
.with_level(false)
.without_time()
.with_writer(syslog);

registry.with(fmt_layer).init();
} else {
registry.with(fmt_layer).init();
}

setup_log(self.verbose)
}
}

Expand All @@ -283,7 +304,7 @@ fn main() -> anyhow::Result<()> {

let opt = Opt::parse();

opt.setup_log()?;
opt.setup_log();

if opt.scan {
opt.run_scan(&conf)?;
Expand Down
15 changes: 3 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use futures_util::ready;
use kobject_uevent::UEvent;
use netlink_sys::{AsyncSocket, SocketAddr, TokioSocket};
use tokio::sync::mpsc;
use tracing_subscriber::{layer::Layered, prelude::*, EnvFilter, Registry};

pub mod rule;
pub mod stream;
Expand Down Expand Up @@ -196,12 +197,7 @@ mod tests {
}
}

pub fn setup_log(verbose: u8) -> anyhow::Result<()> {
use tracing_subscriber::prelude::*;
use tracing_subscriber::{fmt, EnvFilter};

let fmt_layer = fmt::layer().with_target(false);

pub fn setup_log(verbose: u8) -> Layered<EnvFilter, Registry> {
let filter_layer = EnvFilter::try_from_default_env().unwrap_or_else(|_| {
if verbose < 1 {
EnvFilter::new("info")
Expand All @@ -212,10 +208,5 @@ pub fn setup_log(verbose: u8) -> anyhow::Result<()> {
}
});

tracing_subscriber::registry()
.with(filter_layer)
.with(fmt_layer)
.init();

Ok(())
tracing_subscriber::registry().with(filter_layer)
}

0 comments on commit fc1f477

Please sign in to comment.