Skip to main content
GameDev.net gamedev.net
🔒 Locked

Wierd Problem

Started by dbzprogrammer Dec 10, 2005 at 12:49 PM 4 replies 900+ views
Original Post
dbzprogrammer
dbzprogrammer
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:

//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;
}

I'm sorry that this is so "noobish," but it's not making sense...
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
Conner McCloud
Conner McCloud
Your entire setup is rather flawed. Your GetInstance* functions don't initialize the log stream, which makes them rather error prone to begin with. The whole idea is being able to assume the object is in a valid state, and you don't make provide that assumption. With how you are doing things, you are probably just as well using a monostate...make main and fileName static, and scrap the whole singleton idea. You also say singleton = new Log;, but then in Log(), you assign singleton again [with a useless cast to boot]. Perhaps not an error, but definately a waste of time.

With that out of the way, have you stepped through ReadIn with a debugger and seen exactly what's going on? I'll bet that'll be pretty useful.

CM
dbzprogrammer
dbzprogrammer
I have, but it's not giving me an error anywhere. I know the singleton thing is messed up, but I don't know if I will have a class inherit Log. And the class is designed to be needed before made. That's why everything is passive, waiting to do the next step when called. It's just the way I code, and by doing that things can be more controlled.
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
dbzprogrammer
dbzprogrammer
[bump]
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
Skeleton_V@T
Skeleton_V@T
You can try setting break points just before these log functions are called and watch some critical variables as Conner McCloud suggested. Sorry I don't feel like stepping through your code, your problem is from the caller I guess.

[Edited by - Skeleton_V@T on December 11, 2005 10:22:25 AM]
--> The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones <--
dbzprogrammer
dbzprogrammer
Found.

//Kernal.cpp//Initialization functionint 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;}


I create a Basic Settings class first, which calls Log::Write. Unfortunately Log hasn't been Init()'ed yet, so it gets cranky.
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.