Skip to content

Commit

Permalink
Add logging facility
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurenzV committed Feb 17, 2024
1 parent ce33408 commit dd6d537
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ readme = "README.md"
license = "MIT OR Apache-2.0"

[workspace.dependencies]
svg2pdf = { path = ".", version = "0.9.1", default-features = false }
log = "0.4.20"
svg2pdf = { path = ".", default-features = false, version = "0.9.1" }
clap = { version = "4.4.2", features = ["derive"] }
clap_complete = "4.4.3"
clap_mangen = "0.2.14"
Expand Down Expand Up @@ -52,6 +53,7 @@ miniz_oxide = { workspace = true }
once_cell = { workspace = true }
pdf-writer = { workspace = true }
usvg = { workspace = true }
log = { workspace = true }
image = { workspace = true, optional = true }
tiny-skia = {workspace = true, optional = true}
resvg = {workspace = true, optional = true}
1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ doc = false
clap = { workspace = true }
fontdb = { workspace = true }
image = { workspace = true }
log = { workspace = true }
miniz_oxide = { workspace = true }
pdf-writer = { workspace = true }
svg2pdf = { workspace = true }
Expand Down
45 changes: 45 additions & 0 deletions cli/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ pub fn convert_(
output: Option<PathBuf>,
dpi: f32,
) -> Result<(), String> {
if let Ok(()) = log::set_logger(&LOGGER) {
log::set_max_level(log::LevelFilter::Warn);
}

// Prepare the font database.
let mut fontdb = fontdb::Database::new();
fontdb.load_system_fonts();
Expand Down Expand Up @@ -39,3 +43,44 @@ pub fn convert_(

Ok(())
}

// Taken from resvg
/// A simple stderr logger.
static LOGGER: SimpleLogger = SimpleLogger;
struct SimpleLogger;
impl log::Log for SimpleLogger {
fn enabled(&self, metadata: &log::Metadata) -> bool {
metadata.level() <= log::LevelFilter::Warn
}

fn log(&self, record: &log::Record) {
if self.enabled(record.metadata()) {
let target = if !record.target().is_empty() {
record.target()
} else {
record.module_path().unwrap_or_default()
};

let line = record.line().unwrap_or(0);
let args = record.args();

match record.level() {
log::Level::Error => {
eprintln!("Error (in {}:{}): {}", target, line, args)
}
log::Level::Warn => {
eprintln!("Warning (in {}:{}): {}", target, line, args)
}
log::Level::Info => eprintln!("Info (in {}:{}): {}", target, line, args),
log::Level::Debug => {
eprintln!("Debug (in {}:{}): {}", target, line, args)
}
log::Level::Trace => {
eprintln!("Trace (in {}:{}): {}", target, line, args)
}
}
}
}

fn flush(&self) {}
}
5 changes: 5 additions & 0 deletions src/render/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ pub fn render(
return;
}

#[cfg(not(feature = "filters"))]
if !group.filters().is_empty() {
log::warn!("Filters have been disabled in this build of svg2pdf.")
}

let initial_opacity = initial_opacity.unwrap_or(Opacity::ONE);

// Filters will be ignored
Expand Down
4 changes: 3 additions & 1 deletion src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ impl Render for Node {
ctx,
),
#[cfg(not(feature = "image"))]
Node::Image(_) => {},
Node::Image(_) => {
log::warn!("Images have been disabled in this build of svg2pdf.")
}
Node::Text(ref text) => {
group::render(
text.flattened(),
Expand Down
1 change: 0 additions & 1 deletion tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#[rustfmt::skip]
mod library;
mod cli;

use std::cmp::max;
use std::fs;
Expand Down

0 comments on commit dd6d537

Please sign in to comment.