⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 log.cpp

📁 这是DVD中伺服部分的核心代码
💻 CPP
字号:
/******************************************************************************
*******************************************************************************
**                                                                           **
**  Copyright (c) 2006 Videon Central, Inc.                                  **
**  All rights reserved.                                                     **
**                                                                           **
**  The computer program contained herein contains proprietary information   **
**  which is the property of Videon Central, Inc.  The program may be used   **
**  and/or copied only with the written permission of Videon Central, Inc.   **
**  or in accordance with the terms and conditions stipulated in the         **
**  agreement/contract under which the programs have been supplied.          **
**                                                                           **
*******************************************************************************
******************************************************************************/
/**
 * @file log.cpp 
 *
 * $Revision: 1.7 $ 
 *
 * Log class.  This class is used to store log entries (strings) into memory.
 *             The log can be optionally be simulataneously stored in a file.
 *             The log inherits ReferenceCounter and is a shared resource (can
 *             be used by more than one process). The log will not be destroyed
 *             until all the processes have finished with the log.
 *
 *
 */
#include "log.h"
#include <string.h>





/*******************************************************************************
*
*                             Constants
*
*******************************************************************************/
#define     LOG_ENTRY_ARRAY_INCREMENT_SIZE    1000






/**
 *******************************************************************************
 *  Log::Log  initializes object to valid state
 *
 *******************************************************************************/
Log::Log ( void )
{
    m_outputFile                = NULL;
    m_outputFilename            = NULL;
    m_maxLogEntryLength         = 0;
    m_logEntryArraySize         = 0;
    m_logEntryArrayCapacity     = 0;
    m_logEntryArray             = NULL;
}






/**
 *******************************************************************************
 *  Log::Construct  allocates required resources, prepares object for use
 *
 *  This method is private and will be automatically called from 
 *  ReferenceCounter::AddReference method and will be inside a critical section
 *
 *  @return     VDVD_SUCCESS if the function is successful
 *******************************************************************************/
VDVD_ERROR   Log::Construct ( void )
{
    AutoMutex   autoMutex(&m_mutex);

    m_maxLogEntryLength         = 0;
    m_logEntryArraySize         = 0;
    m_logEntryArrayCapacity     = 0;
    m_logEntryArray             = NULL;

    if (( m_outputFilename != NULL ) && ( m_outputFile == NULL ))
    {
        m_outputFile = fopen( m_outputFilename, "wb" );
        VDVD_ASSERT_ERROR( (m_outputFile==NULL), VDVD_ERROR_PLATFORM_FAILED );
    }

    return ( VDVD_SUCCESS );
}






/**
 *******************************************************************************
 *  Log::Destruct   frees allocated resources, prepares object for non-use
 *
 *  This method is private and will be automatically called from 
 *  ReferenceCounter::DeleteReference method when the object is no longer being
 *  referenced(used). This code will execute inside a critical section.
 *
 *  @return     VDVD_SUCCESS if the function is successful
 *******************************************************************************/
VDVD_ERROR   Log::Destruct ( void )
{
    AutoMutex   autoMutex(&m_mutex);
    int         result  = 0;
    uint32      index   = 0;

    for ( index = 0; index < m_logEntryArraySize; index++ ) 
    {
        delete(m_logEntryArray[index]);
        m_logEntryArray[index] = NULL;
    }

    if ( m_logEntryArray != NULL ) 
    {
        delete ( m_logEntryArray );
        m_logEntryArray = NULL;
    }

    if ( m_outputFile != NULL )
    {
        result = fflush(m_outputFile);
        VDVD_ASSERT_ERROR( (result!=0), VDVD_ERROR_PLATFORM_FAILED );

        result = fclose(m_outputFile);
        VDVD_ASSERT_ERROR( (result!=0), VDVD_ERROR_PLATFORM_FAILED );
    }

    return ( VDVD_SUCCESS );
}






/**
 *******************************************************************************
 *  Log::Create    Creates a log
 *
 *  Creates log via Construct method automatically called from method
 *  ReferenceCounter::AddReference. Optionally use Create in the process that 
 *  initializes the system to provide a filename to dump the log to. Create
 *  adds a reference.
 *
 *  @param      filename    Filename to dump log to. If NULL, data is lost when log is destroyed.
 *
 *  @return     VDVD_SUCCESS if the function is successful
 *              VDVD_ERROR_OBJECT_HAS_INVALID_STATE if the log has already been referenced(created)
 *******************************************************************************/
VDVD_ERROR   Log::Create( const char* outputFilename )
{
    AutoMutex   autoMutex(&m_mutex);

    VDVD_ASSERT_ERROR( (IsReferenced()==TRUE), VDVD_ERROR_OBJECT_HAS_INVALID_STATE );

    m_outputFilename = outputFilename;

    VDVD_RAISE_ERROR( AddReference() );

    return ( VDVD_SUCCESS );

}





/**
 *******************************************************************************
 *  Log::Destroy    Destroys a log
 *
 *  Destroys log via Destruct method automatically called from method
 *  ReferenceCounter::DeleteReference. Same as DeleteReference
 *
 *  @return     VDVD_SUCCESS if the function is successful
 *              VDVD_ERROR_OBJECT_HAS_INVALID_STATE if the log has already been destroyed
 *******************************************************************************/
VDVD_ERROR   Log::Destroy( void )
{
    AutoMutex   autoMutex(&m_mutex);

    return ( DeleteReference() );
}





/**
 *******************************************************************************
 *  Log::Read    Reads an entry(string) from the log
 *
 *  @param      index       log entry to read[ 0..(n-1) ]
 *
 *  @return     VDVD_SUCCESS if the function is successful
 *******************************************************************************/
const char* Log::Read ( uint32 index )
{
    AutoMutex   autoMutex(&m_mutex);

    VDVD_ASSERT_REPORT_ERROR( (IsReferenced()==FALSE), VDVD_ERROR_OBJECT_HAS_INVALID_STATE );
    VDVD_ASSERT_REPORT_ERROR( (index>=m_logEntryArraySize), VDVD_ERROR_INVALID_PARAMETER );

    return ( m_logEntryArray[index] );
}







/**
 *******************************************************************************
 *  Log::Write    Writes an entry(string) into the log
 *
 *  @param      entry   null terminated string to add to the log
 *
 *  @return     VDVD_SUCCESS if the function is successful
 *******************************************************************************/
VDVD_ERROR   Log::Write ( const char* entry )
{
    AutoMutex   autoMutex(&m_mutex);
    int         result      = 0;
    void*       newArray    = NULL;

    VDVD_ASSERT_ERROR( (IsReferenced()==FALSE), VDVD_ERROR_OBJECT_HAS_INVALID_STATE );

    if ( m_logEntryArraySize + 1 > m_logEntryArrayCapacity )
    {
        newArray = realloc( m_logEntryArray, (m_logEntryArrayCapacity+LOG_ENTRY_ARRAY_INCREMENT_SIZE)*(sizeof(char*)) );
        if ( newArray != NULL )
        {
            m_logEntryArray = (char**)newArray;
            m_logEntryArrayCapacity += LOG_ENTRY_ARRAY_INCREMENT_SIZE;
        }
        else
        { 
            return ( VDVD_ERROR_NOT_ENOUGH_MEMORY );
        }

    }

    if ( m_logEntryArraySize < m_logEntryArrayCapacity ) 
    {
        m_logEntryArray[m_logEntryArraySize] = strdup(entry);

        if ( m_logEntryArray[m_logEntryArraySize] != NULL ) 
        { 
            m_maxLogEntryLength = max(m_maxLogEntryLength, strlen(m_logEntryArray[m_logEntryArraySize]) );
            m_logEntryArraySize++;
        }
        else
        { 
            return (VDVD_ERROR_NOT_ENOUGH_MEMORY);
        }
    }

    if ( m_outputFile != NULL )
    {
        result = fputs( entry, m_outputFile );
        VDVD_ASSERT_ERROR( (result==EOF), VDVD_ERROR_PLATFORM_FAILED );

        result = fputs( "\r\n", m_outputFile );
        VDVD_ASSERT_ERROR( (result==EOF), VDVD_ERROR_PLATFORM_FAILED );

        result = fflush( m_outputFile );
        VDVD_ASSERT_ERROR( (result==EOF), VDVD_ERROR_PLATFORM_FAILED );
    }

    return ( VDVD_SUCCESS );
}






/**
 *******************************************************************************
 *  Log::Write    Clear (empty) the log
 *
 *  @return     VDVD_SUCCESS if the function is successful
 *******************************************************************************/
VDVD_ERROR   Log::Clear ( void )
{
    AutoMutex   autoMutex(&m_mutex);

    VDVD_ASSERT_ERROR( (IsReferenced()==FALSE), VDVD_ERROR_OBJECT_HAS_INVALID_STATE );

    VDVD_RAISE_ERROR( Destruct() );
    VDVD_RAISE_ERROR( Construct() );

    return ( VDVD_SUCCESS );
}





/**
 *******************************************************************************
 *  Log::Compare    private function used by sort to compare to entries
 *
 *  @param      entry1      passed in from qsort
 *  @param      entry2      passed in from qsort
 *
 *  @return     VDVD_SUCCESS if the function is successful
 *******************************************************************************/
int  Log::Compare( const void* entry1, const void* entry2 )
{
   return _stricmp( * ( char** ) entry1, * ( char** ) entry2 );
}






/**
 *******************************************************************************
 *  Log::Sort    Sorts the log entries alphabetical order
 *
 *  @return     VDVD_SUCCESS if the function is successful
 *******************************************************************************/
VDVD_ERROR   Log::Sort ( void )
{
    AutoMutex   autoMutex(&m_mutex);
    uint32      index = 0;

    VDVD_ASSERT_ERROR( (IsReferenced()==FALSE), VDVD_ERROR_OBJECT_HAS_INVALID_STATE );
    
    qsort(m_logEntryArray, m_logEntryArraySize, sizeof( char* ), Log::Compare );

    return ( VDVD_SUCCESS );
}




/**
 *******************************************************************************
 *  Log::GetNumberOfEntries     returns number of log entries stored    
 *
 *
 *  @return     number of log entries stored
 *******************************************************************************/
uint32  Log::GetNumberOfEntries ( void )
{
    VDVD_ASSERT_REPORT_ERROR( (IsReferenced()==FALSE), VDVD_ERROR_OBJECT_HAS_INVALID_STATE );

    return ( m_logEntryArraySize );
}






/**
 *******************************************************************************
 *  Log::GetMaxEntryLength     returns the size of the longest entry
 *
 *
 *  @return     size(in bytes) of the longest entry
 *******************************************************************************/
uint32  Log::GetMaxEntryLength( void )
{
    return ( m_maxLogEntryLength );
}







⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -