Jump to content
  • Advertisement
Sign in to follow this  
Mizipzor

Problems with log file

This topic is 4811 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want a logging system, I have one the Ive been modifying (found it on a tutorial site or if it was somewhere on these forums). However, as it was when I found it, it took const char* which was written to the log. I wanted to to take std::string*. And I also wanted it to be able to print to the screen as well as the log file. Thats why I started modifying it. But now it doesnt work anymore. :P I creates a .txt file but nothing ever shows in it, and nothing on the screen either. Anyone can see whats wrong? Log.h
#ifndef LOGGER_H
#define LOGGER_H

#include <fstream>
#include <iostream>

using namespace std;

//Defines
#define LOG_FILE	"Sdl.log"


class Log
{
	public:
		Log();
		~Log();

		static bool screenWrite;	// write to screen?
		static bool fileWrite;		// write to logfile?
			
		static void Write(string* logEntry);	// Writes to the log
		static void Write(string logEntry);	// Writes to the log
		
	private:
		bool init_log();
		void close_log();
		static ofstream log_file;						// The File For Error Logging
};

#endif

Log.cpp
#include "Log.h"

//statics Implementation here.
ofstream Log::log_file(LOG_FILE);
bool Log::screenWrite;
bool Log::fileWrite;

/************************************************************
This is the function that initializes the system Log

Input: None

Outputs: None
************************************************************/
Log::Log()
{
	screenWrite = true;
	fileWrite = true;

	init_log();
}

/************************************************************
This is the function that shuts down the system Log

Input: None

Outputs: None
************************************************************/
Log::~Log()
{
	close_log();
}

/************************************************************
This is the function that initializes the system Log

Input: None

Outputs: true/false if can open the file for logging
************************************************************/
bool Log::init_log()
{
	if( !(log_file.is_open()) )
	{
		printf("Can't init Logfile!\n" );
		exit(1);
	}

	Write(" -- Log Init...\n");

	return true;
}

/************************************************************
This is the function that closes the system Log

Input: None

Outputs: None
************************************************************/
void Log::close_log()
{
	Write("\n-- Closing Log...\n");
	if(log_file.is_open())
	{
		log_file.close();
	}
}

/************************************************************
This is a static function that actually writes a line in the log.

Input: line to log

Outputs: writes a line to the log file
************************************************************/
void Log::Write(string* logEntry)
{
	if(screenWrite)
	{
		printf("%s", *logEntry);
	}

	if(fileWrite)
	{
		log_file << *logEntry->c_str() << endl;
	}
}

void Log::Write(string logEntry)
{
	Write(&logEntry);
}

No compilation errors or anything like that I might add.

Share this post


Link to post
Share on other sites
Advertisement
I haven't looked closely but one thing caught my attention:

printf("%s", *logEntry);

You cannot do that; the %s modifier expects a C-string while you supply a std::string. Because the printf() function uses the dreaded ellipsis construct the compiler cannot detect this. Use:

printf("%s", logEntry->c_str() );


You might also want to consider that you are passing each string by value (i.e. all characters are copied around). This unnecessary. A more efficient and common way of passing STL string around is by reference:

void Write( const std::string& logEntry )


Greetz,

Illco

Share this post


Link to post
Share on other sites
This line:
log_file << *logEntry->c_str() << endl;
Is incorrect. logEntry is a "std::string*" so logEntry->c_str() results in a const char*, which are what you wanted, but then you dereference the pointer and the statement results in a "char", which is the first char of the string. So your code is the same as saying:
log_file << (*logEntry)[0] << endl;
It should be this:
log_file << logEntry->c_str() << endl;

Share this post


Link to post
Share on other sites
I've edited the function now and it looks like this:


void Log::Write(const string& logEntry)
{
if(screenWrite)
{
printf("%s", logEntry.c_str());
}

if(fileWrite)
{
log_file << logEntry.c_str() << endl;
}
}


However Im still not getting anything in the logfile, nor on the screen. But no compilation errors. I set screenwrite and filewrite to true in the constructor. Why does it refuse to write? It creates the txt file also, its in the folder with all the other files.

Share this post


Link to post
Share on other sites
Im not aiming for a particular style I just code with the knowledge Ive collected from all the countless tutorials Ive read. And obviusly, that knowledge wasnt enough to pull this one off. :P

Share this post


Link to post
Share on other sites
The reason nothing is happening is because the variables screenWrite and fileWrite are never set to true. You set them in the constructor, but you never create an actual Log object and therefore the constructor is never called. Instead of initializing them in the constructor you should change this:

//statics Implementation here.
ofstream Log::log_file(LOG_FILE);
bool Log::screenWrite;
bool Log::fileWrite;


to this:

//statics Implementation here.
ofstream Log::log_file(LOG_FILE);
bool Log::screenWrite = true;
bool Log::fileWrite = true;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!