Skip to content

Commit

Permalink
Using tracing for logs instead of printing directly
Browse files Browse the repository at this point in the history
  • Loading branch information
tux3 committed Jul 7, 2022
1 parent decf0a0 commit 314f20a
Show file tree
Hide file tree
Showing 13 changed files with 268 additions and 106 deletions.
146 changes: 146 additions & 0 deletions Cargo.lock

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

9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@ goblin = "0.4"
regex = "1.3.9"
rayon = "1.4.0"
thiserror = "1.0.31"
tracing = "0.1.35"
tracing-subscriber = { version = "0.3.14", features = ["env-filter", "local-time"], optional = true }
time = { version = "0.3.11", optional = true }

[[bin]]
name = "armerge"
required-features = ["log_subscriber"]

[features]
default = ["log_subscriber"]
# EXPERIMENTAL. Uses objpoke instead of objcopy for localizing ELF symbols in-place. Very fast, but not stable for production use.
objpoke_symbols = []
log_subscriber = ["dep:tracing-subscriber", "dep:time"]
6 changes: 3 additions & 3 deletions src/arbuilder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ pub trait ArBuilder: Debug {
fn close(self: Box<Self>) -> Result<(), MergeError>;
}

pub fn host_platform_builder(path: &Path, verbose: bool) -> Box<dyn ArBuilder> {
pub fn host_platform_builder(path: &Path) -> Box<dyn ArBuilder> {
if std::env::consts::OS == "macos" {
Box::new(mac::MacArBuilder::new(path, verbose))
Box::new(mac::MacArBuilder::new(path))
} else {
Box::new(common::CommonArBuilder::new(path, verbose))
Box::new(common::CommonArBuilder::new(path))
}
}
6 changes: 2 additions & 4 deletions src/arbuilder/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ pub struct CommonArBuilder {
builder: Builder<File>,
output_path: PathBuf,
closed: bool,
verbose: bool,
}

impl Debug for CommonArBuilder {
Expand All @@ -35,13 +34,12 @@ impl ArBuilder for CommonArBuilder {
}

impl CommonArBuilder {
pub fn new(path: impl Into<PathBuf>, verbose: bool) -> Self {
pub fn new(path: impl Into<PathBuf>) -> Self {
let path = path.into();
Self {
builder: Builder::new(File::create(&path).expect("Failed to create output library")),
output_path: path,
closed: false,
verbose,
}
}

Expand All @@ -51,6 +49,6 @@ impl CommonArBuilder {
}
self.closed = true;

archives::create_index(&self.output_path, self.verbose)
archives::create_index(&self.output_path)
}
}
24 changes: 11 additions & 13 deletions src/arbuilder/mac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use crate::MergeError::ExternalToolLaunchError;
use std::ffi::OsString;
use std::path::{Path, PathBuf};
use std::process::Command;
use tracing::info;

#[derive(Debug)]
pub struct MacArBuilder {
output_path: PathBuf,
obj_paths: Vec<PathBuf>,
verbose: bool,
closed: bool,
}

Expand All @@ -25,11 +25,10 @@ impl ArBuilder for MacArBuilder {
}

impl MacArBuilder {
pub fn new(path: &Path, verbose: bool) -> Self {
pub fn new(path: &Path) -> Self {
Self {
output_path: path.to_owned(),
obj_paths: vec![],
verbose,
closed: false,
}
}
Expand All @@ -53,16 +52,15 @@ impl MacArBuilder {
.inspect(|_| count += 1)
.map(|p| p.as_os_str().into()),
);
if self.verbose {
println!(
"Merging {} objects: libtool {}",
count,
args.iter()
.map(|s| s.to_string_lossy())
.collect::<Vec<_>>()
.join(" ")
);
}

info!(
"Merging {} objects: libtool {}",
count,
args.iter()
.map(|s| s.to_string_lossy())
.collect::<Vec<_>>()
.join(" ")
);

let output =
Command::new("libtool")
Expand Down
7 changes: 3 additions & 4 deletions src/archives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::fmt::{Debug, Formatter};
use std::fs::File;
use std::io::{Read, Write};
use tempdir::TempDir;
use tracing::info;

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum ArchiveContents {
Expand Down Expand Up @@ -114,12 +115,10 @@ pub fn extract_objects<I: IntoIterator<Item = InputLibrary<R>>, R: Read>(
})
}

pub fn create_index(archive_path: &std::path::Path, verbose: bool) -> Result<(), MergeError> {
pub fn create_index(archive_path: &std::path::Path) -> Result<(), MergeError> {
use std::process::Command;

if verbose {
println!("ranlib {}", archive_path.to_string_lossy());
}
info!("ranlib {}", archive_path.to_string_lossy());

let output = Command::new("ranlib")
.args(vec![archive_path])
Expand Down
14 changes: 7 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use regex::Regex;
use std::fs::File;
use std::io::Read;
use std::path::Path;
use tracing::error;

#[derive(Debug)]
pub struct ArMerger {
Expand Down Expand Up @@ -66,15 +67,15 @@ impl ArMerger {
) -> Result<Box<dyn ArBuilder>, ProcessInputError> {
Ok(match contents_type {
ArchiveContents::Empty => return Err(ProcessInputError::Empty),
ArchiveContents::Elf => Box::new(CommonArBuilder::new(output.as_ref(), false)),
ArchiveContents::MachO => Box::new(MacArBuilder::new(output.as_ref(), false)),
ArchiveContents::Elf => Box::new(CommonArBuilder::new(output.as_ref())),
ArchiveContents::MachO => Box::new(MacArBuilder::new(output.as_ref())),
ArchiveContents::Other => {
eprintln!("Input archives contain neither ELF nor Mach-O files, trying to continue with your host toolchain");
arbuilder::host_platform_builder(output.as_ref(), false)
error!("Input archives contain neither ELF nor Mach-O files, trying to continue with your host toolchain");
arbuilder::host_platform_builder(output.as_ref())
}
ArchiveContents::Mixed => {
eprintln!("Input archives contain different object file formats, trying to continue with your host toolchain");
arbuilder::host_platform_builder(output.as_ref(), false)
error!("Input archives contain different object file formats, trying to continue with your host toolchain");
arbuilder::host_platform_builder(output.as_ref())
}
})
}
Expand All @@ -100,7 +101,6 @@ impl ArMerger {
self.extracted.contents_type,
self.extracted.object_dir,
keep_symbols_regexes.into_iter().collect(),
false,
)
}
}
Loading

0 comments on commit 314f20a

Please sign in to comment.