From ef90c78438f8043bccf61104caac40831a5c2d31 Mon Sep 17 00:00:00 2001 From: Matthew LeVan Date: Fri, 15 Mar 2024 08:51:36 -0400 Subject: [PATCH 1/4] debug: use `flog!` macro instead of `eprintln!` sometimes --- rust/ares/src/interpreter.rs | 12 ++++++++---- rust/ares/src/jets.rs | 6 +++--- rust/ares/src/serf.rs | 8 +++++--- rust/ares/src/trace.rs | 15 ++++++++++----- 4 files changed, 26 insertions(+), 15 deletions(-) diff --git a/rust/ares/src/interpreter.rs b/rust/ares/src/interpreter.rs index 250aa966..c250d883 100644 --- a/rust/ares/src/interpreter.rs +++ b/rust/ares/src/interpreter.rs @@ -1,6 +1,7 @@ use crate::assert_acyclic; use crate::assert_no_forwarding_pointers; use crate::assert_no_junior_pointers; +use crate::flog; use crate::guard::call_with_guard; use crate::hamt::Hamt; use crate::jets::cold; @@ -390,7 +391,7 @@ pub fn interpret(context: &mut Context, mut subject: Noun, formula: Noun) -> Res *(context.stack.push()) = NockWork::Done; }; - // DO NOT REMOVE THIS ASSERTION + // DO NOT REMOVE THIS COMMENT // // If you need to allocate for debugging, wrap the debugging code in // @@ -1339,8 +1340,11 @@ unsafe fn write_trace(context: &mut Context) { // Abort writing to trace file if we encountered an error. This should // result in a well-formed partial trace file. if let Err(_e) = write_nock_trace(&mut context.stack, info, trace_stack) { - // XX: need NockStack allocated string interpolation - // eprintln!("\rserf: error writing nock trace to file: {:?}", e); + flog!( + context, + "\rserf: error writing nock trace to file: {:?}", + _e + ); context.trace_info = None; } } @@ -1387,7 +1391,7 @@ mod hint { // XX: what is the head here? let jet_name = jet_formula.tail(); - if let Some(jet) = jets::get_jet(jet_name) { + if let Some(jet) = jets::get_jet(context, jet_name) { match jet(context, subject) { Ok(mut jet_res) => { // XX: simplify this by moving jet test mode into the 11 code in interpret, or into its own function? diff --git a/rust/ares/src/jets.rs b/rust/ares/src/jets.rs index 9d1a97da..f9ebe3cd 100644 --- a/rust/ares/src/jets.rs +++ b/rust/ares/src/jets.rs @@ -15,6 +15,7 @@ pub mod serial; pub mod sort; pub mod tree; +use crate::flog; use crate::interpreter::{Context, Error, Mote}; use crate::jets::bits::*; use crate::jets::cold::Cold; @@ -96,7 +97,7 @@ impl From for Error { } } -pub fn get_jet(jet_name: Noun) -> Option { +pub fn get_jet(context: &mut Context, jet_name: Noun) -> Option { match jet_name.as_direct().ok()?.data() { tas!(b"add") => Some(jet_add), tas!(b"dec") => Some(jet_dec), @@ -166,8 +167,7 @@ pub fn get_jet(jet_name: Noun) -> Option { tas!(b"sivc_de") => Some(jet_sivc_de), // _ => { - // XX: need NockStack allocated string interpolation - // eprintln!("Unknown jet: {:?}", jet_name); + flog!(context, "unknown jet: {:?}", jet_name); None } } diff --git a/rust/ares/src/serf.rs b/rust/ares/src/serf.rs index 1f4aecce..9a94596f 100644 --- a/rust/ares/src/serf.rs +++ b/rust/ares/src/serf.rs @@ -332,6 +332,8 @@ pub fn serf(constant_hot_state: &[HotEntry]) -> io::Result<()> { if let Some(ref mut info) = trace_info.as_mut() { if let Err(_e) = write_metadata(info) { // XX: need NockStack allocated string interpolation + // XX: chicken/egg problem with flog bc it requires context + // before we've initialized it, and context needs trace_info // eprintln!("\rError initializing trace file: {:?}", e); trace_info = None; } @@ -411,7 +413,7 @@ fn peek(context: &mut Context, ovo: Noun) -> Noun { let trace_name = "peek"; let start = Instant::now(); let slam_res = slam(context, PEEK_AXIS, ovo); - write_serf_trace_safe(&mut context.nock_context.trace_info, trace_name, start); + write_serf_trace_safe(&mut context.nock_context, trace_name, start); slam_res.expect("peek error handling unimplemented") } else { @@ -436,7 +438,7 @@ fn soft(context: &mut Context, ovo: Noun, trace_name: Option) -> Result< let start = Instant::now(); let slam_res = slam(context, POKE_AXIS, ovo); write_serf_trace_safe( - &mut context.nock_context.trace_info, + &mut context.nock_context, trace_name.as_ref().unwrap(), start, ); @@ -467,7 +469,7 @@ fn play_life(context: &mut Context, eve: Noun) { let trace_name = "boot"; let start = Instant::now(); let boot_res = interpret(&mut context.nock_context, eve, lyf); - write_serf_trace_safe(&mut context.nock_context.trace_info, trace_name, start); + write_serf_trace_safe(&mut context.nock_context, trace_name, start); boot_res } else { diff --git a/rust/ares/src/trace.rs b/rust/ares/src/trace.rs index 3a4fe046..50bc013e 100644 --- a/rust/ares/src/trace.rs +++ b/rust/ares/src/trace.rs @@ -1,3 +1,5 @@ +use crate::flog; +use crate::interpreter::Context; use crate::jets::bits::util::rap; use crate::jets::form::util::scow; use crate::mem::NockStack; @@ -97,11 +99,14 @@ pub fn write_metadata(info: &mut TraceInfo) -> Result<(), Error> { /// Abort writing to trace file if an error is encountered. /// /// This should result in a well-formed partial trace file. -pub fn write_serf_trace_safe(info: &mut Option, name: &str, start: Instant) { - if let Err(_e) = write_serf_trace(info.as_mut().unwrap(), name, start) { - // XX: need NockStack allocated string interpolation - // eprintln!("\rserf: error writing event trace to file: {:?}", e); - *info = None; +pub fn write_serf_trace_safe(context: &mut Context, name: &str, start: Instant) { + if let Err(e) = write_serf_trace(context.trace_info.as_mut().unwrap(), name, start) { + flog!( + context, + "\rserf: error writing event trace to file: {:?}", + e + ); + *(&mut context.trace_info) = None; } } From cbfa7dc5b3977774e0501fbe21b36356a8c2a624 Mon Sep 17 00:00:00 2001 From: Matthew LeVan Date: Fri, 15 Mar 2024 10:24:11 -0400 Subject: [PATCH 2/4] flog: fix unused result --- rust/ares/src/flog.rs | 2 +- rust/ares/src/trace.rs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/rust/ares/src/flog.rs b/rust/ares/src/flog.rs index dcc9388f..219bbe4f 100644 --- a/rust/ares/src/flog.rs +++ b/rust/ares/src/flog.rs @@ -68,6 +68,6 @@ pub fn flog_fmt(context: &mut Context, fmt: Arguments<'_>) -> Result<()> { #[macro_export] macro_rules! flog { ($ctx:expr, $($arg:tt)*) => { - $crate::flog::flog_fmt($ctx, std::format_args!($($arg)*)) + let _ = $crate::flog::flog_fmt($ctx, std::format_args!($($arg)*)); } } diff --git a/rust/ares/src/trace.rs b/rust/ares/src/trace.rs index 50bc013e..96c10e92 100644 --- a/rust/ares/src/trace.rs +++ b/rust/ares/src/trace.rs @@ -106,7 +106,8 @@ pub fn write_serf_trace_safe(context: &mut Context, name: &str, start: Instant) "\rserf: error writing event trace to file: {:?}", e ); - *(&mut context.trace_info) = None; + let info = &mut context.trace_info; + *info = None; } } From 383b8e6d866d3dc55abf17bb329c1f01d3c768bd Mon Sep 17 00:00:00 2001 From: Matthew LeVan Date: Mon, 18 Mar 2024 09:55:59 -0400 Subject: [PATCH 3/4] serf: flog where possible --- rust/ares/src/serf.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/rust/ares/src/serf.rs b/rust/ares/src/serf.rs index 9a94596f..7a87f1a6 100644 --- a/rust/ares/src/serf.rs +++ b/rust/ares/src/serf.rs @@ -1,5 +1,5 @@ use crate::hamt::Hamt; -use crate::interpreter; +use crate::{flog, interpreter}; use crate::interpreter::{inc, interpret, Error, Mote}; use crate::jets::cold::Cold; use crate::jets::hot::{Hot, HotEntry}; @@ -353,18 +353,18 @@ pub fn serf(constant_hot_state: &[HotEntry]) -> io::Result<()> { tas!(b"live") => { let inner = slot(writ, 6)?.as_direct().unwrap(); match inner.data() { - tas!(b"cram") => eprintln!("\r %cram: not implemented"), + tas!(b"cram") => flog!(&mut context.nock_context, "\r %cram: not implemented"), tas!(b"exit") => { - eprintln!("\r %exit"); + flog!(&mut context.nock_context, "\r %exit"); std::process::exit(0); } tas!(b"save") => { // XX what is eve for? pma_sync(); } - tas!(b"meld") => eprintln!("\r %meld: not implemented"), - tas!(b"pack") => eprintln!("\r %pack: not implemented"), - _ => eprintln!("unknown live"), + tas!(b"meld") => flog!(&mut context.nock_context, "\r %meld: not implemented"), + tas!(b"pack") => flog!(&mut context.nock_context, "\r %pack: not implemented"), + _ => flog!(&mut context.nock_context, "unknown live"), } context.live(); } @@ -607,7 +607,7 @@ fn work_swap(context: &mut Context, job: Noun, goof: Noun) { context.work_swap(ovo, fec); } Err(goof_crud) => { - eprintln!("\rserf: bail"); + flog!(&mut context.nock_context, "\rserf: bail"); let stack = &mut context.nock_context.stack; let lud = T(stack, &[goof_crud, goof, D(0)]); context.work_bail(lud); From bf3b6445c012a3d8aa7138bc059b602fe55d7cfa Mon Sep 17 00:00:00 2001 From: Matthew LeVan Date: Mon, 18 Mar 2024 11:16:45 -0400 Subject: [PATCH 4/4] serf: placate clippy --- rust/ares/src/serf.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/rust/ares/src/serf.rs b/rust/ares/src/serf.rs index 7a87f1a6..be84067b 100644 --- a/rust/ares/src/serf.rs +++ b/rust/ares/src/serf.rs @@ -1,5 +1,4 @@ use crate::hamt::Hamt; -use crate::{flog, interpreter}; use crate::interpreter::{inc, interpret, Error, Mote}; use crate::jets::cold::Cold; use crate::jets::hot::{Hot, HotEntry}; @@ -13,6 +12,7 @@ use crate::noun::{Atom, Cell, DirectAtom, Noun, Slots, D, T}; use crate::persist::pma_meta_set; use crate::persist::{pma_meta_get, pma_open, pma_sync, Persist}; use crate::trace::*; +use crate::{flog, interpreter}; use ares_macros::tas; use signal_hook; use signal_hook::consts::SIGINT; @@ -353,7 +353,9 @@ pub fn serf(constant_hot_state: &[HotEntry]) -> io::Result<()> { tas!(b"live") => { let inner = slot(writ, 6)?.as_direct().unwrap(); match inner.data() { - tas!(b"cram") => flog!(&mut context.nock_context, "\r %cram: not implemented"), + tas!(b"cram") => { + flog!(&mut context.nock_context, "\r %cram: not implemented"); + } tas!(b"exit") => { flog!(&mut context.nock_context, "\r %exit"); std::process::exit(0); @@ -362,9 +364,15 @@ pub fn serf(constant_hot_state: &[HotEntry]) -> io::Result<()> { // XX what is eve for? pma_sync(); } - tas!(b"meld") => flog!(&mut context.nock_context, "\r %meld: not implemented"), - tas!(b"pack") => flog!(&mut context.nock_context, "\r %pack: not implemented"), - _ => flog!(&mut context.nock_context, "unknown live"), + tas!(b"meld") => { + flog!(&mut context.nock_context, "\r %meld: not implemented"); + } + tas!(b"pack") => { + flog!(&mut context.nock_context, "\r %pack: not implemented"); + } + _ => { + flog!(&mut context.nock_context, "unknown live"); + } } context.live(); }