
/***************************************************************************
**
**    $Header:   D:/ECB2S/SRC/LOG/TSTLOG.CPP   1.2.1.4   17 Apr 1997 16:08:26   ZJRD  $
**
**    $Log:   D:/ECB2S/SRC/LOG/TSTLOG.CPP  $
** 
**    Rev 1.2.1.4   17 Apr 1997 16:08:26   ZJRD
** No change.
** 
**    Rev 1.2.1.3   16 Apr 1997 10:43:42   ZJRD
** No change.
** 
**    Rev 1.2.1.2   10 Apr 1997 15:48:34   ZJRD
** No change.
** 
**    Rev 1.2.1.1   03 Apr 1997 15:27:42   ZJRD
** No change.
** 
**    Rev 1.2.1.0   28 Mar 1997 10:31:00   ZJRD
** easy pack sld 2.09d
** 
**    Rev 1.1   19 Mar 1997 11:40:14   ZJRD
** No change.
** 
**    Rev 1.0   12 Mar 1997 14:53:10   ZJRD
** Initial revision.
** 
**    Rev 1.7.1.0.1.2   09 Dec 1996 10:13:54   ZJRD
** EasyPack/SLD Version 2.0P
** 
**    Rev 1.7.1.0.1.0   11 Nov 1996 12:57:10   ZJRD
** EasyPack/SLD Version 2.01
** 
**    Rev 1.7.1.4   09 Sep 1996 13:12:16   ZJRD
** No change.
** 
**    Rev 1.7.1.3   05 Sep 1996 11:24:04   ZJRD
** No change.
** 
**    Rev 1.7.1.2   02 Sep 1996 09:51:22   ZJRD
** No change.
** 
**    Rev 1.7.1.1   28 Aug 1996 15:43:04   ZJRD
** No change.
** 
**    Rev 1.7.1.0   12 Aug 1996 10:48:36   ZJRD
** No change.
** 
**    Rev 1.7   11 Jun 1996 10:23:12   ZJRD
** EasyPack/SLD Version 1.97
** 
**    Rev 1.5   05 Jun 1996 14:50:56   ZJRD
** No change.
** 
**    Rev 1.4   29 May 1996 09:27:30   ZJRD
** No change.
** 
**    Rev 1.3   16 May 1996 08:59:12   ZJRD
** No change.
** 
**    Rev 1.2   10 May 1996 09:07:44   ZJRD
** No change.
** 
**    Rev 1.1   02 May 1996 10:22:14   ZJRD
** EasyPack/SLD Version 1.92
** 
**    Rev 1.31   18 Apr 1996 12:52:34   Shirley
** No change.
** 
**    Rev 1.30   12 Apr 1996 10:33:24   Shirley
** EasyPack/SLD Version 1.90
** 
**    Rev 1.28   15 Feb 1996 08:46:22   Shirley
** No change.
** 
**    Rev 1.27   12 Feb 1996 14:01:26   Shirley
** No change.
** 
**    Rev 1.26   06 Feb 1996 15:26:52   Shirley
** No change.
** 
**    Rev 1.25   06 Feb 1996 13:42:16   Shirley
** No change.
** 
**    Rev 1.24   01 Feb 1996 10:11:10   Shirley
** No change.
** 
**    Rev 1.23   26 Jan 1996 09:11:40   Shirley
** No change.
** 
**    Rev 1.22   25 Jan 1996 13:12:00   Shirley
** No change.
** 
**    Rev 1.21   24 Jan 1996 10:31:54   Shirley
** EasyPack/SLD Version 0.34d
** 
**    Rev 1.20   23 Jan 1996 11:21:44   Shirley
** No change.
** 
**    Rev 1.19   18 Jan 1996 10:07:10   Shirley
** No change.
** 
**    Rev 1.18   15 Jan 1996 16:09:56   Shirley
** No change.
** 
**    Rev 1.17   04 Jan 1996 11:07:24   Shirley
** No change.
** 
**    Rev 1.16   30 Nov 1995 09:07:46   Shirley
** No change.
** 
**    Rev 1.15   28 Nov 1995 15:28:48   Shirley
** No change.
** 
**    Rev 1.14   21 Nov 1995 11:17:44   Shirley
** No change.
** 
**    Rev 1.13   13 Nov 1995 09:21:20   Shirley
** No change.
** 
**    Rev 1.12   12 Nov 1995 11:27:56   Shirley
** No change.
** 
**    Rev 1.11   08 Nov 1995 16:27:28   Shirley
** No change.
** 
**    Rev 1.10   08 Nov 1995 12:39:24   Shirley
** No change.
** 
**    Rev 1.9   02 Nov 1995 10:02:08   Shirley
** No change.
** 
**    Rev 1.8   27 Oct 1995 16:44:46   Shirley
** No change.
** 
**    Rev 1.7   27 Oct 1995 13:43:04   Shirley
** No change.
** 
**    Rev 1.6   25 Oct 1995 14:25:34   Shirley
** No change.
** 
**    Rev 1.5   18 Oct 1995 14:46:38   Shirley
** No change.
** 
**    Rev 1.4   13 Oct 1995 13:20:06   Shirley
** No change.
** 
**    Rev 1.3   29 Sep 1995 09:51:58   Shirley
** No change.
** 
**    Rev 1.2   20 Sep 1995 10:54:22   Shirley
** No change.
** 
**    Rev 1.1   15 Sep 1995 09:48:52   Shirley
** No change.
** 
**    Rev 1.0   07 Sep 1995 09:54:06   Shirley
** Initial revision.
**
****************************************************************************/

/////////////////////////////////////////////////////////////////////////////
//
//  File name: TSTLOG.CPP
//
//  Description: The implementation file for the class: CLogFile.
//
//  Author: Chen Jun
//
//  Date: 05/19/95
//
//  Modification:
//      1. 05/19/95, Initial version of the class: CLogFile.
//
/////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////
// Include files.
#include "stdafx.h"
#include "resource.h"

#include "tstlog.h"


/////////////////////////////////////////////////////////////////////////////
// Debug flags.
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif


/////////////////////////////////////////////////////////////////////////////
// Type definitions.


/////////////////////////////////////////////////////////////////////////////
// Macro definitions.


/////////////////////////////////////////////////////////////////////////////
// Global variables.
CLogFile LogFile;


/////////////////////////////////////////////////////////////////////////////
// Static variables.


/////////////////////////////////////////////////////////////////////////////
// External variables.


/////////////////////////////////////////////////////////////////////////////
// Global function prototypes.
void LogCmd(int nArgc, char* pszArgv[]);
void TstLogInfo(char* pchBuf, int nLen=0);

int TstGetLogOn(void);
int TstGetLogCommand(void);
int TstGetLogAppend(void);
int TstGetLogOpened(void);

void TstSetLogOn(int nLogOn);
void TstSetLogCommand(int nLogCommand);
void TstSetLogAppend(int nLogAppend);
void TstSetLogOpened(int nLogOpened);

void TstOpenLog(CString strFileName);
void TstCloseLog(void);


/////////////////////////////////////////////////////////////////////////////
// Local function prototypes.


/////////////////////////////////////////////////////////////////////////////
// External function prototypes.


/////////////////////////////////////////////////////////////////////////////
// Executable codes.

/////////////////////////////////////////////////////////////////////////////
// Public functions.

// Supply the global flag to the Log dialog.
int TstGetLogOn(void)
{
    return ((int)LogFile.m_isLogOn);
    
}   // End of TstGetLogOn().

int TstGetLogCommand(void)
{
    return ((int)LogFile.m_isLogCommand);
    
}   // End of TstGetLogCommand().

int TstGetLogAppend(void)
{
    return ((int)LogFile.m_isLogAppend);
    
}   // End of TstGetLogAppend().

int TstGetLogOpened(void)
{
    return ((int)LogFile.m_isLogOpened);
    
}   // End of TstGetLogOpened().

void TstSetLogOn(int nLogOn)
{
    LogFile.m_isLogOn = (BOOL) nLogOn;

}   // End of TstSetLogOn().

void TstSetLogCommand(int nLogCommand)
{
    LogFile.m_isLogCommand = (BOOL) nLogCommand;

}   // End of TstSetLogCommand().

void TstSetLogAppend(int nLogAppend)
{
    LogFile.m_isLogAppend = (BOOL) nLogAppend;

}   // End of TstSetLogAppend().

void TstSetLogOpened(int nLogOpened)
{
    LogFile.m_isLogOpened = (BOOL) nLogOpened;

}   // End of TstSetLogOpened().

void TstOpenLog(CString strFileName)
{
    // Assertion of the input parameters.
    ASSERT( !strFileName.IsEmpty() );
    
    // Open log file.
    LogFile.OpenLog(strFileName);
    
}   // End of TstOpenLog().

void TstCloseLog(void)
{
    // Close log file.
    LogFile.CloseLog();

}   // End of TstCloseLog().


/////////////////////////////////////////////////////////////////////////////
//
//  Name:   LogCmd().
//
//  Description: The main control routine of Log command.
//
//  Input:  nArgc - Number of input parameters.
//          pszArgv - Input parameters.
//
//  Output: None.
//
//  Return: None.
//
/////////////////////////////////////////////////////////////////////////////
void LogCmd(int nArgc, char* pszArgv[])
{
    // Assertion of the input parameters.
    ASSERT( nArgc >= 2 && nArgc <= 4 );
    for ( int i = 0; i < nArgc; i++ ) {
        ASSERT( pszArgv[i] );
    }
    
    // Execute Log command.
    LogFile.LogAction(nArgc, pszArgv);

}   // End of LogCmd().


// Log information to the Log file.
void TstLogInfo(char* pchBuf, int nLen)
{
    // Assertion of the input parameters.
    ASSERT( pchBuf );
    ASSERT( nLen >= 0 && nLen <= 256 );
    
    // Log information to the Log file.
    LogFile.LogInfo(pchBuf, nLen);
    
}   // End of TstLogInfo().



/////////////////////////////////////////////////////////////////////////////
// Implementation codes of class: CLogFile.

// Construction.
CLogFile::CLogFile()
{
    // Initial.
    m_isLogOn = FALSE;
    m_isLogCommand = FALSE;
    m_isLogAppend = FALSE;
    
    m_isLogOpened = FALSE;
    
    m_nOpenFlag |= CFile::modeWrite;
    
}   // End of CLogFile::CLogFile().


// Destruction.
CLogFile::~CLogFile()
{
    // Close the Log file.
    if ( m_isLogOpened ) {
        CloseFile();
    }
     
}   // End of CLogFile::~CLogFile().


// LOG          {filename [Result] [Append]} | {ON|OFf|Close}
void CLogFile::LogAction(int nArgc, char* pszArgv[])
{
    // Assertion of the input parameters.
    ASSERT( nArgc >= 2 && nArgc <= 4 );
    for ( int i = 0; i < nArgc; i++ ) {
        ASSERT( pszArgv[i] );
    }
    ASSERT( NO_ERROR == m_nErrorID );
    
    // Parse the parameters.
    CString strArgv;
    switch ( nArgc ) {
        case 2:
            // Log filename|ON|OFf|Close
            strArgv = pszArgv[1];
            if ( !strArgv.CompareNoCase("ON") ) {
                if ( !m_isLogOpened ) {
                    m_nErrorID = LOG_UNOPEN;
                }
                else {
                    m_isLogOn = TRUE;
                }
            }
            else if ( !strArgv.CompareNoCase("OFF") ) {
                if ( !m_isLogOpened ) {
                    m_nErrorID = LOG_UNOPEN;
                }
                else {
                    m_isLogOn = FALSE;
                }
            }
            else if ( !strArgv.CompareNoCase("CLOSE") ) {
                if ( !m_isLogOpened ) {
                    m_nErrorID = LOG_UNOPEN;
                }
                else {
                    CloseFile();
                    m_isLogOpened = FALSE;
                    m_isLogOn = FALSE;
                    m_isLogCommand = FALSE;
                    m_isLogAppend = FALSE;
                    m_nOpenFlag = CFile::typeText|CFile::modeWrite;
                }
            }
            else {
                if ( m_isLogOpened ) {
                    m_nErrorID = LOG_EXIST;
                }
                else {
                    m_strFileName = pszArgv[1];
                    m_nOpenFlag |= CFile::modeCreate;
                    OpenFile();
                    if ( NO_ERROR == m_nErrorID ) {
                        m_isLogOpened = TRUE;
                        m_isLogOn = TRUE;
                        m_isLogAppend = FALSE;
                        m_isLogCommand = FALSE;
                    }
                }
            }
            break;
        case 3:
            // Log filename Command|Append
            if ( m_isLogOpened ) {
                m_nErrorID = LOG_EXIST;
            }
            else {
                m_strFileName = pszArgv[1];
                strArgv = pszArgv[2];
                if ( !strArgv.CompareNoCase("COMMAND") ) {
                    m_isLogCommand = TRUE;
                    m_isLogAppend = FALSE;
                    m_nOpenFlag |= CFile::modeCreate;
                    OpenFile();
                }
                else {
                    m_isLogAppend = TRUE;
                    m_isLogCommand = FALSE;
                    OpenFile();
                    if ( NO_ERROR != m_nErrorID ) {
                        m_nOpenFlag |= CFile::modeCreate;
                        OpenFile();
                    }
                }
                if ( NO_ERROR == m_nErrorID ) {
                    m_isLogOpened = TRUE;
                    m_isLogOn = TRUE;
                }
            }
            break;
        case 4:
            // Log filename {Command Append} | {Append Command}
            if ( m_isLogOpened ) {
                m_nErrorID = LOG_EXIST;
            }
            else {
                m_strFileName = pszArgv[1];
                OpenFile();
                if ( NO_ERROR != m_nErrorID ) {
                    m_nOpenFlag |= CFile::modeCreate;
                    OpenFile();
                }
                if ( NO_ERROR == m_nErrorID ) {
                    m_isLogOpened = TRUE;
                    m_isLogCommand = TRUE;
                    m_isLogAppend = TRUE;
                    m_isLogOn = TRUE;
                }
            }
            break;
        default:
            ASSERT( FALSE );
            break;
    }
    
    // Display error message.
    if ( NO_ERROR != m_nErrorID ) {
        DisplayErrorMessage();
        m_nErrorID = NO_ERROR;
    }
        
}   // End of CLogFile::LogAction().


// Log information to the Log file.
void CLogFile::LogInfo(char* pchBuf, int nLen)
{
    // Assertion of the input parameters.
    ASSERT( pchBuf );
    ASSERT( nLen >= 0 && nLen <= 256 );
    
    // Log file is off.
    if ( !m_isLogOn ) {
        return;
    }
         
    // Log command only.
    char pszBuf[258];
    if ( 0 == nLen ) {
        // Log the results.
        if ( m_isLogCommand ) {
            return;
        }
        else {
            strcpy(pszBuf, " ");
            strcat(pszBuf, pchBuf);
            strcat(pszBuf, "\n");
        }
    }
    else {
        // Log user's commands.
        memcpy(pszBuf, pchBuf, nLen);
        pszBuf[nLen] = '\n';
        pszBuf[nLen+1] = 0;
    }

    // Write to Log file.
    m_pszLineBuf = pszBuf;
    WriteToFile();

}   // End of CLogFile::LogInfo().


// Open log file from Log dialog.
void CLogFile::OpenLog(CString strFileName)
{
    // Assertion of the input parameters.
    ASSERT( !strFileName.IsEmpty() );

    // Set the server flag.
    m_isServer = TRUE;

    // Open log file.
    if ( m_isLogOpened ) {
        m_nErrorID = LOG_EXIST;
    }
    else {
        m_strFileName = strFileName;
        m_nOpenFlag = CFile::typeText|CFile::modeWrite;
        if ( !m_isLogAppend ) {
            m_nOpenFlag |= CFile::modeCreate;
        }
        OpenFile();
        if ( NO_ERROR != m_nErrorID ) {
            m_nErrorID = NO_ERROR;
            m_nOpenFlag |= CFile::modeCreate;
            OpenFile();
        }
        if ( NO_ERROR == m_nErrorID ) {
            m_isLogOpened = TRUE;
            m_isLogOn = TRUE;
        }
    }

    // Display error message.
    if ( NO_ERROR != m_nErrorID ) {
        DisplayErrorMessage();
        m_nErrorID = NO_ERROR;
    }
    
}   // End of CLogFile::OpenLog().


// Close log file from Log dialog.
void CLogFile::CloseLog(void)
{
    // Assertion of the input parameters.

    // Set the server flag.
    m_isServer = TRUE;

    // Close log file.
    if ( !m_isLogOpened ) {
        m_nErrorID = LOG_UNOPEN;
    }
    else {
        CloseFile();
        m_isLogOpened = FALSE;
        m_isLogOn = FALSE;
        m_isLogCommand = FALSE;
        m_isLogAppend = FALSE;
        m_nOpenFlag = CFile::typeText|CFile::modeWrite;
    }

    // Display error message.
    if ( NO_ERROR != m_nErrorID ) {
        DisplayErrorMessage();
        m_nErrorID = NO_ERROR;
    }

}   // End of CLogFile::CloseLog().

//////////////////////////////// End of File ////////////////////////////////
