Make sure linux log files don't get deleted while still logging to them

* We need to use file locks to ensure the last process knows it can
  delete the log, as well as delete any inherited file descriptors when
  forking to launch a new executable.
* To accomplish this, tracking the single logfile handle is now part of
  the OS-specific code instead of common logging code. It makes little
  difference either way as we don't support multiple log files.
This commit is contained in:
baldurk committed 2017-05-17 19:41:39 +01:00
1 parent 6df6d152f0
commit b94d13b838
7 files changed
+111 -42

No files matched your search

+11 -11
View File
@@ -253,7 +253,7 @@ uint64_t Log2Floor(uint64_t value)
#endif
static string logfile;
static void *logfileHandle = NULL;
static bool logfileOpened = false;
const char *rdclog_getfilename()
{
@@ -268,20 +268,21 @@ void rdclog_filename(const char *filename)
if(filename && filename[0])
logfile = filename;
FileIO::logfile_close(logfileHandle);
FileIO::logfile_close(NULL);
logfileOpened = false;
if(!logfile.empty())
{
logfileHandle = FileIO::logfile_open(filename);
logfileOpened = FileIO::logfile_open(filename);
if(logfileHandle && previous.c_str())
if(logfileOpened && previous.c_str())
{
vector<unsigned char> previousContents;
FileIO::slurp(previous.c_str(), previousContents);
if(!previousContents.empty())
FileIO::logfile_append(logfileHandle, (const char *)&previousContents[0],
previousContents.size());
FileIO::logfile_append((const char *)&previousContents[0], previousContents.size());
FileIO::Delete(previous.c_str());
}
@@ -295,11 +296,10 @@ void rdclog_enableoutput()
log_output_enabled = true;
}
void rdclog_closelog()
void rdclog_closelog(const char *filename)
{
log_output_enabled = false;
if(logfileHandle)
FileIO::logfile_close(logfileHandle);
FileIO::logfile_close(filename);
}
void rdclog_flush()
@@ -326,10 +326,10 @@ void rdclogprint_int(LogType type, const char *fullMsg, const char *msg)
OSUtility::WriteOutput(OSUtility::Output_StdErr, msg);
#endif
#if ENABLED(OUTPUT_LOG_TO_DISK)
if(logfileHandle)
if(logfileOpened)
{
// strlen used as byte length - str is UTF-8 so this is NOT number of characters
FileIO::logfile_append(logfileHandle, fullMsg, strlen(fullMsg));
FileIO::logfile_append(fullMsg, strlen(fullMsg));
}
#endif
}