I'm not entirely sure if this is the correct place to be posting things like this as it's purely C++ related. Do we have a C++ forum?
Anyway, I have here a debugging class.
CDebugSystem.h
#ifndef _DEBUG_SYSTEM_H_
#define _DEBUG_SYSTEM_H_
// severity levels
#define DEBUGINFO 0
#define DEBUGNOTICE 1
#define DEBUGWARN 2
#define DEBUGERROR 3
#define DEBUGDISASTER 4
// include files
#include <fstream>
#include <iostream>
// namespaces
using namespace std;
class CDebugSystem
{
// private
private:
// Enable
bool EnableDebug;
// severety tags
char* SeverityTag[4];
// create file object
ofstream File;
const char* FileName;
// protected
protected:
// public
public:
// output functions
void Output( unsigned char SeverityLevel , char* Message );
// Enable, disable
void Enable( void );
void Disable( void );
// constructor, destructor
CDebugSystem( const char* FileName );
virtual ~CDebugSystem();
};
#endif // _DEBUG_SYSTEM_H_
CDebugSystem.cpp
// include files
#include "CDebugSystem.h"
using namespace std;
// constructor
CDebugSystem::CDebugSystem( const char* FileName )
{
// construct file object
this->FileName = FileName;
ofstream* File = new ofstream;
// enable debugging by default
Enable();
// set severity tags
SeverityTag[0] = "<font color=#000000>";
SeverityTag[1] = "<font color=#0000ff>";
SeverityTag[2] = "<font color=#808080>";
SeverityTag[3] = "<font color=#ff8080>";
SeverityTag[4] = "<font color=#ff0000>";
}
// destructor
CDebugSystem::~CDebugSystem()
{
// close the file
if( this->File.is_open() ) this->File.close();
delete File;
}
// enable, disable
void CDebugSystem::Enable( void )
{
// enable debugging flag
this->EnableDebug = true;
// open file for debugging
this->File.open( this->FileName );
}
void CDebugSystem::Disable( void )
{
// disable debugging flag
this->EnableDebug = false;
// close file
if( this->File.is_open() ) this->File.close();
}
// output
void CDebugSystem::Output( unsigned char SeverityLevel , char* Message )
{
// check if debug system is enabled
if( !EnableDebug ) return;
// make sure severty level is in range
if( SeverityLevel > 4 ) return;
// check if file is open
if( this->File.is_open() )
{
this->File << Message << endl; // The error is HERE ******************************
}
// write to console
cout << Message << endl;
}
And here is how it's implemented:
// include files
#include "CDebugSystem.h"
// namespaces
using namespace std;
// main entry point
int main( int argc , char* argv[] )
{
CDebugSystem* PDebug = new CDebugSystem( "test.txt" );
PDebug->Output( 0 , "test" );
PDebug->Disable();
delete PDebug;
return 0;
}
The problem is in the file "CDebugSystem.cpp" where I try to write to the file (I highlighted the line with a comment above). I've debugged the code to check that the filename is passed correctly - which it is - and I can confirm that the actual file "test.txt" appears on my hard drive. I then made sure that the text I'm writing to it is passed correctly - which it is - so now I'm kind of lost as to why
File << Message << endl; doesn't work...
I even tried replacing that line with
File << "test" << endl, but no success.
TheComet