The way that you have it set up, one of two things can happen:
1) You have an instance of "Log" in something whose lifetime is as long as that of your program, thus the file is opened and truncated once at the start of the program, written to through the whole life of the program, and then closed when the program ends through the destructor. For instance, Log might be a
singleton or a member of something that is a singleton.
Code: Select all
void someFn() {
Log log = Log::getInstance();
}
or
2) You declare an instance of "Log" where you need it. This means that every time you make another instance, the file is re-opened and then closed when Log goes out of scope (like at the end of the function). This means that, since you're just using .open(), your file will be truncated. You would need to call it with ios::app for one of the parameters so that you don't lose your old log data. Unfortunately, this means that you would need to truncate the file when your program starts, or regularly delete the file (otherwise you append to the data from the previous execution). You would need to do it something like this:
Code: Select all
void main() {
ifstream mylogfile;
mylogfile.open("mylog.txt"); //truncate the file
mylogfile.close();
while (go) {
//heartbeat (aka main game loop)
}
}
Where
Code: Select all
Log::Log(const char* filename) {
this->filename = filename;
this->file.open(this->filename, ios::app);
}
Log::~Log() {
this->file.close();
}