Skip to content

Commit dd6d537

Browse files
committed
Add logging facility
1 parent ce33408 commit dd6d537

File tree

7 files changed

+59
-3
lines changed

7 files changed

+59
-3
lines changed

Cargo.lock

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ readme = "README.md"
1212
license = "MIT OR Apache-2.0"
1313

1414
[workspace.dependencies]
15-
svg2pdf = { path = ".", version = "0.9.1", default-features = false }
15+
log = "0.4.20"
16+
svg2pdf = { path = ".", default-features = false, version = "0.9.1" }
1617
clap = { version = "4.4.2", features = ["derive"] }
1718
clap_complete = "4.4.3"
1819
clap_mangen = "0.2.14"
@@ -52,6 +53,7 @@ miniz_oxide = { workspace = true }
5253
once_cell = { workspace = true }
5354
pdf-writer = { workspace = true }
5455
usvg = { workspace = true }
56+
log = { workspace = true }
5557
image = { workspace = true, optional = true }
5658
tiny-skia = {workspace = true, optional = true}
5759
resvg = {workspace = true, optional = true}

cli/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ doc = false
2222
clap = { workspace = true }
2323
fontdb = { workspace = true }
2424
image = { workspace = true }
25+
log = { workspace = true }
2526
miniz_oxide = { workspace = true }
2627
pdf-writer = { workspace = true }
2728
svg2pdf = { workspace = true }

cli/src/convert.rs

+45
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ pub fn convert_(
1212
output: Option<PathBuf>,
1313
dpi: f32,
1414
) -> Result<(), String> {
15+
if let Ok(()) = log::set_logger(&LOGGER) {
16+
log::set_max_level(log::LevelFilter::Warn);
17+
}
18+
1519
// Prepare the font database.
1620
let mut fontdb = fontdb::Database::new();
1721
fontdb.load_system_fonts();
@@ -39,3 +43,44 @@ pub fn convert_(
3943

4044
Ok(())
4145
}
46+
47+
// Taken from resvg
48+
/// A simple stderr logger.
49+
static LOGGER: SimpleLogger = SimpleLogger;
50+
struct SimpleLogger;
51+
impl log::Log for SimpleLogger {
52+
fn enabled(&self, metadata: &log::Metadata) -> bool {
53+
metadata.level() <= log::LevelFilter::Warn
54+
}
55+
56+
fn log(&self, record: &log::Record) {
57+
if self.enabled(record.metadata()) {
58+
let target = if !record.target().is_empty() {
59+
record.target()
60+
} else {
61+
record.module_path().unwrap_or_default()
62+
};
63+
64+
let line = record.line().unwrap_or(0);
65+
let args = record.args();
66+
67+
match record.level() {
68+
log::Level::Error => {
69+
eprintln!("Error (in {}:{}): {}", target, line, args)
70+
}
71+
log::Level::Warn => {
72+
eprintln!("Warning (in {}:{}): {}", target, line, args)
73+
}
74+
log::Level::Info => eprintln!("Info (in {}:{}): {}", target, line, args),
75+
log::Level::Debug => {
76+
eprintln!("Debug (in {}:{}): {}", target, line, args)
77+
}
78+
log::Level::Trace => {
79+
eprintln!("Trace (in {}:{}): {}", target, line, args)
80+
}
81+
}
82+
}
83+
}
84+
85+
fn flush(&self) {}
86+
}

src/render/group.rs

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ pub fn render(
2424
return;
2525
}
2626

27+
#[cfg(not(feature = "filters"))]
28+
if !group.filters().is_empty() {
29+
log::warn!("Filters have been disabled in this build of svg2pdf.")
30+
}
31+
2732
let initial_opacity = initial_opacity.unwrap_or(Opacity::ONE);
2833

2934
// Filters will be ignored

src/render/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ impl Render for Node {
6767
ctx,
6868
),
6969
#[cfg(not(feature = "image"))]
70-
Node::Image(_) => {},
70+
Node::Image(_) => {
71+
log::warn!("Images have been disabled in this build of svg2pdf.")
72+
}
7173
Node::Text(ref text) => {
7274
group::render(
7375
text.flattened(),

tests/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#[rustfmt::skip]
22
mod library;
3-
mod cli;
43

54
use std::cmp::max;
65
use std::fs;

0 commit comments

Comments
 (0)