about summary refs log tree commit diff stats
path: root/003trace.cc
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2019-03-03 11:02:11 -0800
committerKartik Agaram <vc@akkartik.com>2019-03-03 11:55:23 -0800
commit8359e5798993b4c2d2b5da7b03b38ead5c0f32ca (patch)
tree5e0d86491f06cc3eb06ddb08fba22e81324afe69 /003trace.cc
parent6bcdfa87a71c00a12a918a0f6e7dad814b6a087a (diff)
downloadmu-8359e5798993b4c2d2b5da7b03b38ead5c0f32ca.tar.gz
4994
Bring back support for incrementally printing the trace to the screen (stderr).

I previously thought I didn't need this as long as I'm always incrementally
saving to the 'last_run' trace file. But I quickly ran into a use for it:
when I want to see a complete trace including switching into the sandbox's
trace and back out again.

So there are now two separate commandline flags:
  --trace to save the trace to file
  --dump to print the trace to screen
The former won't handle sandbox traces. But the latter will.

I'm deemphasizing --dump in the help message since it should be rarely
used.

One other situation where I've used stderr in the past is for just raw
convenience. But trying to always use the trace was a foolish consistency.
Conclusion:
  a) For simple debugging, feel free to just use cout/cerr. Delete them
  before committing.
  b) If the prints get too complex, switch to the trace and browse_trace
  tool.
  c) If using nested sandboxes, emit to stderr, redirect to file, and browse_trace.

I've gone back and forth on these questions in the past; now I'm trying
to be a little more rigorous about capturing reasoning.
Diffstat (limited to '003trace.cc')
-rw-r--r--003trace.cc36
1 files changed, 29 insertions, 7 deletions
diff --git a/003trace.cc b/003trace.cc
index dd60ce42..af707354 100644
--- a/003trace.cc
+++ b/003trace.cc
@@ -109,16 +109,10 @@ ofstream null_stream;  // never opened, so writes to it silently fail
 //: Some constants.
 :(before "struct trace_stream")  // include constants in all cleaved compilation units
 const int Max_depth = 9999;
-// Most important traces are printed to the screen by default
-const int Error_depth = 0;
-:(before "End Globals")
-int Hide_errors = false;  // if set, don't print errors to screen
 :(before "End trace_stream Constructor")
 curr_stream = NULL;
 curr_depth = Max_depth;
 collect_depth = Max_depth;
-:(before "End Reset")
-Hide_errors = false;
 
 :(before "struct trace_stream")
 struct trace_line {
@@ -170,7 +164,7 @@ void trace_stream::newline() {
     past_lines.push_back(trace_line(curr_contents, trim(curr_label), curr_depth));  // preserve indent in contents
     // maybe incrementally dump trace
     trace_line& t = past_lines.back();
-    if (!Hide_errors && curr_depth == Error_depth) {
+    if (should_incrementally_print_trace()) {
       cerr       << std::setw(4) << t.depth << ' ' << t.label << ": " << t.contents << '\n';
     }
     // End trace Commit
@@ -208,6 +202,22 @@ lease_tracer::~lease_tracer() {
 :(before "End Includes")
 #define raise  (!Trace_stream ? (scroll_to_bottom_and_close_console(),++Trace_errors,cerr) /*do print*/ : Trace_stream->stream(Error_depth, "error"))
 
+//: Print errors to the screen by default.
+:(before "struct trace_stream")  // include constants in all cleaved compilation units
+const int Error_depth = 0;
+:(before "End Globals")
+int Hide_errors = false;  // if set, don't print errors to screen
+:(before "End Reset")
+Hide_errors = false;
+:(code)
+bool trace_stream::should_incrementally_print_trace() {
+  if (!Hide_errors && curr_depth == Error_depth) return true;
+  // End Incremental Trace Print Conditions
+  return false;
+}
+:(before "End trace_stream Methods")
+bool should_incrementally_print_trace();
+
 :(before "End Globals")
 int Trace_errors = 0;  // used only when Trace_stream is NULL
 
@@ -448,6 +458,18 @@ string readable_contents(string label) {
   return output.str();
 }
 
+//: Print traces to the screen as they happen.
+//: Particularly useful when juggling multiple trace streams, like when
+//: debugging sandboxes.
+:(before "End Globals")
+bool Dump_trace = false;
+:(before "End Commandline Options(*arg)")
+else if (is_equal(*arg, "--dump")) {
+  Dump_trace = true;
+}
+:(before "End Incremental Trace Print Conditions")
+if (Dump_trace) return true;
+
 //: Miscellaneous helpers.
 
 :(code)