about summary refs log tree commit diff stats
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
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.
-rw-r--r--001help.cc3
-rw-r--r--003trace.cc36
-rw-r--r--101run_sandboxed.cc2
-rw-r--r--subx/003trace.cc50
4 files changed, 69 insertions, 22 deletions
diff --git a/001help.cc b/001help.cc
index 3cab06d9..78877561 100644
--- a/001help.cc
+++ b/001help.cc
@@ -39,6 +39,9 @@ if (argc <= 1 || is_equal(argv[1], "--help")) {
        << "  To see where a mu program is spending its time:\n"
        << "    mu --profile file_or_dir1 file_or_dir2 ...\n"
        << "  this slices and dices time spent in various profile.* output files\n"
+       << "  To print out the trace to stderr:\n"
+       << "    mu --dump file1.mu file2.mu ...\n"
+       << "  this is handy when you want to see sandboxed traces alongside the main one\n"
        << "\n"
        << "  To browse a trace generated by a previous run:\n"
        << "    mu browse-trace file\n"
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)
diff --git a/101run_sandboxed.cc b/101run_sandboxed.cc
index f054cd8b..4a897d3a 100644
--- a/101run_sandboxed.cc
+++ b/101run_sandboxed.cc
@@ -163,7 +163,7 @@ void run_code_begin(bool should_stash_snapshots) {
   Save_callstack_depth = Callstack_depth;
   Callstack_depth = Initial_callstack_depth;
   Trace_stream = new trace_stream;
-  Trace_stream->collect_depth = App_depth;
+  Trace_stream->collect_depth = Save_trace_stream->collect_depth;
 }
 
 void run_code_end() {
diff --git a/subx/003trace.cc b/subx/003trace.cc
index 9aba3f12..bb614c66 100644
--- a/subx/003trace.cc
+++ b/subx/003trace.cc
@@ -109,22 +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;
-const int Warn_depth = 1;
-:(before "End Globals")
-int Hide_errors = false;  // if set, don't print errors or warnings to screen
-int Hide_warnings = false;  // if set, don't print warnings to screen
 :(before "End trace_stream Constructor")
 curr_stream = NULL;
 curr_depth = Max_depth;
 collect_depth = Max_depth;
-:(before "End Reset")
-Hide_errors = false;
-Hide_warnings = false;
-//: Never dump warnings in scenarios
-:(before "End Test Setup")
-Hide_warnings = true;
 
 :(before "struct trace_stream")
 struct trace_line {
@@ -177,8 +165,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)
-        || (!Hide_warnings && !Hide_errors && curr_depth == Warn_depth)) {
+    if (should_incrementally_print_trace()) {
       cerr       << std::setw(4) << t.depth << ' ' << t.label << ": " << t.contents << '\n';
     }
     // End trace Commit
@@ -217,6 +204,29 @@ lease_tracer::~lease_tracer() {
 #define raise  (!Trace_stream ? (++Trace_errors,cerr) /*do print*/ : Trace_stream->stream(Error_depth, "error"))
 #define warn (!Trace_stream ? (++Trace_errors,cerr) /*do print*/ : Trace_stream->stream(Warn_depth, "warn"))
 
+//: Print errors and warnings to the screen by default.
+:(before "struct trace_stream")  // include constants in all cleaved compilation units
+const int Error_depth = 0;
+const int Warn_depth = 1;
+:(before "End Globals")
+int Hide_errors = false;  // if set, don't print errors or warnings to screen
+int Hide_warnings = false;  // if set, don't print warnings to screen
+:(before "End Reset")
+Hide_errors = false;
+Hide_warnings = false;
+//: Never dump warnings in scenarios
+:(before "End Test Setup")
+Hide_warnings = true;
+:(code)
+bool trace_stream::should_incrementally_print_trace() {
+  if (!Hide_errors && curr_depth == Error_depth) return true;
+  if (!Hide_warnings && !Hide_errors && curr_depth == Warn_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
 
@@ -442,6 +452,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)