My program saves all kinds of log information into text file. But if the program is crashing due to some problems such as memory access violation, the log text file has nothing in there.
I believe it is because the program failed to close opened log text file.
Currently, I am using FILE* for saving log text files. I probably can open and close every time to write each log but I think that is too much overhead.
Is there any other way that I can keep the log regardless of crashing or unexpected stopping of program?
I do want to see the log right before the program is crashing.
I am using C++/CLI for my program. Thank you very much in advance.
FILE* logfile;
errno_t err;
char LogFileNameBuf[512] = {0,};
sprintf_s(LogFileNameBuf, "LogFile.txt");
err = fopen_s(&logfile, LogFileNameBuf, "wt");
if(logfile != NULL)
{
bLogfile = true;
GetLocalTime(&st);
sprintf_s(logBuf, "[%04d-%02d-%02d][%02d:%02d:%02d] SUCCESS:: Log Started\n", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
fputs(logBuf, logfile);
}
// close log file
if(bLogfile == true)
{
GetLocalTime(&st);
sprintf_s(logBuf, "[%04d-%02d-%02d][%02d:%02d:%02d] SUCCESS:: Log File Closed\n", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
fputs(logBuf, logfile);
fclose(logfile);
}