Skip to content

Commit d3d1988

Browse files
committed
Add line number + file name + function name to logs
1 parent 9e59d41 commit d3d1988

File tree

8 files changed

+51
-6
lines changed

8 files changed

+51
-6
lines changed

monitor/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,9 @@ baa.workspace = true
1616
patronus.workspace = true
1717
wellen = "0.19.0"
1818
log = "0.4.27"
19+
stdext = "0.3"
1920
rustc-hash.workspace = true
2021
anyhow = "1.0.100"
22+
23+
[dev-dependencies]
24+
wellen = "0.19.0"

monitor/src/designs.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// author: Kevin Laeufer <laeufer@cornell.edu>
44
// author: Ernest Ng <eyn5@cornell.edu>
55

6-
use log::info;
76
use protocols::{
87
ir::{Field, SymbolId, SymbolTable, Transaction, Type},
98
serialize::serialize_field,

monitor/src/global_scheduler.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use crate::{
1414
Trace,
1515
},
1616
};
17-
use log::info;
1817
use rustc_hash::FxHashSet;
1918

2019
pub struct GlobalScheduler {

monitor/src/interpreter.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::collections::hash_map::Entry;
22

33
use baa::{BitVecMutOps, BitVecOps, BitVecValue};
4-
use log::info;
54
use protocols::{
65
errors::{AssertionError, EvaluationError, ExecutionError, ExecutionResult, SymbolError},
76
interpreter::ExprValue,

monitor/src/macros.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/// A wrapper around the `log::info!` macro, which includes the name of the
2+
/// function in which the log was called (obtained via `stdext::function_name!`)
3+
macro_rules! info {
4+
($($arg:tt)*) => { log::info!(target: stdext::function_name!(), $($arg)*) }
5+
}

monitor/src/main.rs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// author: Ernest Ng <eyn5@cornell.edu>
44
// author: Kevin Laeufer <laeufer@cornell.edu>
55

6+
#[macro_use]
7+
mod macros;
68
mod axi_experiment;
79
mod designs;
810
mod global_context;
@@ -27,6 +29,8 @@ use protocols::diagnostic::DiagnosticHandler;
2729
use protocols::ir::{SymbolTable, Transaction};
2830
use protocols::parser::parsing_helper;
2931
use protocols::typecheck::type_check;
32+
use std::io::Write;
33+
use std::path::Path;
3034

3135
// From the top-level directory, run:
3236
// $ cargo run --package protocols-monitor -- -p protocols/tests/adders/adder_d1/add_d1.prot -w trace.fst -i add_d1:Adder
@@ -104,8 +108,45 @@ fn main() -> anyhow::Result<()> {
104108
// For concision, we disable timestamps and the module paths in the log
105109
let mut logger = env_logger::Builder::new();
106110

111+
let use_color = cli.color != ColorChoice::Never;
107112
logger
108-
.format_timestamp(None)
113+
.format(move |buf, record| {
114+
use clap::builder::styling::{AnsiColor, Color, Style};
115+
use log::Level;
116+
let file = record
117+
.file()
118+
.and_then(|f| Path::new(f).file_name()?.to_str())
119+
.unwrap_or("?");
120+
let line = record.line().unwrap_or(0);
121+
let fn_name = record
122+
.target()
123+
.rsplit("::")
124+
.next()
125+
.unwrap_or(record.target());
126+
let level_style = if use_color {
127+
let color = match record.level() {
128+
Level::Info => AnsiColor::Cyan,
129+
_ => AnsiColor::Red,
130+
};
131+
Style::new().fg_color(Some(Color::Ansi(color))).bold()
132+
} else {
133+
Style::new()
134+
};
135+
let dim_style = if use_color {
136+
Style::new().dimmed()
137+
} else {
138+
Style::new()
139+
};
140+
writeln!(
141+
buf,
142+
"{}{file}:{line}{} {}({fn_name}){}: {}",
143+
dim_style.render(),
144+
dim_style.render_reset(),
145+
level_style.render(),
146+
level_style.render_reset(),
147+
record.args()
148+
)
149+
})
109150
.filter_level(cli.verbosity.log_level_filter());
110151

111152
if cli.color == ColorChoice::Never {

monitor/src/scheduler.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use anyhow::{Context, anyhow};
22
use baa::{BitVecOps, BitVecValue};
3-
use log::info;
43
use protocols::{
54
errors::{EvaluationError, ExecutionError},
65
ir::{Expr, Stmt, StmtId, SymbolId, SymbolTable, Transaction},

monitor/src/signal_trace.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use crate::{Instance, designs::Design, global_context::TimeUnit};
77
use anyhow::Context;
88
use baa::BitVecValue;
9-
use log::info;
109
use protocols::ir::SymbolId;
1110
use rustc_hash::FxHashMap;
1211
use wellen::{Hierarchy, SignalRef};

0 commit comments

Comments
 (0)