I am writing a singleton logger with logging level for my exe and dll.
The Logger.h:
#define LOG CLogger::GetInstance().Log
#define LOG_PATH _T(".\\LogFile\\Logger.log")
enum eLogLevel { NONE=0, ERR, WARNING, USER, SYSTEM, DEVELOPER };
class CLogger
{
public:
//Construcor & Destructor
CLogger();
virtual ~CLogger();
//Singleton
static CLogger& GetInstance();
//For logging level preference
//Example: WARNING -> Log only ERR & WARNING messages
//Default = NONE
virtual void SetLogLevel(eLogLevel eLevel);
//Logging
virtual void Log(eLogLevel eLevelType, CString szText);
protected:
//Open & Close the log after used
virtual void CloseLog();
virtual BOOL OpenLog();
CStdioFile m_File;
CString m_szFile;
eLogLevel m_eLevel;
BOOL m_bFileOpened;
};
The idea is, EXE project will need to include Logger.cpp & Logger.h, responsible to set the logging level.
Meanwhile, DLL project will need to include Logger.cpp & Logger.h, but no need to set the logging level, as it will follow the logging level of EXE project.
Both of EXE & DLL are expected to able to write anything into the same log file.
The outcome now is, I will need to ask DLL project to SetLogLevel() so that DLL project is able to write into log file.
Can anyone spot the problem on the Logger.h above? Wouldn’t the Singleton sharing one instance of object including member variables, as EXE & DLL will be running on the same process/thread?
For a singleton object in this case, it will be instantiated twice in EXE and DLL.
Both of them are created in different memory address, hence, they are sharing different member variable. It indicates both of them exist without knowing each other.
Now, there are probably two ways to solve this problem:
1) instantiate the only ONE logger by wrapping the Logger class into DLL
2) EXE and DLL create instance separately, they need to set the log level at EXE and DLL