0

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);
    }
diehard98
  • 232
  • 4
  • 14
  • Show the code you are using to log the error? In the past, I recall having to call `FileStream.Flush()` so that the contents got written out to the file before the process was terminated. – Matt Smith May 22 '13 at 14:27
  • Current code is very simple by using FILE* pointer. I will edit my original post to show my code. Thanks. – diehard98 May 22 '13 at 16:21
  • looks like you've got your answer--in the future is easier for everyone if you post code so that no one is left guessing. – Matt Smith May 22 '13 at 16:25
  • 1
    Thanks, Matt. I will do so from now on. – diehard98 May 22 '13 at 16:32

1 Answers1

1

You can try forcing I/O operation in the file using fflush(). You could do that each time you write in your logs to make sure you have as much data actually written as possible.

Although I would suggest, as you are using C++, using fstream instead of FILE*

JBL
  • 12,588
  • 4
  • 53
  • 84
  • Or you can use OS-dependent file writing (`CreateFile` in Windows, `open` in Unices) as they don't use intermediate memory buffering. With `fstream`, a `flush` call is also needed. – Liviu May 22 '13 at 14:30
  • Thank you for quick reply! It does work with 'fflush()'. I want to follow your advice which is using 'fstream' instead of 'FILE*'. I am sure 'fstream' is better than FILE* but could you tell me why 'fstream' is better? Thanks. – diehard98 May 22 '13 at 16:19
  • I found kind of answer about my question here. http://stackoverflow.com/questions/5570361/file-vs-fstream – diehard98 May 22 '13 at 16:32
  • @diehard98 It's not that much better, but it definitely has some valuable pros, such as the face that you don't have to carry a pointer, exception safety, encapsulation, etc. But this has been listed/discussed in the post you linked. :) – JBL May 22 '13 at 17:32