Hi, I am creating a logger class but I am having a run-time error.
Basically I use this code to open the log:
int CLogger::openLog(const char* filename)
{
std::ofstream logFile(filename);
if (!logFile.is_open())
{
return -1; //Error! Couldn't open the log
}
writeLog("--LOG STARTED--");
return 0;
}
Then, I use this code to write to the log (the overloaded method other classes throughout the code use):
int CLogger::writeLog(char* text)
{
logFile << text << "\n";
logFile.flush();
return 1;
}
int CLogger::writeLog(int text)
{
logFile << text << "\n";
logFile.flush();
return 1;
}
int CLogger::writeLog(std::string text)
{
const char* charText = text.c_str();
logFile << charText << "\n";
logFile.flush();
return 1;
}
The error though, is strange. The log outputs --LOG STARTED-- as it should in the openLog method, but then when I call upon it from an outside class such as this one:
int CGameState_Menu::preRun()
{
log->writeLog("preRun() menu state");
return 1;
}
I get a run-time error.
Here is the call-stack:
> msvcp71d.dll!std::operator<<<std::char_traits<char> >(std::basic_ostream<char,std::char_traits<char> > & _Ostr={...}, const char * _Val=0x00455f48) Line 711 + 0x3 C++
OninWars_d.exe!CLogger::writeLog(char * text=0x00455f48) Line 32 + 0x17 C++
OninWars_d.exe!CGameState_Menu::preRun() Line 22 C++
OninWars_d.exe!CGameManager::run() Line 106 + 0x24 C++
OninWars_d.exe!main() Line 17 C++
So, it dies on this line:
streamsize _Pad = _Ostr.width() <= 0 || _Ostr.width() <= _Count
? 0 : _Ostr.width() - _Count;
in ostream.
Any ideas why this could be?