Logo Search packages:      
Sourcecode: passage version File versions  Download package

FileLog.cpp

/*
 * Modification History
 *
 * 2002-February-25    Jason Rohrer
 * Created.  
 *
 * 2002-March-13    Jason Rohrer
 * Added a flush after every write.  
 *
 * 2002-April-8    Jason Rohrer
 * Changed to be thread-safe.  
 *
 * 2002-November-5    Jason Rohrer
 * Added support for backing up logs and deleting old log data.
 */


#include "FileLog.h"

#include "PrintLog.h"

#include "minorGems/util/stringUtils.h"
#include "minorGems/io/file/File.h"


#include <stdio.h>
#include <time.h>



const char *FileLog::mDefaultLogFileName = "default.log";



00035 FileLog::FileLog( char *inFileName, unsigned long inSecondsBetweenBackups )
    : mLogFile( NULL ),
      mLogFileName( stringDuplicate( inFileName ) ),
      mSecondsBetweenBackups( inSecondsBetweenBackups ),
      mTimeOfLastBackup( time( NULL ) ) {

    
    mLogFile = fopen( mLogFileName, "a" );

    if( mLogFile == NULL ) {
        printf( "Log file %s failed to open.\n", mLogFileName );
        printf( "Writing log to default file:  %s\n",
                mDefaultLogFileName );

        // switch to default log file name
        
        delete [] mLogFileName;
        mLogFileName = stringDuplicate( mDefaultLogFileName );

        
        mLogFile = fopen( mLogFileName, "a" );
        
        if( mLogFile == NULL ) {
            printf( "Default log file %s failed to open.\n",
                    mLogFileName );
            }
        }
    
    }



FileLog::~FileLog() {
    if( mLogFile != NULL ) {
        fclose( mLogFile );
        }
    delete [] mLogFileName;
    }


        
00076 void FileLog::logString( char *inLoggerName, char *inString,
                         int inLevel ) {

    if( mLogFile != NULL ) {
        if( inLevel <= mLoggingLevel ) {


            char *message = PrintLog::generateLogMessage( inLoggerName,
                                                          inString,
                                                          inLevel );

            mLock->lock();
            fprintf( mLogFile, "%s\n", message );
            
            fflush( mLogFile );

            if( time( NULL ) - mTimeOfLastBackup > mSecondsBetweenBackups ) {
                makeBackup();
                }
            
            mLock->unlock();

            delete [] message;
            }
        }
    }



00105 void FileLog::makeBackup() {
    fclose( mLogFile );

    File *currentLogFile = new File( NULL, mLogFileName );

    char *backupFileName = new char[ strlen( mLogFileName ) + 10 ];
    sprintf( backupFileName, "%s.backup", mLogFileName );


    File *backupLogFile = new File( NULL, backupFileName );
    delete [] backupFileName;
    
    
    // copy into backup log file, which will overwrite it
    currentLogFile->copy( backupLogFile );
    

    delete currentLogFile;
    delete backupLogFile;


    // clear main log file and start writing to it again
    mLogFile = fopen( mLogFileName, "w" );

    if( mLogFile == NULL ) {
        printf( "Log file %s failed to open.\n", mLogFileName );
        }

    mTimeOfLastBackup = time( NULL );
    }










Generated by  Doxygen 1.6.0   Back to index