基于ptlib更新日志

bool  Gatekeeper::RotateLogFile()
{
    PWaitAndSignal 
lock(m_logFileMutex);

    
if (m_logFile) {
        PTRACE(
1"GK Logging closed (log file rotation)");
        PTrace::SetStream(
&cerr); // redirect to cerr
#ifndef hasDeletingSetStream
        delete m_logFile;
#endif
        m_logFile 
= NULL;
    }


    
if (m_logFilename.IsEmpty())
        
return false;
    
    PFile
* const oldLogFile = new PTextFile(m_logFilename, PFile::WriteOnly, 
        PFile::MustExist
        );
    
if (oldLogFile->IsOpen()) {
        
// Backup of log file
        PFilePath filename = oldLogFile->GetFilePath();
        
const PString timeStr = PTime().AsString("yyyyMMdd_hhmmss");
        
const PINDEX lastDot = filename.FindLast('.');
        
if (lastDot != P_MAX_INDEX)
            filename.Replace(
".""." + timeStr + ".", FALSE, lastDot);
        
else
            filename 
+= "." + timeStr;
        oldLogFile
->Close();
        oldLogFile
->Move(oldLogFile->GetFilePath(), filename);
    }

    delete oldLogFile;
        
    m_logFile 
= new PTextFile(m_logFilename, PFile::WriteOnly, PFile::Create);
    
if (!m_logFile->IsOpen()) {
        cerr 
<< "Warning: could not open the log file ""
             
<< m_logFilename << "" after rotation" << endl;
        delete m_logFile;
        m_logFile 
= NULL;
        
return false;
    }


    m_logFile
->SetPosition(0, PFile::End);
    PTrace::SetStream(m_logFile);
    PTRACE(
1"GK Logging restarted.");
    
return true;
}
该函数实现的功能是将老日志文件修改名称为“名称+日期”,新日志文件继续记录日志

你可能感兴趣的:(基于ptlib更新日志)