Skip to content

Commit 1488ffa

Browse files
authored
Use log crate instead of eprintln & remove some unwraps (#5010)
<!-- Please read the "Making a PR" section of [`CONTRIBUTING.md`](https://github.com/emilk/egui/blob/master/CONTRIBUTING.md) before opening a Pull Request! * Keep your PR:s small and focused. * The PR title is what ends up in the changelog, so make it descriptive! * If applicable, add a screenshot or gif. * If it is a non-trivial addition, consider adding a demo for it to `egui_demo_lib`, or a new example. * Do NOT open PR:s from your `master` branch, as that makes it hard for maintainers to test and add commits to your PR. * Remember to run `cargo fmt` and `cargo clippy`. * Open the PR as a draft until you have self-reviewed it and run `./scripts/check.sh`. * When you have addressed a PR comment, mark it as resolved. Please be patient! I will review your PR, but my time is limited! --> - I fixed the TODO to use the `log` crate instead of `eprintln` - Set the rust-version in the `scripts/check.sh` to the same as egui is on - I made xtask use anyhow to remove some unwraps * [x] I have followed the instructions in the PR template
1 parent 6607610 commit 1488ffa

File tree

13 files changed

+31
-23
lines changed

13 files changed

+31
-23
lines changed

Cargo.lock

+2
Original file line numberDiff line numberDiff line change
@@ -3155,6 +3155,7 @@ version = "0.1.0"
31553155
dependencies = [
31563156
"eframe",
31573157
"env_logger",
3158+
"log",
31583159
"puffin",
31593160
"puffin_http",
31603161
]
@@ -3583,6 +3584,7 @@ version = "0.1.0"
35833584
dependencies = [
35843585
"eframe",
35853586
"env_logger",
3587+
"log",
35863588
]
35873589

35883590
[[package]]

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ negative_feature_names = "warn"
207207
nonstandard_macro_braces = "warn"
208208
option_option = "warn"
209209
path_buf_push_overwrite = "warn"
210+
print_stderr = "warn"
210211
ptr_as_ptr = "warn"
211212
ptr_cast_constness = "warn"
212213
pub_without_shorthand = "warn"
@@ -251,11 +252,11 @@ wildcard_dependencies = "warn"
251252
wildcard_imports = "warn"
252253
zero_sized_map_values = "warn"
253254

255+
254256
# TODO(emilk): enable more of these lints:
255257
iter_over_hash_type = "allow"
256258
let_underscore_untyped = "allow"
257259
missing_assert_message = "allow"
258-
print_stderr = "allow" # TODO(emilk): use `log` crate instead
259260
should_panic_without_expect = "allow"
260261
too_many_lines = "allow"
261262
unwrap_used = "allow" # TODO(emilk): We really wanna warn on this one

crates/eframe/src/native/glow_integration.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1547,7 +1547,7 @@ fn save_screenshot_and_exit(
15471547
.unwrap_or_else(|err| {
15481548
panic!("Failed to save screenshot to {path:?}: {err}");
15491549
});
1550-
eprintln!("Screenshot saved to {path:?}.");
1550+
log::info!("Screenshot saved to {path:?}.");
15511551

15521552
#[allow(clippy::exit)]
15531553
std::process::exit(0);

crates/egui/src/hit_test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ mod tests {
417417
];
418418

419419
for (i, w) in widgets.iter().enumerate() {
420-
eprintln!("Widget {i}: {:?}", w.id);
420+
println!("Widget {i}: {:?}", w.id);
421421
}
422422

423423
// In the middle of the bg-left-label:

crates/egui_demo_app/src/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ fn start_puffin_server() {
6464

6565
match puffin_http::Server::new("127.0.0.1:8585") {
6666
Ok(puffin_server) => {
67-
eprintln!("Run: cargo install puffin_viewer && puffin_viewer --url 127.0.0.1:8585");
67+
log::info!("Run: cargo install puffin_viewer && puffin_viewer --url 127.0.0.1:8585");
6868

6969
std::process::Command::new("puffin_viewer")
7070
.arg("--url")
@@ -78,7 +78,7 @@ fn start_puffin_server() {
7878
std::mem::forget(puffin_server);
7979
}
8080
Err(err) => {
81-
eprintln!("Failed to start puffin server: {err}");
81+
log::error!("Failed to start puffin server: {err}");
8282
}
8383
};
8484
}

crates/emath/src/rect.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -775,22 +775,22 @@ mod tests {
775775
fn test_ray_intersection() {
776776
let rect = Rect::from_min_max(pos2(1.0, 1.0), pos2(3.0, 3.0));
777777

778-
eprintln!("Righward ray from left:");
778+
println!("Righward ray from left:");
779779
assert!(rect.intersects_ray(pos2(0.0, 2.0), Vec2::RIGHT));
780780

781-
eprintln!("Righward ray from center:");
781+
println!("Righward ray from center:");
782782
assert!(rect.intersects_ray(pos2(2.0, 2.0), Vec2::RIGHT));
783783

784-
eprintln!("Righward ray from right:");
784+
println!("Righward ray from right:");
785785
assert!(!rect.intersects_ray(pos2(4.0, 2.0), Vec2::RIGHT));
786786

787-
eprintln!("Leftward ray from left:");
787+
println!("Leftward ray from left:");
788788
assert!(!rect.intersects_ray(pos2(0.0, 2.0), Vec2::LEFT));
789789

790-
eprintln!("Leftward ray from center:");
790+
println!("Leftward ray from center:");
791791
assert!(rect.intersects_ray(pos2(2.0, 2.0), Vec2::LEFT));
792792

793-
eprintln!("Leftward ray from right:");
793+
println!("Leftward ray from right:");
794794
assert!(rect.intersects_ray(pos2(4.0, 2.0), Vec2::LEFT));
795795
}
796796
}

crates/emath/src/smart_aim.rs

-3
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ pub fn best_in_range_f64(min: f64, max: f64) -> f64 {
5656
let min_str = to_decimal_string(min / exp_factor);
5757
let max_str = to_decimal_string(max / exp_factor);
5858

59-
// eprintln!("min_str: {:?}", min_str);
60-
// eprintln!("max_str: {:?}", max_str);
61-
6259
let mut ret_str = [0; NUM_DECIMALS];
6360

6461
// Select the common prefix:

crates/epaint/src/tessellator.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1749,8 +1749,12 @@ impl Tessellator {
17491749
}
17501750

17511751
if galley.pixels_per_point != self.pixels_per_point {
1752-
eprintln!("epaint: WARNING: pixels_per_point (dpi scale) have changed between text layout and tessellation. \
1753-
You must recreate your text shapes if pixels_per_point changes.");
1752+
let warn = "epaint: WARNING: pixels_per_point (dpi scale) have changed between text layout and tessellation. \
1753+
You must recreate your text shapes if pixels_per_point changes.";
1754+
#[cfg(feature = "log")]
1755+
log::warn!("{warn}");
1756+
#[cfg(not(feature = "log"))]
1757+
println!("{warn}");
17541758
}
17551759

17561760
out.vertices.reserve(galley.num_vertices);

examples/puffin_profiler/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ env_logger = { version = "0.10", default-features = false, features = [
2525
"auto-color",
2626
"humantime",
2727
] }
28+
log = { workspace = true }
2829
puffin = "0.19"
2930
puffin_http = "0.16"

examples/puffin_profiler/src/main.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use std::sync::{
99
use eframe::egui;
1010

1111
fn main() -> eframe::Result {
12+
let rust_log = std::env::var("RUST_LOG").unwrap_or_else(|_| "info".to_owned());
13+
std::env::set_var("RUST_LOG", rust_log);
1214
env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).
1315
start_puffin_server(); // NOTE: you may only want to call this if the users specifies some flag or clicks a button!
1416

@@ -153,7 +155,7 @@ fn start_puffin_server() {
153155

154156
match puffin_http::Server::new("127.0.0.1:8585") {
155157
Ok(puffin_server) => {
156-
eprintln!("Run: cargo install puffin_viewer && puffin_viewer --url 127.0.0.1:8585");
158+
log::info!("Run: cargo install puffin_viewer && puffin_viewer --url 127.0.0.1:8585");
157159

158160
std::process::Command::new("puffin_viewer")
159161
.arg("--url")
@@ -167,7 +169,7 @@ fn start_puffin_server() {
167169
std::mem::forget(puffin_server);
168170
}
169171
Err(err) => {
170-
eprintln!("Failed to start puffin server: {err}");
172+
log::error!("Failed to start puffin server: {err}");
171173
}
172174
};
173175
}

examples/serial_windows/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ env_logger = { version = "0.10", default-features = false, features = [
2020
"auto-color",
2121
"humantime",
2222
] }
23+
log = { workspace = true }

examples/serial_windows/src/main.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fn main() -> eframe::Result {
1212
..Default::default()
1313
};
1414

15-
eprintln!("Starting first window…");
15+
log::info!("Starting first window…");
1616
eframe::run_native(
1717
"First Window",
1818
options.clone(),
@@ -21,7 +21,7 @@ fn main() -> eframe::Result {
2121

2222
std::thread::sleep(std::time::Duration::from_secs(2));
2323

24-
eprintln!("Starting second window…");
24+
log::info!("Starting second window…");
2525
eframe::run_native(
2626
"Second Window",
2727
options.clone(),
@@ -30,7 +30,7 @@ fn main() -> eframe::Result {
3030

3131
std::thread::sleep(std::time::Duration::from_secs(2));
3232

33-
eprintln!("Starting third window…");
33+
log::info!("Starting third window…");
3434
eframe::run_native(
3535
"Third Window",
3636
options,
@@ -53,7 +53,7 @@ impl eframe::App for MyApp {
5353
ui.label(label_text);
5454

5555
if ui.button("Close").clicked() {
56-
eprintln!("Pressed Close button");
56+
log::info!("Pressed Close button");
5757
ui.ctx().send_viewport_cmd(egui::ViewportCommand::Close);
5858
}
5959
});

xtask/src/utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub fn ask_to_run(mut cmd: Command, ask: bool, reason: &str) -> Result<(), DynEr
3434
a => return Err(format!("Invalid answer `{a}`").into()),
3535
};
3636
} else {
37-
eprintln!("Running `{cmd:?}` to {reason}.");
37+
println!("Running `{cmd:?}` to {reason}.");
3838
}
3939

4040
let status = cmd.status()?;

0 commit comments

Comments
 (0)