I have the following code
void _log_message(const char* fmt, ...)
{
va_list arg;
ofstream logfile;
cout << "Open Log File" << endl;
logfile.open(LOG_FILE, ios::out | ios::app);
if(!logfile.is_open()) return;
time_t t = time(NULL);
struct tm *tmptr = gmtime(&t);
char tmStr[70];
if (tmptr == NULL || strftime(tmStr, sizeof tmStr, "%Y-%m-%d %H:%M:%S", tmptr) == 0)
{
boost::format errFormat("gmtime() failed in file %s at line # %d\n");
logfile << errFormat % __FILE__ % (int)(__LINE__-3) << endl;
}
char* fmtMessage;
va_start(arg, fmt);
vsprintf(fmtMessage, fmt, arg);
va_end(arg);
try
{
cout << "write to logfile\t" << tmStr << "\t" << fmtMessage << endl;
logfile << tmStr << "\t" << fmtMessage << endl;
cout << "close logfile" << endl;
logfile.close();
cout << "close done" << endl;
}
catch(exception e)
{
cout << "Exception: " << e.what() << endl;
}
cout << "exit" << endl;
}
It runs fine up until the output to the logfile. Then it just stops. No errors that I can spot, and no exceptions being caught.
Open Log File
write to logfile 2014-07-30 16:12:34 Starting...
I check with ps and the process is dead, not hung.
If I remove the endl from the end of the line then it works, but then it hits the same problem again on the close method call:
Open Log File
write to logfile 2014-07-30 16:15:53 Starting...
close logfile
If I comment on the close method call then I get the final "exit" line but the function never returns.
This is called on the first line of function that is called on the first line of main so I'm fairly sure that I couldn't have screwed anything up too seriously by that point. I did run it through valgrind which found nothing.
Not that all of the cout calls are debugging only and not part of the program itself.