Skip to content

Commit

Permalink
feat: Switch from log to tracing
Browse files Browse the repository at this point in the history
This simplifies the code to just use one logging framework
  • Loading branch information
VorpalBlade committed Aug 16, 2024
1 parent 8ae57f4 commit 1f5394d
Show file tree
Hide file tree
Showing 14 changed files with 83 additions and 97 deletions.
37 changes: 5 additions & 32 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ directories = "5.0.1"
dirs = "5.0.1"
duct = "0.13.7"
either = "1.13.0"
env_logger = "0.11.5"
faster-hex = { version = "0.9.0", default-features = false, features = [
"std",
] }
Expand All @@ -47,7 +46,6 @@ lasso = { version = "0.7.2", features = [
"multi-threaded",
] }
libc = "0.2.155"
log = "0.4.22"
md-5 = "0.10.6"
memchr = "2.7.4"
mimalloc = "0.1.43"
Expand Down
3 changes: 2 additions & 1 deletion crates/paketkoll/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ ahash.workspace = true
anyhow.workspace = true
clap.workspace = true
compact_str.workspace = true
env_logger.workspace = true
os_info.workspace = true
paketkoll_core = { version = "0.5.3", path = "../paketkoll_core" }
paketkoll_types = { version = "0.1.3", path = "../paketkoll_types" }
proc-exit.workspace = true
rayon.workspace = true
serde = { workspace = true, optional = true }
serde_json = { workspace = true, optional = true }
tracing-subscriber.workspace = true
tracing.workspace = true

[target.'cfg(target_env = "musl")'.dependencies]
# The allocator on musl is attrociously slow, so we use a custom one.
Expand Down
21 changes: 14 additions & 7 deletions crates/paketkoll/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ use clap::Parser;
use proc_exit::Code;
use proc_exit::Exit;
use rayon::prelude::*;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;

#[cfg(target_env = "musl")]
use mimalloc::MiMalloc;
use paketkoll::cli::Cli;
use paketkoll::cli::Commands;
use paketkoll::cli::Format;
Expand All @@ -29,13 +29,20 @@ use paketkoll_types::backend::OriginalFileQuery;
use paketkoll_types::package::PackageInterned;

#[cfg(target_env = "musl")]
#[cfg_attr(target_env = "musl", global_allocator)]
static GLOBAL: MiMalloc = MiMalloc;
mod _musl {
use mimalloc::MiMalloc;
#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;
}

fn main() -> anyhow::Result<Exit> {
let mut builder =
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("warn"));
builder.init();
let filter = tracing_subscriber::EnvFilter::builder()
.with_default_directive(tracing::level_filters::LevelFilter::INFO.into())
.from_env()?;
tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer())
.with(filter)
.init();
let cli = Cli::parse();

match cli.command {
Expand Down
2 changes: 1 addition & 1 deletion crates/paketkoll_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ flume.workspace = true
glob.workspace = true
ignore.workspace = true
libc.workspace = true
log.workspace = true
md-5 = { workspace = true, optional = true }
mtree2 = { version = "0.6.4", path = "../mtree2", optional = true }
nix = { workspace = true, features = ["user"], optional = true }
Expand All @@ -93,6 +92,7 @@ smallvec.workspace = true
strum.workspace = true
systemd_tmpfiles = { version = "0.1.5", path = "../systemd_tmpfiles", optional = true }
tar.workspace = true
tracing.workspace = true
xz2 = { workspace = true, optional = true }
zstd = { workspace = true, optional = true }

Expand Down
14 changes: 7 additions & 7 deletions crates/paketkoll_core/src/backend/arch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub(crate) struct ArchLinuxBuilder {
impl ArchLinuxBuilder {
/// Load pacman config
fn load_config(&mut self) -> anyhow::Result<pacman_conf::PacmanConfig> {
log::debug!("Loading pacman config");
tracing::debug!("Loading pacman config");
let mut readable = BufReader::new(std::fs::File::open("/etc/pacman.conf")?);
let pacman_config: pacman_conf::PacmanConfig =
pacman_conf::PacmanConfig::new(&mut readable)?;
Expand Down Expand Up @@ -113,11 +113,11 @@ impl Files for ArchLinux {
let db_path: &Path = Path::new(&self.pacman_config.db_path);

// Load packages
log::debug!("Loading packages");
tracing::debug!("Loading packages");
let pkgs_and_paths = get_mtree_paths(db_path, interner, self.package_filter)?;

// Load mtrees
log::debug!("Loading mtrees");
tracing::debug!("Loading mtrees");
// Directories are duplicated across packages, we deduplicate them here
let seen_directories = DashSet::new();
// It is counter-intuitive, but we are faster if we collect into a vec here and
Expand Down Expand Up @@ -176,7 +176,7 @@ impl Files for ArchLinux {
.for_each(|entry| {
if let Ok(entry) = entry {
if let Err(e) = find_files(&entry, interner, &re, paths, &file_to_package) {
log::error!("Failed to parse package data: {e}");
tracing::error!("Failed to parse package data: {e}");
}
}
});
Expand Down Expand Up @@ -232,13 +232,13 @@ impl Files for ArchLinux {
package_map: &PackageMap,
interner: &Interner,
) -> Result<Vec<ArchiveResult>, PackageManagerError> {
log::info!(
tracing::info!(
"Finding archives for {} packages (may take a while)",
filter.len()
);
let archives = self.iterate_pkg_archives(filter, package_map, interner);

log::info!(
tracing::info!(
"Loading files from {} archives (may take a while)",
filter.len()
);
Expand Down Expand Up @@ -477,7 +477,7 @@ fn download_arch_pkg(pkg: &str) -> Result<(), anyhow::Error> {
.args(["-Sw", "--noconfirm", pkg])
.status()?;
if !status.success() {
log::warn!("Failed to download package for {pkg}");
tracing::warn!("Failed to download package for {pkg}");
};
Ok(())
}
Expand Down
36 changes: 18 additions & 18 deletions crates/paketkoll_core/src/backend/deb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,30 +118,30 @@ impl Name for Debian {

impl Files for Debian {
fn files(&self, interner: &Interner) -> anyhow::Result<Vec<FileEntry>> {
log::debug!("Loading packages");
tracing::debug!("Loading packages");
let packages_files: Vec<_> = get_package_files(interner)?.collect();

// Handle diversions: (parse output of dpkg-divert --list)
log::debug!("Loading diversions");
tracing::debug!("Loading diversions");
let diversions =
divert::get_diversions(interner).context("Failed to get dpkg diversions")?;

// Load config files.
log::debug!("Loading status to get config files");
tracing::debug!("Loading status to get config files");
let (config_files, _) = {
let mut status = BufReader::new(File::open(STATUS_PATH)?);
parsers::parse_status(interner, &mut status, self.primary_architecture)
}
.context(format!("Failed to parse {}", STATUS_PATH))?;

log::debug!("Merging packages files into one map");
tracing::debug!("Merging packages files into one map");
let merged = DashMap::with_hasher(ahash::RandomState::new());
packages_files.into_par_iter().for_each(|files| {
merge_deb_fileentries(&merged, files, &diversions);
});

// The config files must be merged into the results
log::debug!("Merging config files");
tracing::debug!("Merging config files");
merge_deb_fileentries(&merged, config_files, &diversions);

// For Debian we apply the filter here at the end, since multiple steps
Expand Down Expand Up @@ -194,7 +194,7 @@ impl Files for Debian {
if let Err(e) =
is_file_match(&entry.path(), interner, &re, paths, &file_to_package)
{
log::error!("Failed to parse package data: {e}");
tracing::error!("Failed to parse package data: {e}");
}
}
}
Expand Down Expand Up @@ -264,18 +264,18 @@ impl Files for Debian {
interner: &Interner,
) -> Result<Vec<ArchiveResult>, PackageManagerError> {
// Handle diversions: (parse output of dpkg-divert --list)
log::debug!("Loading diversions");
tracing::debug!("Loading diversions");
let diversions =
divert::get_diversions(interner).context("Failed to get dpkg diversions")?;

log::debug!("List of diversions: {diversions:?}");
tracing::debug!("List of diversions: {diversions:?}");

log::info!(
tracing::info!(
"Loading file data from dpkg cache archives for {} packages",
filter.len()
);
let archives = self.iterate_deb_archives(filter, package_map, interner)?;
log::info!(
tracing::info!(
"Got list of {} archives, starting extracting information (this may take a while, \
especially on the first run before the disk cache can help)",
filter.len()
Expand All @@ -291,7 +291,7 @@ impl Files for Debian {
})
})
.collect();
log::info!("Extracted information from archives");
tracing::info!("Extracted information from archives");
Ok(results)
}

Expand Down Expand Up @@ -340,7 +340,7 @@ impl Debian {

if !missing.is_empty() {
let _guard = self.pkgmgr_mutex.lock();
log::info!("Downloading missing packages (installed but not in local cache)");
tracing::info!("Downloading missing packages (installed but not in local cache)");
download_debs(&missing)?;
}

Expand Down Expand Up @@ -373,7 +373,7 @@ fn archive_to_entries(
packages: &PackageMap,
interner: &Interner,
) -> anyhow::Result<Vec<FileEntry>> {
log::debug!("Processing {}", deb_file.display());
tracing::debug!("Processing {}", deb_file.display());
// The package is a .deb, which is actually an ar archive
let package_file = File::open(deb_file)?;
let mut archive = ar::Archive::new(package_file);
Expand Down Expand Up @@ -401,7 +401,7 @@ fn archive_to_entries(
if let Some(diversion) = diversions.get(&entry.path) {
if !self_pkg.ids.contains(&diversion.by_package) {
// This file is diverted
log::debug!(
tracing::debug!(
"Diverted file: {opath} -> {npath} by {diverting_pkg} while \
processing {pkg}",
opath = entry.path.display(),
Expand Down Expand Up @@ -583,15 +583,15 @@ fn process_file(interner: &Interner, entry: &DirEntry) -> anyhow::Result<Option<
impl Packages for Debian {
fn packages(&self, interner: &Interner) -> anyhow::Result<Vec<PackageInterned>> {
// Parse status
log::debug!("Loading status to installed packages");
tracing::debug!("Loading status to installed packages");
let (_, mut packages) = {
let mut status = BufReader::new(File::open(STATUS_PATH)?);
parsers::parse_status(interner, &mut status, self.primary_architecture)
}
.context(format!("Failed to parse {}", STATUS_PATH))?;

// Parse extended status
log::debug!("Loading extended status to get auto installed packages");
tracing::debug!("Loading extended status to get auto installed packages");
let extended_packages = {
let mut status = BufReader::new(File::open(EXTENDED_STATUS_PATH)?);
parsers::parse_extended_status(interner, &mut status)?
Expand Down Expand Up @@ -686,7 +686,7 @@ fn download_debs(pkgs: &[&str]) -> Result<(), anyhow::Error> {
.args(pkgs)
.status()?;
if !status.success() {
log::warn!("Failed to download package for {pkgs:?}");
tracing::warn!("Failed to download package for {pkgs:?}");
};
Ok(())
}
Expand All @@ -703,7 +703,7 @@ fn download_deb(pkg: &str) -> Result<(), anyhow::Error> {
])
.status()?;
if !status.success() {
log::warn!("Failed to download package for {pkg}");
tracing::warn!("Failed to download package for {pkg}");
};
Ok(())
}
4 changes: 2 additions & 2 deletions crates/paketkoll_core/src/backend/deb/parsers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ fn fixup_pkg_ids(
));
}
None => {
log::error!(
tracing::error!(
"Package {} has no architecture",
package.name.to_str(interner)
);
Expand Down Expand Up @@ -368,7 +368,7 @@ pub(super) fn parse_extended_status(
"1" => Some(InstallReason::Dependency),
"0" => Some(InstallReason::Explicit),
_ => {
log::warn!("Unknown auto-installed value: {}", stripped);
tracing::warn!("Unknown auto-installed value: {}", stripped);
None
}
};
Expand Down
2 changes: 1 addition & 1 deletion crates/paketkoll_core/src/backend/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ fn check_contents(
}
}
_ => {
log::error!("Checksum {expected_checksum} is of an unsupported type");
tracing::error!("Checksum {expected_checksum} is of an unsupported type");
issues.push(IssueKind::FsCheckError(Box::new(anyhow::anyhow!(
"Unsupported checksum type"
))));
Expand Down
Loading

0 comments on commit 1f5394d

Please sign in to comment.