File handling question...
I have a class that I am using to Log Debug Messages in ".log" files.
The problem I am having is getting the class to only save 30 log files.
So if I have file: Log_1.log, etc, etc, Log_30.log then I want the program to delete Log_1.log and save the new Debug log as Log_31.log. That way the names don't lost meaning.
I have tried, and tried, and tried. It must be something simple that I am overlooking.
I have the following code. It will only save 10 files, and I have absolutely no idea what it is doing. Any assistance would be greatly appreciated.
Also, as far as the code that finds the files. I am 100% sure that it works the way it is suppose to.
Code:
MT_Log::MT_Log()
{
//Gather all files, and store them in filelist
flist list;
BuildFileList(list);
int FirstFileID = 0;
int LastFileID = 0;
int FileID = 0;
//Get first filename from list
if ( list.files != 0 )
{
std::stringstream NameParser;
std::string FileName;
//Get FileID from filename
FileName = list.files[0].cFileName;
NameParser << FileName.substr(FileName.find("Log_")+4, FileName.find(".log"));
NameParser >> FileID;
}
FirstFileID = FileID;
FileID = 0;
//Get last filename from list
if ( list.files != 0 )
{
std::stringstream NameParser;
std::string FileName;
//Get FileID from filename
FileName = list.files[list.num_entries-1].cFileName;
NameParser << FileName.substr(FileName.find("Log_")+4, FileName.find(".log"));
NameParser >> FileID;
}
LastFileID = FileID;
if ( ( FirstFileID+30 ) < LastFileID )
{
std::string File = "Logs\\";
File += list.files[0].cFileName;
std::remove(File.c_str());
}
std::stringstream ss;
ss << "Logs\\Log_" << LastFileID+1 << ".log";
log.open(ss.str().c_str(),std::ios::trunc);
// free the list of files
free(list.files);
}
Thank you SO much if you can help.