Jump to content
  • Advertisement
Sign in to follow this  
bah

Regarding log files

This topic is 4845 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

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

Share this post


Link to post
Share on other sites
Advertisement
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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
}

Share this post


Link to post
Share on other sites
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.

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!