Error Log Help

Started by
18 comments, last by Crusable77 11 years, 6 months ago
Hello,

I am learning SDL and i thought it would be helpful to make an Debug log to tell me if something is working or not. I am doing the project as a stand alone just to see if it works and then implement it in my SDL projects. So i have played a round with it a little but i cannot get the program to work. I am trying to make a class that handles all the debug stuff for me using the fstream to make a file telling me what happened. I cannot seem to get this to work, can anyone give me some hints? anything would be much appreciated.
Advertisement
Let me just grab my crystal ball... seriously, if you want help on your code, you might want to post said code, otherwise there's not much to go on. We're not psychic tongue.png

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

Sorry i thought i linked the project.
Main.cpp
[source lang="cpp"]#include "stdafx.h"
#include "DebugLog.h"
int main (){
DebugLog debug("DebugLog.txt");
debug.~debug();
return 0;
}[/source]
DebugLog.h
[source lang="cpp"]
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

class DebugLog {
public:
DebugLog();
~DebugLog();
string writeTo(string text);
private:
string m_Text;
string m_Filename;
};[/source]
DebugLog.cpp
[source lang="cpp"]
#include "stdafx.h"
#include "DebugLog.h"
DebugLog::DebugLog(){
ofstream debugLog("DebugLog.txt");
m_Text = "-----Debug Log Starts Now-----\n\n";
debugLog << m_Text;
}
DebugLog::~DebugLog(){
m_Text = "-----End of DebugLog-----";
debugLog << m_Text;
debugLog.close();
}
string DebugLog::writeTo(string text){
m_Text = text;
debugLog << text;
return;
}[/source]
I dont know whats wrong the code isnt right and the site wont let me reply properly.
Try pastebin.com, the code tags aren't working too well at the moment :(

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

DebugLog.h:
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

class DebugLog {
public:
DebugLog();
~DebugLog();
string writeTo(string text);
private:
string m_Text;
string m_Filename;
};
Debug.cpp:
#include "stdafx.h"
#include "DebugLog.h"
DebugLog::DebugLog(){
ofstream debugLog("DebugLog.txt");
m_Text = "-----Debug Log Starts Now-----\n\n";
debugLog << m_Text;
}
DebugLog::~DebugLog(){
m_Text = "-----End of DebugLog-----";
debugLog << m_Text;
debugLog.close();
}
string DebugLog::writeTo(string text){
m_Text = text;
debugLog << text;
return;
}
and Main.cpp
{
DebugLog debug("DebugLog.txt");
debug.writeTo("Line 1\n");
debug.~debug();
return 0;

I have an idea about whats going on but not a way to fix it.
First, don't call destructors like that. Use delete debug instead - and if the class is allocated on the stack instead - like it is here - just let it die when it goes out of scope, its destructor will be called automatically.

Secondly, does this even compile? debugLog is not defined in the destructor or in writeTo(). Basically, you allocate your iostream in the constructor, which is on the stack - as soon as it goes out of scope (i.e. when the constructor returns) the iostream is closed and lost. This iostream exists only in the constructor. If you want to keep a persistent iostream throughout your debug log's lifetime, you want it to exist as a private field in your class. Then you can use it anywhere, and your writeTo() will actually work. Does it make sense?

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

If im understanding this right, your saying the ofstream debugLog("DebugLog.txt"); should be in my private: part of the class. When i do this i get an error saying that the "DebugLog.txt" needs an identifier before it. And Im not understanding what your saying about the destructor.
And int the main function it says DebugLog debug("DebugLog.txt"), that isnt supposed to be there sorry i forgot to delete it.
[source lang="cpp"]class DebugLog {
public:
DebugLog();
~DebugLog();
string writeTo(string text);
private:
string m_Text;
string m_Filename;
ofstream m_debugLog;
};

DebugLog::DebugLog()
{
m_debugLog.open("DebugLog.txt"); // m_debuglog now belongs to the class, not just the constructor
// so it can be accessed in writeTo() as well
}[/source]

This means the debugLog object is now attached to your class instead of a specific method, so every method in the class has now access to it. The constructor takes care of creating the stream. Does it make more sense now?

Never mind the destructor stuff for now - you need to understand the concept of class fields before.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

This topic is closed to new replies.

Advertisement