From 1a4de9dd58201bb57a07ea931d1764064fc52e64 Mon Sep 17 00:00:00 2001 From: Kartik Agaram Date: Sat, 22 Sep 2018 00:32:03 -0700 Subject: 4588 --- html/003trace.cc.html | 376 +++++++++++++++++++++++++------------------------- 1 file changed, 190 insertions(+), 186 deletions(-) (limited to 'html/003trace.cc.html') diff --git a/html/003trace.cc.html b/html/003trace.cc.html index 3c8eef8e..e06c043e 100644 --- a/html/003trace.cc.html +++ b/html/003trace.cc.html @@ -135,112 +135,112 @@ if ('onhashchange' in window) { 76 trace_line(int d, string l, string c) :depth(d), label(l), contents(c) {} 77 }; 78 - 79 :(before "End Globals") - 80 bool Hide_errors = false; // if set, don't print even error trace lines to screen - 81 bool Dump_trace = false; // if set, print trace lines to screen - 82 string Dump_label = ""; // if set, print trace lines matching a single label to screen - 83 :(before "End Reset") - 84 Hide_errors = false; - 85 Dump_trace = false; - 86 Dump_label = ""; - 87 - 88 //: Support for tracing an entire run. - 89 //: Traces can have a lot of overhead, so only turn them on when asked. - 90 :(before "End Commandline Options(*arg)") - 91 else if (is_equal(*arg, "--trace")) { - 92 Save_trace = true; - 93 } - 94 :(before "End Commandline Parsing") - 95 if (Save_trace) { - 96 cerr << "initializing trace\n"; - 97 Trace_stream = new trace_stream; - 98 } - 99 :(code) -100 void cleanup_main() { -101 if (!Trace_stream) return; -102 if (Save_trace) -103 Trace_stream->save(); -104 delete Trace_stream; -105 Trace_stream = NULL; -106 } -107 :(before "End One-time Setup") -108 atexit(cleanup_main); -109 -110 :(before "End Types") -111 // Pre-define some global constants that trace_stream needs to know about. -112 // Since they're in the Types section, they'll be included in any cleaved -113 // compilation units. So no extern linkage. -114 const int Max_depth = 9999; -115 const int Error_depth = 0; // definitely always print errors -116 const int App_depth = 2; // temporarily where all Mu code will trace to -117 -118 struct trace_stream { -119 vector<trace_line> past_lines; -120 // accumulator for current line -121 ostringstream* curr_stream; -122 string curr_label; -123 int curr_depth; -124 int callstack_depth; -125 int collect_depth; -126 ofstream null_stream; // never opens a file, so writes silently fail -127 trace_stream() :curr_stream(NULL), curr_depth(Max_depth), callstack_depth(0), collect_depth(Max_depth) {} -128 ~trace_stream() { if (curr_stream) delete curr_stream; } -129 -130 ostream& stream(string label) { -131 return stream(Max_depth, label); -132 } -133 -134 ostream& stream(int depth, string label) { -135 if (depth > collect_depth) return null_stream; -136 curr_stream = new ostringstream; -137 curr_label = label; -138 curr_depth = depth; -139 return *curr_stream; -140 } -141 -142 void save() { -143 cerr << "saving trace to 'last_run'\n"; -144 ofstream fout("last_run"); -145 fout << readable_contents(""); -146 fout.close(); -147 } -148 -149 // be sure to call this before messing with curr_stream or curr_label -150 void newline(); -151 // useful for debugging -152 string readable_contents(string label); // empty label = show everything -153 }; -154 -155 :(code) -156 void trace_stream::newline() { -157 if (!curr_stream) return; -158 string curr_contents = curr_stream->str(); -159 if (!curr_contents.empty()) { -160 past_lines.push_back(trace_line(curr_depth, trim(curr_label), curr_contents)); // preserve indent in contents -161 if ((!Hide_errors && curr_label == "error") -162 || Dump_trace -163 || (!Dump_label.empty() && curr_label == Dump_label)) -164 cerr << curr_label << ": " << curr_contents << '\n'; -165 } -166 delete curr_stream; -167 curr_stream = NULL; -168 curr_label.clear(); -169 curr_depth = Max_depth; -170 } -171 -172 string trace_stream::readable_contents(string label) { -173 ostringstream output; -174 label = trim(label); -175 for (vector<trace_line>::iterator p = past_lines.begin(); p != past_lines.end(); ++p) -176 if (label.empty() || label == p->label) { -177 output << std::setw(4) << p->depth << ' ' << p->label << ": " << p->contents << '\n'; -178 } -179 return output.str(); -180 } -181 -182 :(before "End Globals") -183 trace_stream* Trace_stream = NULL; -184 int Trace_errors = 0; // used only when Trace_stream is NULL + 79 //: Support for tracing an entire run. + 80 //: Traces can have a lot of overhead, so only turn them on when asked. + 81 :(before "End Commandline Options(*arg)") + 82 else if (is_equal(*arg, "--trace")) { + 83 Save_trace = true; + 84 } + 85 :(before "End Commandline Parsing") + 86 if (Save_trace) { + 87 cerr << "initializing trace\n"; + 88 Trace_stream = new trace_stream; + 89 } + 90 :(code) + 91 void cleanup_main() { + 92 if (!Trace_stream) return; + 93 if (Save_trace) + 94 Trace_stream->save(); + 95 delete Trace_stream; + 96 Trace_stream = NULL; + 97 } + 98 :(before "End One-time Setup") + 99 atexit(cleanup_main); +100 +101 :(before "End Types") +102 // Pre-define some global constants that trace_stream needs to know about. +103 // Since they're in the Types section, they'll be included in any cleaved +104 // compilation units. So no extern linkage. +105 const int Max_depth = 9999; +106 const int Error_depth = 0; // definitely always print errors +107 const int App_depth = 2; // temporarily where all Mu code will trace to +108 +109 struct trace_stream { +110 vector<trace_line> past_lines; +111 // accumulator for current line +112 ostringstream* curr_stream; +113 string curr_label; +114 int curr_depth; +115 int callstack_depth; +116 int collect_depth; +117 ofstream null_stream; // never opens a file, so writes silently fail +118 trace_stream() :curr_stream(NULL), curr_depth(Max_depth), callstack_depth(0), collect_depth(Max_depth) {} +119 ~trace_stream() { if (curr_stream) delete curr_stream; } +120 +121 ostream& stream(string label) { +122 return stream(Max_depth, label); +123 } +124 +125 ostream& stream(int depth, string label) { +126 if (depth > collect_depth) return null_stream; +127 curr_stream = new ostringstream; +128 curr_label = label; +129 curr_depth = depth; +130 return *curr_stream; +131 } +132 +133 void save() { +134 cerr << "saving trace to 'last_run'\n"; +135 ofstream fout("last_run"); +136 fout << readable_contents(""); +137 fout.close(); +138 } +139 +140 // be sure to call this before messing with curr_stream or curr_label +141 void newline(); +142 // useful for debugging +143 string readable_contents(string label); // empty label = show everything +144 }; +145 +146 :(code) +147 void trace_stream::newline() { +148 if (!curr_stream) return; +149 string curr_contents = curr_stream->str(); +150 if (!curr_contents.empty()) { +151 past_lines.push_back(trace_line(curr_depth, trim(curr_label), curr_contents)); // preserve indent in contents +152 if ((!Hide_errors && curr_label == "error") +153 || Dump_trace +154 || (!Dump_label.empty() && curr_label == Dump_label)) +155 cerr << curr_label << ": " << curr_contents << '\n'; +156 } +157 delete curr_stream; +158 curr_stream = NULL; +159 curr_label.clear(); +160 curr_depth = Max_depth; +161 } +162 +163 string trace_stream::readable_contents(string label) { +164 ostringstream output; +165 label = trim(label); +166 for (vector<trace_line>::iterator p = past_lines.begin(); p != past_lines.end(); ++p) +167 if (label.empty() || label == p->label) { +168 output << std::setw(4) << p->depth << ' ' << p->label << ": " << p->contents << '\n'; +169 } +170 return output.str(); +171 } +172 +173 :(before "End Globals") +174 trace_stream* Trace_stream = NULL; +175 int Trace_errors = 0; // used only when Trace_stream is NULL +176 +177 :(before "End Globals") +178 bool Hide_errors = false; // if set, don't print even error trace lines to screen +179 bool Dump_trace = false; // if set, print trace lines to screen +180 string Dump_label = ""; // if set, print trace lines matching a single label to screen +181 :(before "End Reset") +182 Hide_errors = false; +183 Dump_trace = false; // toggle this to print traces to screen as they are emitted +184 Dump_label = ""; 185 186 :(before "End Includes") 187 #define CLEAR_TRACE delete Trace_stream, Trace_stream = new trace_stream; @@ -285,7 +285,7 @@ if ('onhashchange' in window) { 226 struct end {}; 227 :(code) 228 ostream& operator<<(ostream& os, end /*unused*/) { -229 if (Trace_stream) Trace_stream->newline(); +229 if (Trace_stream) Trace_stream->newline(); 230 return os; 231 } 232 @@ -301,7 +301,7 @@ if ('onhashchange' in window) { 242 :(code) 243 lease_tracer::lease_tracer() { Trace_stream = new trace_stream; } 244 lease_tracer::~lease_tracer() { -245 if (Save_trace) Trace_stream->save(); +245 if (Save_trace) Trace_stream->save(); 246 delete Trace_stream, Trace_stream = NULL; 247 } 248 :(before "End Includes") @@ -336,7 +336,7 @@ if ('onhashchange' in window) { 277 bool check_trace_contents(string FUNCTION, string FILE, int LINE, string expected) { 278 if (!Passed) return false; 279 if (!Trace_stream) return false; -280 vector<string> expected_lines = split(expected, "^D"); +280 vector<string> expected_lines = split(expected, "^D"); 281 int curr_expected_line = 0; 282 while (curr_expected_line < SIZE(expected_lines) && expected_lines.at(curr_expected_line).empty()) 283 ++curr_expected_line; @@ -345,7 +345,7 @@ if ('onhashchange' in window) { 286 split_label_contents(expected_lines.at(curr_expected_line), &label, &contents); 287 for (vector<trace_line>::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) { 288 if (label != p->label) continue; -289 if (contents != trim(p->contents)) continue; +289 if (contents != trim(p->contents)) continue; 290 ++curr_expected_line; 291 while (curr_expected_line < SIZE(expected_lines) && expected_lines.at(curr_expected_line).empty()) 292 ++curr_expected_line; @@ -370,18 +370,18 @@ if ('onhashchange' in window) { 311 size_t pos = s.find(delim); 312 if (pos == string::npos) { 313 *label = ""; -314 *contents = trim(s); +314 *contents = trim(s); 315 } 316 else { -317 *label = trim(s.substr(0, pos)); -318 *contents = trim(s.substr(pos+SIZE(delim))); +317 *label = trim(s.substr(0, pos)); +318 *contents = trim(s.substr(pos+SIZE(delim))); 319 } 320 } 321 322 bool line_exists_anywhere(const string& label, const string& contents) { 323 for (vector<trace_line>::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) { 324 if (label != p->label) continue; -325 if (contents == trim(p->contents)) return true; +325 if (contents == trim(p->contents)) return true; 326 } 327 return false; 328 } @@ -395,7 +395,7 @@ if ('onhashchange' in window) { 336 long result = 0; 337 for (vector<trace_line>::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) { 338 if (label == p->label) { -339 if (line == "" || trim(line) == trim(p->contents)) +339 if (line == "" || trim(line) == trim(p->contents)) 340 ++result; 341 } 342 } @@ -407,7 +407,7 @@ if ('onhashchange' in window) { 348 long result = 0; 349 for (vector<trace_line>::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) { 350 if (label == p->label) { -351 if (starts_with(trim(p->contents), trim(prefix))) +351 if (starts_with(trim(p->contents), trim(prefix))) 352 ++result; 353 } 354 } @@ -419,78 +419,82 @@ if ('onhashchange' in window) { 360 } 361 362 bool trace_doesnt_contain(string expected) { -363 vector<string> tmp = split_first(expected, ": "); -364 return trace_doesnt_contain(tmp.at(0), tmp.at(1)); -365 } -366 -367 vector<string> split(string s, string delim) { -368 vector<string> result; -369 size_t begin=0, end=s.find(delim); -370 while (true) { -371 if (end == string::npos) { -372 result.push_back(string(s, begin, string::npos)); -373 break; -374 } -375 result.push_back(string(s, begin, end-begin)); -376 begin = end+SIZE(delim); -377 end = s.find(delim, begin); -378 } -379 return result; -380 } -381 -382 vector<string> split_first(string s, string delim) { -383 vector<string> result; -384 size_t end=s.find(delim); -385 result.push_back(string(s, 0, end)); -386 if (end != string::npos) -387 result.push_back(string(s, end+SIZE(delim), string::npos)); -388 return result; -389 } -390 -391 string trim(const string& s) { -392 string::const_iterator first = s.begin(); -393 while (first != s.end() && isspace(*first)) -394 ++first; -395 if (first == s.end()) return ""; -396 -397 string::const_iterator last = --s.end(); -398 while (last != s.begin() && isspace(*last)) -399 --last; -400 ++last; -401 return string(first, last); -402 } -403 -404 :(before "End Includes") -405 #include <vector> -406 using std::vector; -407 #include <list> -408 using std::list; -409 #include <set> -410 using std::set; -411 -412 #include <sstream> -413 using std::istringstream; -414 using std::ostringstream; +363 vector<string> tmp = split_first(expected, ": "); +364 if (SIZE(tmp) == 1) { +365 raise << expected << ": missing label or contents in trace line\n" << end(); +366 assert(false); +367 } +368 return trace_doesnt_contain(tmp.at(0), tmp.at(1)); +369 } +370 +371 vector<string> split(string s, string delim) { +372 vector<string> result; +373 size_t begin=0, end=s.find(delim); +374 while (true) { +375 if (end == string::npos) { +376 result.push_back(string(s, begin, string::npos)); +377 break; +378 } +379 result.push_back(string(s, begin, end-begin)); +380 begin = end+SIZE(delim); +381 end = s.find(delim, begin); +382 } +383 return result; +384 } +385 +386 vector<string> split_first(string s, string delim) { +387 vector<string> result; +388 size_t end=s.find(delim); +389 result.push_back(string(s, 0, end)); +390 if (end != string::npos) +391 result.push_back(string(s, end+SIZE(delim), string::npos)); +392 return result; +393 } +394 +395 string trim(const string& s) { +396 string::const_iterator first = s.begin(); +397 while (first != s.end() && isspace(*first)) +398 ++first; +399 if (first == s.end()) return ""; +400 +401 string::const_iterator last = --s.end(); +402 while (last != s.begin() && isspace(*last)) +403 --last; +404 ++last; +405 return string(first, last); +406 } +407 +408 :(before "End Includes") +409 #include <vector> +410 using std::vector; +411 #include <list> +412 using std::list; +413 #include <set> +414 using std::set; 415 -416 #include <fstream> -417 using std::ifstream; -418 using std::ofstream; +416 #include <sstream> +417 using std::istringstream; +418 using std::ostringstream; 419 -420 #include "termbox/termbox.h" -421 -422 :(before "End Globals") -423 //: In future layers we'll use the depth field as follows: -424 //: -425 //: Errors will be depth 0. -426 //: Mu 'applications' will be able to use depths 1-100 as they like. -427 //: Primitive statements will occupy 101-9989 -428 extern const int Initial_callstack_depth = 101; -429 extern const int Max_callstack_depth = 9989; -430 //: Finally, details of primitive Mu statements will occupy depth 9990-9999 -431 //: (more on that later as well) -432 //: -433 //: This framework should help us hide some details at each level, mixing -434 //: static ideas like layers with the dynamic notion of call-stack depth. +420 #include <fstream> +421 using std::ifstream; +422 using std::ofstream; +423 +424 #include "termbox/termbox.h" +425 +426 :(before "End Globals") +427 //: In future layers we'll use the depth field as follows: +428 //: +429 //: Errors will be depth 0. +430 //: Mu 'applications' will be able to use depths 1-100 as they like. +431 //: Primitive statements will occupy 101-9989 +432 extern const int Initial_callstack_depth = 101; +433 extern const int Max_callstack_depth = 9989; +434 //: Finally, details of primitive Mu statements will occupy depth 9990-9999 +435 //: (more on that later as well) +436 //: +437 //: This framework should help us hide some details at each level, mixing +438 //: static ideas like layers with the dynamic notion of call-stack depth. -- cgit 1.4.1-2-gfad0