Regarding log files

Started by
13 comments, last by bah 18 years, 8 months ago
Hi I have an application that writes debugging output to a text file named "log.txt". When I launch multiple instances of the same application from the same directory they all try to write to the same file and the result is a big mess. How can I prevent this? I was thinking of determining if "log#.txt" exists and is open and if it's not, create and write to it or pick a new name (e.g. "log5.txt") and try again. I am using the ofstream object. Any one know how to do this? Thanks
Advertisement
You could build your log filename always with the time you started the process...
Or you could add the process id to the log file name...
my-eulogy - A blog about coding and gfxsdgi - Semi-Daily Game IdeaChunkyHacker - Viewer for Relic chunky formats (used in DOW)
There are plenty of ways of encoding a unique filename, but which functions should I use to determine whether the log with the specified name exists and is open?
you could create a counter that you add to the filename. Then when you shall open the log file, you find the last number used, increment it and add the value to the end of the new log filename. This way you know which files were created last and you're also shure not to overwrite older log files, if that's a must. Another way is to add the current date/time to filename, but this might give quite long filenames.
my-eulogy - A blog about coding and gfxsdgi - Semi-Daily Game IdeaChunkyHacker - Viewer for Relic chunky formats (used in DOW)
eulogy

Your solution is predicated on my running the application multiple times but in sequence, while I am saying that I'll be running multiple instances of the same application from the same directory.
Howdy,

If my memory serves me correctly, attempting to open the file for reading will fail [if you specify the flags correctly...]. So to check if the file exists, just try to open it. If you get a FILE*==0 then you're free, otherwise you need to try again. This should even work if the files are still open in the other contexts (I think). Just don't forget to close the file on a failure...

I think that's the most straightforward and standard way...

Hope this helps,

CJM
CJM

I am using the ofstream object on account of it being easier to use than FILE. To be more specific I do the following:

log.open( "log.txt" );

Do you know how to do the same thing with ofstream?
I have this in an old project of mine (hence the char-array :-)). It may be a bit 'Linux'-ish (stat is much more common there), but it works for me:
bool fileExists( const char *filename ){#ifdef _WIN32	struct _stat s;	return ( _stat(filename, &s) == 0 );#else	struct stat s;	return ( stat(filename, &s) == 0 );#endif}
bah - Yes, I understood that :). But even though you're running multiple instances you only assign the log file name to the current instance once. If you find out which file is the next one to create on startup and then create it, the next instance of the program to be created (since the instances aren't created at the same time even though they run at the same time) will see this file and create the next one.
my-eulogy - A blog about coding and gfxsdgi - Semi-Daily Game IdeaChunkyHacker - Viewer for Relic chunky formats (used in DOW)

This topic is closed to new replies.

Advertisement