about summary refs log tree commit diff
path: root/tvix/eval/src/observer.rs
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2023-03-03T18·23+0300
committertazjin <tazjin@tvl.su>2023-03-13T20·30+0000
commitfb4ea1f5a40d086cdb6fa5ede52adf21bb82082f (patch)
tree3e47c9ad084a693a5b00f01fef60063cd34c3b77 /tvix/eval/src/observer.rs
parenteef48b8f1f3c7ae37a793bfef22c276dfd69767c (diff)
refactor(tvix/eval): condense observer's stack writing logic r/5971
Change-Id: I1282c3387ac1e0d1528b894814f2a495ca5a6a32
Reviewed-on: https://cl.tvl.fyi/c/depot/+/8199
Tested-by: BuildkiteCI
Reviewed-by: raitobezarius <tvl@lahfa.xyz>
Diffstat (limited to 'tvix/eval/src/observer.rs')
-rw-r--r--tvix/eval/src/observer.rs77
1 files changed, 24 insertions, 53 deletions
diff --git a/tvix/eval/src/observer.rs b/tvix/eval/src/observer.rs
index 7dc3ac1cd6d6..b26d0ff9e352 100644
--- a/tvix/eval/src/observer.rs
+++ b/tvix/eval/src/observer.rs
@@ -149,6 +149,16 @@ impl<W: Write> TracingObserver<W> {
             writer: TabWriter::new(writer),
         }
     }
+
+    fn write_stack(&mut self, stack: &[Value]) {
+        let _ = write!(&mut self.writer, "[ ");
+
+        for val in stack {
+            let _ = write!(&mut self.writer, "{} ", val);
+        }
+
+        let _ = writeln!(&mut self.writer, "]");
+    }
 }
 
 impl<W: Write> RuntimeObserver for TracingObserver<W> {
@@ -179,69 +189,40 @@ impl<W: Write> RuntimeObserver for TracingObserver<W> {
 
     /// Called when the runtime exits a call frame.
     fn observe_exit_call_frame(&mut self, frame_at: usize, stack: &[Value]) {
-        let _ = write!(&mut self.writer, "=== exiting frame {} ===\t[ ", frame_at);
-
-        for val in stack {
-            let _ = write!(&mut self.writer, "{} ", val);
-        }
-
-        let _ = writeln!(&mut self.writer, "]");
+        let _ = write!(&mut self.writer, "=== exiting frame {} ===\t ", frame_at);
+        self.write_stack(stack);
     }
 
     fn observe_suspend_call_frame(&mut self, frame_at: usize, stack: &[Value]) {
-        let _ = write!(
-            &mut self.writer,
-            "=== suspending frame {} ===\t[ ",
-            frame_at
-        );
+        let _ = write!(&mut self.writer, "=== suspending frame {} ===\t", frame_at);
 
-        for val in stack {
-            let _ = write!(&mut self.writer, "{} ", val);
-        }
-
-        let _ = writeln!(&mut self.writer, "]");
+        self.write_stack(stack);
     }
 
     fn observe_enter_generator(&mut self, frame_at: usize, stack: &[Value]) {
         let _ = write!(
             &mut self.writer,
-            "=== entering generator frame {} ===\t[ ",
+            "=== entering generator frame {} ===\t",
             frame_at
         );
 
-        for val in stack {
-            let _ = write!(&mut self.writer, "{} ", val);
-        }
-
-        let _ = writeln!(&mut self.writer, "]");
+        self.write_stack(stack);
     }
 
     fn observe_exit_generator(&mut self, frame_at: usize, stack: &[Value]) {
-        let _ = write!(
-            &mut self.writer,
-            "=== exiting generator {} ===\t[ ",
-            frame_at
-        );
-
-        for val in stack {
-            let _ = write!(&mut self.writer, "{} ", val);
-        }
+        let _ = write!(&mut self.writer, "=== exiting generator {} ===\t", frame_at);
 
-        let _ = writeln!(&mut self.writer, "]");
+        self.write_stack(stack);
     }
 
     fn observe_suspend_generator(&mut self, frame_at: usize, stack: &[Value]) {
         let _ = write!(
             &mut self.writer,
-            "=== suspending generator {} ===\t[ ",
+            "=== suspending generator {} ===\t",
             frame_at
         );
 
-        for val in stack {
-            let _ = write!(&mut self.writer, "{} ", val);
-        }
-
-        let _ = writeln!(&mut self.writer, "]");
+        self.write_stack(stack);
     }
 
     fn observe_generator_request(&mut self, msg: &GeneratorRequest) {
@@ -253,13 +234,8 @@ impl<W: Write> RuntimeObserver for TracingObserver<W> {
     }
 
     fn observe_exit_builtin(&mut self, name: &'static str, stack: &[Value]) {
-        let _ = write!(&mut self.writer, "=== exiting builtin {} ===\t[ ", name);
-
-        for val in stack {
-            let _ = write!(&mut self.writer, "{} ", val);
-        }
-
-        let _ = writeln!(&mut self.writer, "]");
+        let _ = write!(&mut self.writer, "=== exiting builtin {} ===\t", name);
+        self.write_stack(stack);
     }
 
     fn observe_tail_call(&mut self, frame_at: usize, lambda: &Rc<Lambda>) {
@@ -271,13 +247,8 @@ impl<W: Write> RuntimeObserver for TracingObserver<W> {
     }
 
     fn observe_execute_op(&mut self, ip: CodeIdx, op: &OpCode, stack: &[Value]) {
-        let _ = write!(&mut self.writer, "{:04} {:?}\t[ ", ip.0, op);
-
-        for val in stack {
-            let _ = write!(&mut self.writer, "{} ", val);
-        }
-
-        let _ = writeln!(&mut self.writer, "]");
+        let _ = write!(&mut self.writer, "{:04} {:?}\t", ip.0, op);
+        self.write_stack(stack);
     }
 }