Skip to content

Commit

Permalink
Use tracing for configurable logging
Browse files Browse the repository at this point in the history
  • Loading branch information
neocturne committed Sep 19, 2023
1 parent e57ec81 commit a8eb2da
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 11 deletions.
135 changes: 135 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ rustc-hash = "1.1.0"
serde = { version = "1.0.152", features = ["rc", "derive"] }
serde_json = "1.0.99"
tokio = { version = "1.31.0", features = ["rt", "parking_lot", "sync"] }
tracing = "0.1.37"
tracing-subscriber = "0.3.17"
zstd = "0.12.3"

[features]
Expand Down
12 changes: 12 additions & 0 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ pub struct Args {
/// use one thread per logical CPU core.
#[arg(short, long)]
pub jobs: Option<usize>,
/// Enable verbose messages
#[arg(short, long)]
pub verbose: bool,
/// Minecraft save directory
pub input_dir: PathBuf,
/// MinedMap data directory
Expand All @@ -54,6 +57,15 @@ pub fn cli() -> Result<()> {
let args = Args::parse();
let config = Config::new(&args);

tracing_subscriber::fmt()
.with_max_level(if args.verbose {
tracing::Level::DEBUG
} else {
tracing::Level::INFO
})
.with_target(false)
.init();

setup_threads(config.num_threads)?;

let rt = tokio::runtime::Builder::new_current_thread()
Expand Down
7 changes: 4 additions & 3 deletions src/core/region_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{ffi::OsStr, path::Path, time::SystemTime};
use anyhow::{Context, Result};
use indexmap::IndexSet;
use rayon::prelude::*;
use tracing::{debug, error};

use super::common::*;
use crate::{
Expand Down Expand Up @@ -148,11 +149,11 @@ impl<'a> RegionProcessor<'a> {

if Some(input_timestamp) <= output_timestamp && Some(input_timestamp) <= lightmap_timestamp
{
println!("Skipping unchanged region r.{}.{}.mca", coords.x, coords.z);
debug!("Skipping unchanged region r.{}.{}.mca", coords.x, coords.z);
return Ok(());
}

println!("Processing region r.{}.{}.mca", coords.x, coords.z);
debug!("Processing region r.{}.{}.mca", coords.x, coords.z);

crate::nbt::region::from_file(path)?.foreach_chunk(
|chunk_coords, data: world::de::Chunk| {
Expand Down Expand Up @@ -204,7 +205,7 @@ impl<'a> RegionProcessor<'a> {

regions.par_iter().for_each(|&coords| {
if let Err(err) = self.process_region(coords) {
eprintln!("Failed to process region {:?}: {:?}", coords, err);
error!("Failed to process region {:?}: {:?}", coords, err);
}
});

Expand Down
9 changes: 5 additions & 4 deletions src/core/tile_mipmapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use anyhow::{Context, Result};
use rayon::prelude::*;
use tracing::{debug, warn};

use super::common::*;
use crate::{io::fs, types::*};
Expand Down Expand Up @@ -83,7 +84,7 @@ impl<'a> TileMipmapper<'a> {
let timestamp = match fs::modified_timestamp(&source_path) {
Ok(timestamp) => timestamp,
Err(err) => {
eprintln!("{}", err);
warn!("{}", err);
return None;
}
};
Expand All @@ -96,7 +97,7 @@ impl<'a> TileMipmapper<'a> {
};

if Some(input_timestamp) <= output_timestamp {
println!(
debug!(
"Skipping unchanged mipmap tile {}",
output_path
.strip_prefix(&self.config.output_dir)
Expand All @@ -106,7 +107,7 @@ impl<'a> TileMipmapper<'a> {
return Ok(());
}

println!(
debug!(
"Rendering mipmap tile {}",
output_path
.strip_prefix(&self.config.output_dir)
Expand All @@ -121,7 +122,7 @@ impl<'a> TileMipmapper<'a> {
let source = match image::open(&source_path) {
Ok(source) => source,
Err(err) => {
eprintln!(
warn!(
"Failed to read source image {}: {}",
source_path.display(),
err,
Expand Down
5 changes: 3 additions & 2 deletions src/core/tile_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use glam::Vec3;
use lru::LruCache;
use rayon::prelude::*;
use tokio::sync::OnceCell;
use tracing::debug;

use super::{common::*, region_group::RegionGroup};
use crate::{
Expand Down Expand Up @@ -272,7 +273,7 @@ impl<'a> TileRenderer<'a> {
let output_timestamp = fs::read_timestamp(&output_path, FILE_META_VERSION);

if Some(processed_timestamp) <= output_timestamp {
println!(
debug!(
"Skipping unchanged tile {}",
output_path
.strip_prefix(&self.config.output_dir)
Expand All @@ -282,7 +283,7 @@ impl<'a> TileRenderer<'a> {
return Ok(());
}

println!(
debug!(
"Rendering tile {}",
output_path
.strip_prefix(&self.config.output_dir)
Expand Down
5 changes: 3 additions & 2 deletions src/world/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::fmt::Debug;

use anyhow::{bail, Context, Result};
use num_integer::div_rem;
use tracing::warn;

use super::de;
use crate::{
Expand Down Expand Up @@ -94,7 +95,7 @@ impl<'a> SectionV1_13<'a> {
.map(|entry| {
let block_type = block_types.get(&entry.name);
if block_type.is_none() {
eprintln!("Unknown block type: {}", entry.name);
warn!("Unknown block type: {}", entry.name);
}
block_type
})
Expand Down Expand Up @@ -246,7 +247,7 @@ impl<'a> BiomesV1_18<'a> {
.map(|entry| {
let biome_type = biome_types.get(entry);
if biome_type.is_none() {
eprintln!("Unknown biome type: {}", entry);
warn!("Unknown biome type: {}", entry);
}
biome_type
})
Expand Down

0 comments on commit a8eb2da

Please sign in to comment.