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.
This commit is contained in:
Kartik Agaram 2019-03-03 11:02:11 -08:00
parent 6bcdfa87a7
commit 8359e57989
4 changed files with 69 additions and 22 deletions

View File

@ -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"

View File

@ -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)

View File

@ -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() {

View File

@ -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)