I’m using Visual Studio 2008 Express, and I have this logging code:
fprintf(fp,"%s %s %s %s %d %s\n", pType, __DATE__, __TIME__, pFileName, lineNo, pMsg.c_str());
fflush(fp);
Sometimes, if I don’t delete the log and just keep appending to it, the __TIME__ macro just keeps printing the old time, rather than the current time.
I’m using the following code to open the file
if(pfileName != NULL)
{
fp = fopen(pfileName, "a+");
if(fp != NULL)
fseek(fp, 0, SEEK_END);
}
The problem is you’re not doing a rebuild.
By default, Visual Studio only builds/compiles the source files that have changed (been modified) since the last time you have compiled your project. This is intended to save time, and it’s a very useful feature. If you want to ensure that source files are recompiled, regardless of whether or not they’ve been changed since the last build, you have to instruct VS to rebuild your code (or go through the song and dance of doing a clean, and then a build on your project).
The reason why that matters is that the
__TIME__preprocessor macro is expanded during preprocessing (which happens right before compilation, at least for our purposes here) with the current time. It got expanded the first time you compiled the code, and never gets changed unless you modify the source file or recompile (rebuild) it.This is probably not what you’re looking for in a logging function. Instead of the compilation time, you want to print the current system time. You can use the
timefunction to obtain that—to use it, make sure that you include<time.h>at the top of your source file. Or alternatively, if you’re already including<windows.h>, you can call theGetSystemTimefunction to fill aSYSTEMTIMEstructure that gives you the current time and date.