Global counter in an application: bad practice?
In my C ++ application, I sometimes create different output files for troubleshooting purposes. Each file is created at a different stage in our pipeline operation, and it is difficult for him to know which file appeared before which another (the timestamps of the files show the same date).
I am going to add a global counter to my application and a function (multithreading) that increments and returns this global counter. I can then use this counter as part of the filenames I am creating.
Is this considered bad practice? Many different modules need to create files, and they are not necessarily related to each other.
a source to share
I think adding the current system time to the filename with a high enough precision would be cleaner and easier to implement and maintain
//helper function to encapsulate details
string makeFileName(string filename)
{ return filename + getCurrentTime(); }
void MyClass::SomeMethod()
{
File f = CreateFile(makeFileName("myclassfile"));
...
}
a source to share
If it's just for debugging, is temporary and won't be released, you can do whatever is quicker and easier.
If, on the other hand, this is something that can stay in your application for a long time and possibly break into a release, then I will definitely take more time and write a basic file creation module available to all your other modules.
a source to share