https://github.com/akkartik/mu/blob/master/003trace.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 :(before "End Globals")
72 trace_stream* Trace_stream = NULL;
73
74 :(before "End Types")
75 struct trace_stream {
76 vector<trace_line> past_lines;
77
78
79 trace_stream() {
80
81 }
82 ~trace_stream() {
83
84 }
85
86 };
87
88
89
90
91
92
93 :(before "End Includes")
94
95
96 #define trace(...) !Trace_stream ? cerr : Trace_stream->stream(__VA_ARGS__)
97
98 :(before "End trace_stream Fields")
99
100 ostringstream* curr_stream;
101 string curr_label;
102 int curr_depth;
103
104 int collect_depth;
105 ofstream null_stream;
106
107
108 :(before "struct trace_stream")
109 const int Max_depth = 9999;
110 :(before "End trace_stream Constructor")
111 curr_stream = NULL;
112 curr_depth = Max_depth;
113 collect_depth = Max_depth;
114
115 :(before "struct trace_stream")
116 struct trace_line {
117 string contents;
118 string label;
119 int depth;
120 trace_line(string c, string l) {
121 contents = c;
122 label = l;
123 depth = 0;
124 }
125 trace_line(string c, string l, int d) {
126 contents = c;
127 label = l;
128 depth = d;
129 }
130 };
131
132 string unescape_newline(string& s) {
133 std::stringstream ss;
134 for (int i = 0; i < SIZE(s); ++i) {
135 if (s.at(i) == '\n')
136 ss << "\\n";
137 else
138 ss << s.at(i);
139 }
140 return ss.str();
141 }
142
143 void dump_trace_line(ostream& s, trace_line& t) {
144 s << std::setw(4) << t.depth << ' ' << t.label << ": " << unescape_newline(t.contents) << '\n';
145 }
146
147
148 :(before "End trace_stream Methods")
149 ostream& stream(string label) {
150 return stream(Max_depth, label);
151 }
152
153 ostream& stream(int depth, string label) {
154 if (depth > collect_depth) return null_stream;
155 curr_stream = new ostringstream;
156 curr_label = label;
157 curr_depth = depth;
158 (*curr_stream) << std::hex;
159 return *curr_stream;
160 }
161
162
163 :(before "End Types")
164 struct end {};
165 :(code)
166 ostream& operator<<(ostream& os, end ) {
167 if (Trace_stream) Trace_stream->newline();
168 return os;
169 }
170
171
172 :(before "End Types")
173 struct die {};
174 :(code)
175 ostream& operator<<(ostream& , die ) {
176 if (Trace_stream) Trace_stream->newline();
177 exit(1);
178 }
179
180 :(before "End trace_stream Methods")
181 void newline();
182 :(code)
183 void trace_stream::newline() {
184 if (!curr_stream) return;
185 string curr_contents = curr_stream->str();
186 if (!curr_contents.empty()) {
187 past_lines.push_back(trace_line(curr_contents, trim(curr_label), curr_depth));
188
189 trace_line& t = past_lines.back();
190 if (should_incrementally_print_trace()) {
191 dump_trace_line(cerr, t);
192 }
193
194 if (Dump_trace) past_lines.pop_back();
195
196 }
197
198
199 delete curr_stream;
200 curr_stream = NULL;
201 curr_label.clear();
202 curr_depth = Max_depth;
203 }
204
205
206
207 :(before "End Includes")
208 #define START_TRACING_UNTIL_END_OF_SCOPE lease_tracer leased_tracer;
209 :(before "End Test Setup")
210 START_TRACING_UNTIL_END_OF_SCOPE
211
212
213 :(before "End Types")
214 struct lease_tracer {
215 lease_tracer();
216 ~lease_tracer();
217 };
218 :(code)
219 lease_tracer::lease_tracer() { Trace_stream = new trace_stream; }
220 lease_tracer::~lease_tracer() {
221 delete Trace_stream;
222 Trace_stream = NULL;
223 }
224
225
226
227 :(before "End Includes")
228 #define raise (!Trace_stream ? (++Trace_errors,cerr) : Trace_stream->stream(Error_depth, "error"))
229 #define warn (!Trace_stream ? (++Trace_errors,cerr) : Trace_stream->stream(Warn_depth, "warn"))
230
231
232 :(before "struct trace_stream")
233 const int Error_depth = 0;
234 const int Warn_depth = 1;
235 :(before "End Globals")
236 int Hide_errors = false;
237 int Hide_warnings = false;
238 :(before "End Reset")
239 Hide_errors = false;
240 Hide_warnings = false;
241
242 :(before "End Test Setup")
243 Hide_warnings = true;
244 :(code)
245 bool trace_stream::should_incrementally_print_trace() {
246 if (!Hide_errors && curr_depth == Error_depth) return true;
247 if (!Hide_warnings && !Hide_errors && curr_depth == Warn_depth) return true;
248
249 return false;
250 }
251 :(before "End trace_stream Methods")
252 bool should_incrementally_print_trace();
253
254 :(before "End Globals")
255 int Trace_errors = 0;
256
257
258
259 :(before "End Test Teardown")
260 if (Passed && !Hide_errors && trace_contains_errors()) {
261 Passed = false;
262 }
263 :(code)
264 bool trace_contains_errors() {
265 return Trace_errors > 0 || trace_count("error") > 0;
266 }
267
268 :(before "End Includes")
269
270
271 #define assert_for_now assert
272 #define raise_for_now raise
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291 :(before "End Includes")
292 #define CHECK_TRACE_CONTENTS(...) check_trace_contents(__FUNCTION__, __FILE__, __LINE__, __VA_ARGS__)
293
294 #define CHECK_TRACE_DOESNT_CONTAIN(...) CHECK(trace_doesnt_contain(__VA_ARGS__))
295
296 #define CHECK_TRACE_COUNT(label, count) \
297 if (Passed && trace_count(label) != (count)) { \
298 cerr << "\nF - " << __FUNCTION__ << "(" << __FILE__ << ":" << __LINE__ << "): trace_count of " << label << " should be " << count << '\n'; \
299 cerr << " got " << trace_count(label) << '\n'; \
300 DUMP(label); \
301 Passed = false; \
302 return; \
303 }
304
305 #define CHECK_TRACE_CONTAINS_ERRORS() CHECK(trace_contains_errors())
306 #define CHECK_TRACE_DOESNT_CONTAIN_ERRORS() \
307 if (Passed && trace_contains_errors()) { \
308 cerr << "\nF - " << __FUNCTION__ << "(" << __FILE__ << ":" << __LINE__ << "): unexpected errors\n"; \
309 DUMP("error"); \
310 Passed = false; \
311 return; \
312 }
313
314
315 #define CLEAR_TRACE delete Trace_stream, Trace_stream = new trace_stream
316
317 :(code)
318 bool check_trace_contents(string FUNCTION, string FILE, int LINE, string expected) {
319 if (!Passed) return false;
320 if (!Trace_stream) return false;
321 vector<string> expected_lines = split(expected, "\n");
322 int curr_expected_line = 0;
323 while (curr_expected_line < SIZE(expected_lines) && expected_lines.at(curr_expected_line).empty())
324 ++curr_expected_line;
325 if (curr_expected_line == SIZE(expected_lines)) return true;
326 string label, contents;
327 split_label_contents(expected_lines.at(curr_expected_line), &label, &contents);
328 for (vector<trace_line>::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) {
329 if (label != p->label) continue;
330 string t = trim(p->contents);
331 if (contents != unescape_newline(t)) continue;
332 ++curr_expected_line;
333 while (curr_expected_line < SIZE(expected_lines) && expected_lines.at(curr_expected_line).empty())
334 ++curr_expected_line;
335 if (curr_expected_line == SIZE(expected_lines)) return true;
336 split_label_contents(expected_lines.at(curr_expected_line), &label, &contents);
337 }
338
339 if (line_exists_anywhere(label, contents)) {
340 cerr << "\nF - " << FUNCTION << "(" << FILE << ":" << LINE << "): line [" << label << ": " << contents << "] out of order in trace:\n";
341 DUMP("");
342 }
343 else {
344 cerr << "\nF - " << FUNCTION << "(" << FILE << ":" << LINE << "): missing [" << contents << "] in trace:\n";
345 DUMP(label);
346 }
347 Passed = false;
348 return false;
349 }
350
351 bool trace_doesnt_contain(string expected) {
352 vector<string> tmp = split_first(expected, ": ");
353 if (SIZE(tmp) == 1) {
354 raise << expected << ": missing label or contents in trace line\n" << end();
355 assert(false);
356 }
357 return trace_count(tmp.at(0), tmp.at(1)) == 0;
358 }
359
360 int trace_count(string label) {
361 return trace_count(label, "");
362 }
363
364 int trace_count(string label, string line) {
365 if (!Trace_stream) return 0;
366 long result = 0;
367 for (vector<trace_line>::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) {
368 if (label == p->label) {
369 if (line == "" || trim(line) == trim(p->contents))
370 ++result;
371 }
372 }
373 return result;
374 }
375
376 int trace_count_prefix(string label, string prefix) {
377 if (!Trace_stream) return 0;
378 long result = 0;
379 for (vector<trace_line>::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) {
380 if (label == p->label) {
381 if (starts_with(trim(p->contents), trim(prefix)))
382 ++result;
383 }
384 }
385 return result;
386 }
387
388 void split_label_contents(const string& s, string* label, string* contents) {
389 static const string delim(": ");
390 size_t pos = s.find(delim);
391 if (pos == string::npos) {
392 *label = "";
393 *contents = trim(s);
394 }
395 else {
396 *label = trim(s.substr(0, pos));
397 *contents = trim(s.substr(pos+SIZE(delim)));
398 }
399 }
400
401 bool line_exists_anywhere(const string& label, const string& contents) {
402 for (vector<trace_line>::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) {
403 if (label != p->label) continue;
404 if (contents == trim(p->contents)) return true;
405 }
406 return false;
407 }
408
409 vector<string> split(string s, string delim) {
410 vector<string> result;
411 size_t begin=0, end=s.find(delim);
412 while (true) {
413 if (end == string::npos) {
414 result.push_back(string(s, begin, string::npos));
415 break;
416 }
417 result.push_back(string(s, begin, end-begin));
418 begin = end+SIZE(delim);
419 end = s.find(delim, begin);
420 }
421 return result;
422 }
423
424 vector<string> split_first(string s, string delim) {
425 vector<string> result;
426 size_t end=s.find(delim);
427 result.push_back(string(s, 0, end));
428 if (end != string::npos)
429 result.push_back(string(s, end+SIZE(delim), string::npos));
430 return result;
431 }
432
433
434
435 :(before "End Includes")
436
437 #define DUMP(label) if (Trace_stream) cerr << Trace_stream->readable_contents(label);
438
439
440
441 #define dbg trace(0, "a")
442
443
444
445
446
447 :(before "End Globals")
448 ofstream Trace_file;
449 :(before "End Commandline Options(*arg)")
450 else if (is_equal(*arg, "--trace")) {
451 cerr << "saving trace to 'last_run'\n";
452 Trace_file.open("last_run");
453
454
455 Trace_file << " 0 dummy: start\n";
456 }
457 :(before "End trace Commit")
458 if (Trace_file) {
459 dump_trace_line(Trace_file, t);
460 }
461 :(before "End One-time Setup")
462 atexit(cleanup_main);
463 :(code)
464 void cleanup_main() {
465 if (Trace_file) Trace_file.close();
466
467 }
468
469 :(before "End trace_stream Methods")
470 string readable_contents(string label) {
471 string trim(const string& s);
472 ostringstream output;
473 label = trim(label);
474 for (vector<trace_line>::iterator p = past_lines.begin(); p != past_lines.end(); ++p)
475 if (label.empty() || label == p->label)
476 dump_trace_line(output, *p);
477 return output.str();
478 }
479
480
481
482
483 :(before "End Globals")
484 bool Dump_trace = false;
485 :(before "End Commandline Options(*arg)")
486 else if (is_equal(*arg, "--dump")) {
487 Dump_trace = true;
488 }
489 :(before "End Incremental Trace Print Conditions")
490 if (Dump_trace) return true;
491
492
493
494 :(code)
495 string trim(const string& s) {
496 string::const_iterator first = s.begin();
497 while (first != s.end() && isspace(*first))
498 ++first;
499 if (first == s.end()) return "";
500
501 string::const_iterator last = --s.end();
502 while (last != s.begin() && isspace(*last))
503 --last;
504 ++last;
505 return string(first, last);
506 }
507
508 :(before "End Includes")
509 #include <vector>
510 using std::vector;
511 #include <list>
512 using std::list;
513 #include <set>
514 using std::set;
515
516 #include <sstream>
517 using std::istringstream;
518 using std::ostringstream;
519
520 #include <fstream>
521 using std::ifstream;
522 using std::ofstream;