Samuele Armondi said:
Maybe you could manually keep track of the current function, i.e:
#include <vector>
#include <string>
#include <iostream>
class FunctionTracer
{
std::cout << ft.CurrentFunction();
}
This is untested code, I just wrote as it came...so don't blame me if its
broken and buggy (as it probaly will be!). I hope it helps anyway.
S. Armondi
Sorry, I forgot to add the CurrentFunction() code... the code should look
like this:
#include <vector>
#include <string>
#include <iostream>
class FunctionTracer
{
private:
std::vector<std::string> Trace;
int MaxSteps;
public:
FunctionTracer(int);
void EnterFunction(const std::string&);
void LeaveFunction(const std::string&);
void SetMaxSteps(int);
void Reset();
const std::string& CurrentFunction();
};
FunctionTracer::FunctionTracer(int n) : MaxSteps(n)
{
Trace.clear();
Trace.reserve(MaxSteps);
}
void FunctionTracer::EnterFunction(const std::string& s)
{
if (Trace.size() == MaxSteps)
Trace.clear();
Trace.push-back(s);
}
void FunctionTracer::LeaveFunction(const std::string& s)
{
if (Trace.back() == s) // the function we are leaving will
be th
e last one we entered
Trace.pop_back();
else
; // error!
}
void SetMaxSteps(int n)
{std::cout << ft.CurrentFunction();
if (MaxSteps != n)
{
MaxSteps = n;
Trace.reserve(MaxSteps);
}
}
void Reset()
{
Trace.clear();
}
const std::string& CurrentFunction()
{
return Trace.back();
}
//test it
FunctionTracer ft(10);
void f1()
{
ft.EnterFunction("f1");
std::cout << "in function 1\n";
ft.LeaveFunction("f1");
}
void f2()
{
ft.EnterFunction("f2");
std::cout << "in function2\n";
ft.LeaveFunction("f2");
}
int main()
{
ft.EnterFunction("main");
f1();
std::cout << ft.CurrentFunction();
f2();
std::cout << ft.CurrentFunction();
}