How come when I do this: (logging in class constructor)
Sdl_handle::Sdl_handle(int resolutionX, int resolutionY, int depth, cLog* LogFile) {
Log = LogFile; // store the adress to the logfile, if NULL is sent, no logs will be written
// write to log
std::stringstream temp;
temp << "SDLasd: Screen resolution set to " << resolutionX << " x " << resolutionY;
Log->Write(temp.str());
}
It ends up on the console screen instead of in the log file?
The Log class instance is owned by the same class as Sdl_handle is owned by. It looks like this:
cLog.h
#ifndef _LOG
#define _LOG
#include <fstream>
class cLog {
private:
std::ofstream log; // the log file
public:
cLog() {
log.open("Log.txt"); //Automatically deletes any existing file, you have to tell it to append for it not to.
std::clog.rdbuf(log.rdbuf()); //Redirect clog to output to file.
}
~cLog() {log.close();};
// log file function
void Write(std::string message) {std::clog << message << std::endl;}
};
#endif
If I write to the log in other functions (not the constructor) it works. :S Why?