Skip to content

Commit 5d3ca8f

Browse files
committed
Use breadcrumbs logging instead of println! / eprintln!
1 parent 203223e commit 5d3ca8f

File tree

17 files changed

+1350
-576
lines changed

17 files changed

+1350
-576
lines changed

Cargo.lock

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ serde = { version = "1.0.219", features = ["derive"] }
1616
serde_json = "1.0.140"
1717
sha2 = "0.10.9"
1818
zerocopy = "0.8.25"
19+
breadcrumbs = "0.1.5"
1920

2021
[lib]
2122
crate-type = ["dylib"]

src/lib.rs

Lines changed: 109 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ extern crate rustc_hir;
2020
extern crate rustc_metadata;
2121
extern crate rustc_middle;
2222
extern crate rustc_session;
23-
extern crate rustc_target;
2423
extern crate rustc_span;
24+
extern crate rustc_target;
2525

2626
use oomir::{CodeBlock, Function, Operand, Type};
2727
use rustc_codegen_ssa::back::archive::{ArArchiveBuilder, ArchiveBuilder, ArchiveBuilderBuilder};
@@ -42,9 +42,9 @@ use misc::ToIdent;
4242

4343
mod lower1;
4444
mod lower2;
45+
mod misc;
4546
mod oomir;
4647
mod optimise1;
47-
mod misc;
4848

4949
/// An instance of our Java bytecode codegen backend.
5050
struct MyBackend;
@@ -86,11 +86,23 @@ impl CodegenBackend for MyBackend {
8686
let instance = rustc_middle::ty::Instance::mono(tcx, def_id);
8787
let mut mir = tcx.optimized_mir(instance.def_id()).clone(); // Clone the MIR
8888

89-
println!("MIR for function {i}: {:?}", mir);
90-
91-
println!("--- Starting MIR to OOMIR Lowering for function: {i} ---");
89+
breadcrumbs::log!(
90+
breadcrumbs::LogLevel::Info,
91+
"backend",
92+
format!("MIR for function {i}: {:?}", mir)
93+
);
94+
95+
breadcrumbs::log!(
96+
breadcrumbs::LogLevel::Info,
97+
"mir-lowering",
98+
format!("--- Starting MIR to OOMIR Lowering for function: {i} ---")
99+
);
92100
let oomir_result = lower1::mir_to_oomir(tcx, instance, &mut mir);
93-
println!("--- Finished MIR to OOMIR Lowering for function: {i} ---");
101+
breadcrumbs::log!(
102+
breadcrumbs::LogLevel::Info,
103+
"mir-lowering",
104+
format!("--- Finished MIR to OOMIR Lowering for function: {i} ---")
105+
);
94106

95107
let oomir_function = oomir_result.0;
96108

@@ -109,12 +121,20 @@ impl CodegenBackend for MyBackend {
109121
format!("{}", ps.ident)
110122
}
111123
_ => {
112-
println!("Warning: {:?} is an unknown qpath", qpath);
124+
breadcrumbs::log!(
125+
breadcrumbs::LogLevel::Warn,
126+
"backend",
127+
format!("Warning: {:?} is an unknown qpath", qpath)
128+
);
113129
"unknown_qpath_kind".into()
114130
}
115131
},
116132
_ => {
117-
println!("Warning: {:?} has unknown kind", impl_a.self_ty);
133+
breadcrumbs::log!(
134+
breadcrumbs::LogLevel::Warn,
135+
"backend",
136+
format!("Warning: {:?} has unknown kind", impl_a.self_ty)
137+
);
118138
"unknown_type_kind".into()
119139
}
120140
};
@@ -141,11 +161,23 @@ impl CodegenBackend for MyBackend {
141161

142162
let i2 = format!("{}_{}", ident, i).to_lowercase();
143163

144-
println!("MIR for function {i2}: {:?}", mir);
164+
breadcrumbs::log!(
165+
breadcrumbs::LogLevel::Info,
166+
"backend",
167+
format!("MIR for function {i2}: {:?}", mir)
168+
);
145169

146-
println!("--- Starting MIR to OOMIR Lowering for function: {i2} ---");
170+
breadcrumbs::log!(
171+
breadcrumbs::LogLevel::Info,
172+
"mir-lowering",
173+
format!("--- Starting MIR to OOMIR Lowering for function: {i2} ---")
174+
);
147175
let oomir_result = lower1::mir_to_oomir(tcx, instance, &mut mir);
148-
println!("--- Finished MIR to OOMIR Lowering for function: {i2} ---");
176+
breadcrumbs::log!(
177+
breadcrumbs::LogLevel::Info,
178+
"mir-lowering",
179+
format!("--- Finished MIR to OOMIR Lowering for function: {i2} ---")
180+
);
149181

150182
let mut oomir_function = oomir_result.0;
151183
oomir_function.name = i.to_string();
@@ -248,7 +280,11 @@ impl CodegenBackend for MyBackend {
248280
helper_sig.params = new_params;
249281
}
250282

251-
println!("Function signature: {:?}", oomir_function.signature);
283+
breadcrumbs::log!(
284+
breadcrumbs::LogLevel::Info,
285+
"backend",
286+
format!("Function signature: {:?}", oomir_function.signature)
287+
);
252288

253289
let mut instructions = vec![];
254290

@@ -264,7 +300,11 @@ impl CodegenBackend for MyBackend {
264300
idx += 1;
265301
}
266302

267-
println!("Function args: {:?}", args);
303+
breadcrumbs::log!(
304+
breadcrumbs::LogLevel::Info,
305+
"backend",
306+
format!("Function args: {:?}", args)
307+
);
268308

269309
let mut bbs = HashMap::new();
270310

@@ -526,31 +566,55 @@ impl CodegenBackend for MyBackend {
526566
}
527567
}
528568

529-
println!("OOMIR module: {:?}", oomir_module);
569+
breadcrumbs::log!(
570+
breadcrumbs::LogLevel::Info,
571+
"backend",
572+
format!("OOMIR module: {:?}", oomir_module)
573+
);
530574

531-
println!(
532-
"--- Starting OOMIR Optimisation for module: {} ---",
533-
crate_name
575+
breadcrumbs::log!(
576+
breadcrumbs::LogLevel::Info,
577+
"optimisation",
578+
format!(
579+
"--- Starting OOMIR Optimisation for module: {} ---",
580+
crate_name
581+
)
534582
);
535583

536584
let oomir_module = optimise1::optimise_module(oomir_module);
537585

538-
println!("Optimised OOMIR module: {:?}", oomir_module);
586+
breadcrumbs::log!(
587+
breadcrumbs::LogLevel::Info,
588+
"optimisation",
589+
format!("Optimised OOMIR module: {:?}", oomir_module)
590+
);
539591

540-
println!(
541-
"--- Finished OOMIR Optimisation for module: {} ---",
542-
crate_name
592+
breadcrumbs::log!(
593+
breadcrumbs::LogLevel::Info,
594+
"optimisation",
595+
format!(
596+
"--- Finished OOMIR Optimisation for module: {} ---",
597+
crate_name
598+
)
543599
);
544600

545-
println!(
546-
"--- Starting OOMIR to JVM Bytecode Lowering for module: {} ---",
547-
crate_name
601+
breadcrumbs::log!(
602+
breadcrumbs::LogLevel::Info,
603+
"bytecode-gen",
604+
format!(
605+
"--- Starting OOMIR to JVM Bytecode Lowering for module: {} ---",
606+
crate_name
607+
)
548608
);
549609
let bytecode = lower2::oomir_to_jvm_bytecode(&oomir_module, tcx).unwrap();
550610
//let bytecode = vec![0; 1024];
551-
println!(
552-
"--- Finished OOMIR to JVM Bytecode Lowering for module: {} ---",
553-
crate_name
611+
breadcrumbs::log!(
612+
breadcrumbs::LogLevel::Info,
613+
"bytecode-gen",
614+
format!(
615+
"--- Finished OOMIR to JVM Bytecode Lowering for module: {} ---",
616+
crate_name
617+
)
554618
);
555619

556620
Box::new((
@@ -635,7 +699,7 @@ impl CodegenBackend for MyBackend {
635699
metadata: EncodedMetadata,
636700
outputs: &OutputFilenames,
637701
) {
638-
println!("linking!");
702+
breadcrumbs::log!(breadcrumbs::LogLevel::Info, "backend", "linking!");
639703
use rustc_codegen_ssa::back::link::link_binary;
640704
link_binary(
641705
sess,
@@ -648,9 +712,26 @@ impl CodegenBackend for MyBackend {
648712
}
649713
}
650714

715+
struct RustcCodegenJvmLogListener;
716+
717+
const LISTENING_CHANNELS: &[&str] = &[];
718+
719+
impl breadcrumbs::LogListener for RustcCodegenJvmLogListener {
720+
fn on_log(&mut self, log: breadcrumbs::Log) {
721+
if log.level.is_at_least(breadcrumbs::LogLevel::Warn)
722+
|| LISTENING_CHANNELS.contains(&log.channel.as_str())
723+
{
724+
println!("{}", log);
725+
} else {
726+
log.remove();
727+
}
728+
}
729+
}
730+
651731
#[unsafe(no_mangle)]
652732
pub extern "Rust" fn __rustc_codegen_backend() -> Box<dyn CodegenBackend> {
653733
std::alloc::set_alloc_error_hook(custom_alloc_error_hook);
734+
breadcrumbs::init!(RustcCodegenJvmLogListener);
654735
Box::new(MyBackend)
655736
}
656737

0 commit comments

Comments
 (0)