https://github.com/akkartik/mu/blob/master/subx/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 :(before "End trace_stream Methods")
172 void newline();
173 :(code)
174 void trace_stream::newline() {
175 if (!curr_stream) return;
176 string curr_contents = curr_stream->str();
177 if (!curr_contents.empty()) {
178 past_lines.push_back(trace_line(curr_contents, trim(curr_label), curr_depth));
179
180 trace_line& t = past_lines.back();
181 if (should_incrementally_print_trace()) {
182 dump_trace_line(cerr, t);
183 }
184
185 }
186
187
188 delete curr_stream;
189 curr_stream = NULL;
190 curr_label.clear();
191 curr_depth = Max_depth;
192 }
193
194
195
196 :(before "End Includes")
197 #define START_TRACING_UNTIL_END_OF_SCOPE lease_tracer leased_tracer;
198 :(before "End Test Setup")
199 START_TRACING_UNTIL_END_OF_SCOPE
200
201
202 :(before "End Types")
203 struct lease_tracer {
204 lease_tracer();
205 ~lease_tracer();
206 };
207 :(code)
208 lease_tracer::lease_tracer() { Trace_stream = new trace_stream; }
209 lease_tracer::~lease_tracer() {
210 delete Trace_stream;
211 Trace_stream = NULL;
212 }
213
214
215
216 :(before "End Includes")
217 #define raise (!Trace_stream ? (++Trace_errors,cerr) : Trace_stream->stream(Error_depth, "error"))
218 #define warn (!Trace_stream ? (++Trace_errors,cerr) : Trace_stream->stream(Warn_depth, "warn"))
219
220
221 :(before "struct trace_stream")
222 const int Error_depth = 0;
223 const int Warn_depth = 1;
224 :(before "End Globals")
225 int Hide_errors = false;
226 int Hide_warnings = false;
227 :(before "End Reset")
228 Hide_errors = false;
229 Hide_warnings = false;
230
231 :(before "End Test Setup")
232 Hide_warnings = true;
233 :(code)
234 bool trace_stream::should_incrementally_print_trace() {
235 if (!Hide_errors && curr_depth == Error_depth) return true;
236 if (!Hide_warnings && !Hide_errors && curr_depth == Warn_depth) return true;
237
238 return false;
239 }
240 :(before "End trace_stream Methods")
241 bool should_incrementally_print_trace();
242
243 :(before "End Globals")
244 int Trace_errors = 0;
245
246
247
248 :(before "End Test Teardown")
249 if (Passed && !Hide_errors && trace_contains_errors()) {
250 Passed = false;
251 }
252 :(code)
253 bool trace_contains_errors() {
254 return Trace_errors > 0 || trace_count("error") > 0;
255 }
256
257 :(before "End Includes")
258
259
260 #define assert_for_now assert
261 #define raise_for_now raise
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280 :(before "End Includes")
281 #define CHECK_TRACE_CONTENTS(...) check_trace_contents(__FUNCTION__, __FILE__, __LINE__, __VA_ARGS__)
282
283 #define CHECK_TRACE_DOESNT_CONTAIN(...) CHECK(trace_doesnt_contain(__VA_ARGS__))
284
285 #define CHECK_TRACE_COUNT(label, count) \
286 if (Passed && trace_count(label) != (count)) { \
287 cerr << "\nF - " << __FUNCTION__ << "(" << __FILE__ << ":" << __LINE__ << "): trace_count of " << label << " should be " << count << '\n'; \
288 cerr << " got " << trace_count(label) << '\n'; \
289 DUMP(label); \
290 Passed = false; \
291 return; \
292 }
293
294 #define CHECK_TRACE_CONTAINS_ERRORS() CHECK(trace_contains_errors())
295 #define CHECK_TRACE_DOESNT_CONTAIN_ERRORS() \
296 if (Passed && trace_contains_errors()) { \
297 cerr << "\nF - " << __FUNCTION__ << "(" << __FILE__ << ":" << __LINE__ << "): unexpected errors\n"; \
298 DUMP("error"); \
299 Passed = false; \
300 return; \
301 }
302
303
304 #define CLEAR_TRACE delete Trace_stream, Trace_stream = new trace_stream
305
306 :(code)
307 bool check_trace_contents(string FUNCTION, string FILE, int LINE, string expected) {
308 if (!Passed) return false;
309 if (!Trace_stream) return false;
310 vector<string> expected_lines = split(expected, "\n");
311 int curr_expected_line = 0;
312 while (curr_expected_line < SIZE(expected_lines) && expected_lines.at(curr_expected_line).empty())
313 ++curr_expected_line;
314 if (curr_expected_line == SIZE(expected_lines)) return true;
315 string label, contents;
316 split_label_contents(expected_lines.at(curr_expected_line), &label, &contents);
317 for (vector<trace_line>::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) {
318 if (label != p->label) continue;
319 string t = trim(p->contents);
320 if (contents != unescape_newline(t)) continue;
321 ++curr_expected_line;
322 while (curr_expected_line < SIZE(expected_lines) && expected_lines.at(curr_expected_line).empty())
323 ++curr_expected_line;
324 if (curr_expected_line == SIZE(expected_lines)) return true;
325 split_label_contents(expected_lines.at(curr_expected_line), &label, &contents);
326 }
327
328 if (line_exists_anywhere(label, contents)) {
329 cerr << "\nF - " << FUNCTION << "(" << FILE << ":" << LINE << "): line [" << label << ": " << contents << "] out of order in trace:\n";
330 DUMP("");
331 }
332 else {
333 cerr << "\nF - " << FUNCTION << "(" << FILE << ":" << LINE << "): missing [" << contents << "] in trace:\n";
334 DUMP(label);
335 }
336 Passed = false;
337 return false;
338 }
339
340 bool trace_doesnt_contain(string expected) {
341 vector<string> tmp = split_first(expected, ": ");
342 if (SIZE(tmp) == 1) {
343 raise << expected << ": missing label or contents in trace line\n" << end();
344 assert(false);
345 }
346 return trace_count(tmp.at(0), tmp.at(1)) == 0;
347 }
348
349 int trace_count(string label) {
350 return trace_count(label, "");
351 }
352
353 int trace_count(string label, string line) {
354 if (!Trace_stream) return 0;
355 long result = 0;
356 for (vector<trace_line>::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) {
357 if (label == p->label) {
358 if (line == "" || trim(line) == trim(p->contents))
359 ++result;
360 }
361 }
362 return result;
363 }
364
365 int trace_count_prefix(string label, string prefix) {
366 if (!Trace_stream) return 0;
367 long result = 0;
368 for (vector<trace_line>::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) {
369 if (label == p->label) {
370 if (starts_with(trim(p->contents), trim(prefix)))
371 ++result;
372 }
373 }
374 return result;
375 }
376
377 void split_label_contents(const string& s, string* label, string* contents) {
378 static const string delim(": ");
379 size_t pos = s.find(delim);
380 if (pos == string::npos) {
381 *label = "";
382 *contents = trim(s);
383 }
384 else {
385 *label = trim(s.substr(0, pos));
386 *contents = trim(s.substr(pos+SIZE(delim)));
387 }
388 }
389
390 bool line_exists_anywhere(const string& label, const string& contents) {
391 for (vector<trace_line>::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) {
392 if (label != p->label) continue;
393 if (contents == trim(p->contents)) return true;
394 }
395 return false;
396 }
397
398 vector<string> split(string s, string delim) {
399 vector<string> result;
400 size_t begin=0, end=s.find(delim);
401 while (true) {
402 if (end == string::npos) {
403 result.push_back(string(s, begin, string::npos));
404 break;
405 }
406 result.push_back(string(s, begin, end-begin));
407 begin = end+SIZE(delim);
408 end = s.find(delim, begin);
409 }
410 return result;
411 }
412
413 vector<string> split_first(string s, string delim) {
414 vector<string> result;
415 size_t end=s.find(delim);
416 result.push_back(string(s, 0, end));
417 if (end != string::npos)
418 result.push_back(string(s, end+SIZE(delim), string::npos));
419 return result;
420 }
421
422
423
424 :(before "End Includes")
425
426 #define DUMP(label) if (Trace_stream) cerr << Trace_stream->readable_contents(label);
427
428
429
430 #define dbg trace(0, "a")
431
432
433
434
435
436 :(before "End Globals")
437 ofstream Trace_file;
438 :(before "End Commandline Options(*arg)")
439 else if (is_equal(*arg, "--trace")) {
440 cerr << "saving trace to 'last_run'\n";
441 Trace_file.open("last_run");
442
443
444 Trace_file << " 0 dummy: start\n";
445 }
446 :(before "End trace Commit")
447 if (Trace_file) {
448 dump_trace_line(Trace_file, t);
449 }
450 :(before "End One-time Setup")
451 atexit(cleanup_main);
452 :(code)
453 void cleanup_main() {
454 if (Trace_file) Trace_file.close();
455
456 }
457
458 :(before "End trace_stream Methods")
459 string readable_contents(string label) {
460 string trim(const string& s);
461 ostringstream output;
462 label = trim(label);
463 for (vector<trace_line>::iterator p = past_lines.begin(); p != past_lines.end(); ++p)
464 if (label.empty() || label == p->label)
465 dump_trace_line(output, *p);
466 return output.str();
467 }
468
469
470
471
472 :(before "End Globals")
473 bool Dump_trace = false;
474 :(before "End Commandline Options(*arg)")
475 else if (is_equal(*arg, "--dump")) {
476 Dump_trace = true;
477 }
478 :(before "End Incremental Trace Print Conditions")
479 if (Dump_trace) return true;
480
481
482
483 :(code)
484 string trim(const string& s) {
485 string::const_iterator first = s.begin();
486 while (first != s.end() && isspace(*first))
487 ++first;
488 if (first == s.end()) return "";
489
490 string::const_iterator last = --s.end();
491 while (last != s.begin() && isspace(*last))
492 --last;
493 ++last;
494 return string(first, last);
495 }
496
497 :(before "End Includes")
498 #include <vector>
499 using std::vector;
500 #include <list>
501 using std::list;
502 #include <set>
503 using std::set;
504
505 #include <sstream>
506 using std::istringstream;
507 using std::ostringstream;
508
509 #include <fstream>
510 using std::ifstream;
511 using std::ofstream;