Skip to content

Commit 1212ab8

Browse files
committed
[plantuml/linker] add --log-level flag and structured logging
Adds env_logger/log crates to the linker binary and introduces a --log-level flag (error/warn/info/debug/trace, default: warn) matching the convention used by puml_cli.
1 parent 8104173 commit 1212ab8

2 files changed

Lines changed: 41 additions & 5 deletions

File tree

plantuml/linker/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ rust_binary(
2020
deps = [
2121
"//plantuml/parser/puml_serializer/src/fbs:component_fbs",
2222
"@crates//:clap",
23+
"@crates//:env_logger",
2324
"@crates//:flatbuffers",
25+
"@crates//:log",
2426
"@crates//:serde",
2527
"@crates//:serde_json",
2628
],

plantuml/linker/src/main.rs

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,37 @@
2323
use std::collections::HashMap;
2424
use std::fs;
2525

26-
use clap::Parser;
26+
use clap::{Parser, ValueEnum};
27+
use env_logger::Builder;
2728

2829
use component_fbs::component as fb_component;
2930

31+
// ---------------------------------------------------------------------------
32+
// Log level
33+
// ---------------------------------------------------------------------------
34+
35+
/// CLI-visible log level (mirrors the parser's convention).
36+
#[derive(Copy, Clone, ValueEnum, Debug)]
37+
enum CliLogLevel {
38+
Error,
39+
Warn,
40+
Info,
41+
Debug,
42+
Trace,
43+
}
44+
45+
impl CliLogLevel {
46+
fn to_level_filter(self) -> log::LevelFilter {
47+
match self {
48+
CliLogLevel::Error => log::LevelFilter::Error,
49+
CliLogLevel::Warn => log::LevelFilter::Warn,
50+
CliLogLevel::Info => log::LevelFilter::Info,
51+
CliLogLevel::Debug => log::LevelFilter::Debug,
52+
CliLogLevel::Trace => log::LevelFilter::Trace,
53+
}
54+
}
55+
}
56+
3057
// ---------------------------------------------------------------------------
3158
// CLI
3259
// ---------------------------------------------------------------------------
@@ -48,6 +75,10 @@ struct Args {
4875
/// Output JSON file path
4976
#[arg(long, default_value = "plantuml_links.json")]
5077
output: String,
78+
79+
/// Log level: error, warn, info, debug, trace
80+
#[arg(long, value_enum, default_value = "warn")]
81+
log_level: CliLogLevel,
5182
}
5283

5384
// ---------------------------------------------------------------------------
@@ -198,6 +229,9 @@ fn generate_links(diagrams: &[DiagramInfo]) -> Vec<LinkEntry> {
198229

199230
fn main() -> Result<(), Box<dyn std::error::Error>> {
200231
let args = Args::parse();
232+
Builder::new()
233+
.filter_level(args.log_level.to_level_filter())
234+
.init();
201235

202236
if args.fbs_files.is_empty() {
203237
return Err("No .fbs.bin files provided. Use --fbs-files <file> ...".into());
@@ -207,26 +241,26 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
207241
for fbs_path in &args.fbs_files {
208242
match read_diagram(fbs_path) {
209243
Ok(diagram) => {
210-
eprintln!(
244+
log::info!(
211245
"Read {} components from {}",
212246
diagram.components.len(),
213247
diagram.source_file
214248
);
215249
diagrams.push(diagram);
216250
}
217251
Err(e) => {
218-
eprintln!("Warning: skipping {fbs_path}: {e}");
252+
log::warn!("Skipping {}: {}", fbs_path, e);
219253
}
220254
}
221255
}
222256

223257
let links = generate_links(&diagrams);
224-
eprintln!("Generated {} link(s)", links.len());
258+
log::info!("Generated {} link(s)", links.len());
225259

226260
let output = LinksJson { links };
227261
let json = serde_json::to_string_pretty(&output)?;
228262
fs::write(&args.output, &json)?;
229-
eprintln!("Written to {}", args.output);
263+
log::debug!("Written to {}", args.output);
230264

231265
Ok(())
232266
}

0 commit comments

Comments
 (0)