I am trying to overload operator<< for ostream to do up a writing to log file mechanism. Within CExportFunctions project, I am able to log << "xxx"
. However, when I tried to perform the same in another project (CCallMethods), I am unable to write into the file. Compilation was okay. No errors. But Entered processMessage()
did not get written into the file. Can any one please help?
Project A - CExportFunctions.h:
#ifdef DLLDIR_EX
#define DLLDIR __declspec(dllexport) // export DLL information
#else
#define DLLDIR __declspec(dllimport) // import DLL information
#endif
...
class DLLDIR CExportFunctions
{
public:
...
ofstream stream;
};
Project A - CExportFunctions.cpp:
#include "CExportFunctions.h"
...
//! write to log file
template<typename T> CExportFunctions& operator<<(CExportFunctions& stream, T val)
{
...
stream.stream.open("D:/Logger/logs.txt", ios::out | ios::app);
stream.stream << << val << std::endl;
stream.stream.close();
return stream;
}
//! save scenario dialog
void CExportFunctions::saveScenario()
{
CExportFunctions log;
log << "Entered saveScenario()";
...
}
Project B - CCallMethods.cpp:
#include "CExportFunctions.h"
void CCallMethods::processMessage()
{
...
CExportFunctions log;
log.stream << "Entered processMessage()";
}