I'm trying to write a log class with threadsafe practice in c++. Now the thing is, I want to make the call to each log line very simple. I can go with static class way like below:
//Calling this in some GUI initialization point of application
CLogger::InitLogger("LogFile.log");
//Calling below from any class just adding the header to the cpp
CLogger::Log("Some informational text goes here");
Now this doesn't follow the OOP so would like to go with a singleton class.
//Singleton log class
class CLogWrite
{
public:
static CLogWrite* GetInstance();
private:
static CLogWrite *pInstance;
void Log();
};
CLogWrite* CLogWrite::GetInstance()
{
if(pInstance != NULL)
{
pInstance = new CLogWrite;
}
return pInstance;
}
void CLogWrite::Log()
{
//threadSafe lock
//write 'logText' to file here
//threadSafe unlock
}
Now the thing is, if I write above class and call CLogWriter::GetInstance() inside my GUI class init function like below:
//single logger instance for my app
CLogWriter *mLogInstance;
mLogInstance = CLogWriter::GetInstance()
I need to pass 'mLogInstance' variable to each and every class inside my project from where I want to write a log. But I don't wanna do that.
What would be best approach?