Original Post
I have a Log class, which is a singleton. It is contained on the stack as a private member in my Kernal class. The Kernal accesses it normally, instead of using it's GetInstanceP()//pointer and GetInstaceR()//reference functions. I have another class called Basic Settings that loads all the data neccessary to load the engine. Everything compiles fine, everything runs fine. But my Log class is supposed to print 2 lines: Game Engine started Function: Kernal::Init Basic Settings Loaded Function: BasicSettings::ReadIn But I'm getting nothing. When I comment out the Basic Settings write function, I can get the first line. Otherwise I get nothing. It is opening the file, but I don't know whats going on now. Code: I'm sorry that this is so "noobish," but it's not making sense...
//Log.h
//The Log class definition
#ifndef _E_LOG_
#define _E_LOG_
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <assert.h>
#include <cstring>
using namespace std;
class Log
{
private:
ofstream main;
char* fileName;
//Singlton
static Log* singleton;
public:
Log()
{
assert(!singleton);
singleton = static_cast<Log*>(this);
}
Log(char* filename)
{
assert(!singleton);
singleton = static_cast<Log*>(this);
size_t length = strlen(filename);
fileName = new char[length];
strcpy(fileName,filename);
Open();
}
~Log()
{
main.close();
delete[] fileName;
}
inline void Init(char* filename)
{
size_t length = strlen(filename);
fileName = new char[length];
strcpy(fileName,filename);
if(!Open())
{
cout << "Error" << endl;
system("PAUSE");
}
}
inline bool Open()
{
main.open(fileName);
if( main == NULL )
return false;
return true;
}
void Write(char* msg,char* funcName);
void Write(long MsgID,char* funcName);
inline static Log* GetInstanceP()
{
if(!singleton)
singleton = new Log;
return singleton;
}
inline static Log& GetInstanceR()
{
if(!singleton)
singleton = new Log;
return (*singleton);
}
};
#endif
//Log.cpp
//The Log class definition of functions
#include "inc/ELog.h"
Log* Log::singleton = 0;
void Log::Write(char* msg,char* funcName)
{
main << msg << " - Function: " << funcName << '\n';
main.flush();
}
void Log::Write(long MsgID,char* funcName)
{
main << MsgID << " - Function: " << funcName << '\n';
main.flush();
}
//Kernal.cpp
//Initialization function
int Kernal::Init(HINSTANCE& hCI, LPSTR& CmdString, int& ShowCmd)
{
hInstanceK = &hCI
hString = &CmdString
hShowCmd = &ShowCmd
bSettings.Init("main.txt");
eventmanager = new EventManager;
log.Init("log.txt");
log.Write("Game Engine started","Kernal::Init");
return 1;
}
//BasicSettings.cpp
bool BasicSettings::ReadIn()
{
char buffer[12];
ifstream inf(fileName);
inf >> data.numOfCPU >> data.numOfRAM;
inf.getline(buffer,12);
size_t length = strlen(buffer);
assert(!data.OGLversion);
data.OGLversion = new char[length];
strcpy(data.OGLversion,buffer);
inf >> data.windowLength >> data.windowHeight;
inf.close();
Log::GetInstanceR().Write("Basic Settings Read","BasicSettings::ReadIn");
return false;
}