xxxxxxxxxx
import std;
// Snooping fun()
mixin(snoop(q{
int fun(int a, int b)
{
int c = 3;
foreach (i; 1 .. 5)
{
a += i;
}
int d = a + b + c;
return d;
}
}));
// The snoop lib
string snoop(string code, size_t start = __LINE__) pure
{
auto buf = appender!string;
buf.reserve(code.length * 2);
auto l = start;
size_t flush_pos = 0;
foreach (i, c; code) {
switch (c) {
case ';':
if (code[i+1] != '\n')
continue;
buf ~= code[flush_pos .. i + 1] ~
` stderr.writeln(Clock.currTime(), " executed line ", ` ~
to!string(l) ~ `, ":\t` ~
code[flush_pos .. i + 1] ~ `");`;
flush_pos = i + 1;
break;
case '\n':
l++;
buf ~= code[flush_pos .. i + 1];
flush_pos = i + 1;
break;
default:
}
}
return buf.data;
}
void main()
{
writeln(fun(1, 2)); // 16
}