#include<iostream>
#include<string>
using namespace std;
// Generic class for tracing the function call
class functionlogging {
private:
string name;
string in{ ">>" };
string out{ "<<" };
public:
functionlogging(string x) :name(x) {
cout << in << name << endl;
}
~functionlogging(){
cout << out << name << endl;
}
};
//uses
void generic_function() {
functionlogging logger{ __func__ };
int a = 10;
int b = 100;
a += b;
}
int main() {
generic_function();
}
Many times, we do need to log when function call starts and ends for better understanding. With modern C++ concepts like __funct__/RAII
, I wrote the small class which can be used to achieve trace. With this I was able to achieve this for above "generic_function()"
.
Output
>>generic_function
<<generic_function
I wanted others opinion about the functionlogging
class and how to make it in such a way that user of this class would have to do the minimal work/code changes in his/her code base.
At present user of this class needs to create a object of this class and pass the __func__
information. Is it acceptable or in a way can it be embed inside something by which user of the class almost do not require to do anything?.
#define
like withassert
, if you like that more. – firda 12 hours ago