home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / ole / tstcon / log.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  3.2 KB  |  190 lines

  1. #include "StdAfx.H"
  2. #include "TestCon.H"
  3.  
  4. #ifdef _DEBUG
  5. #define new DEBUG_NEW
  6. #undef THIS_FILE
  7. static char THIS_FILE[] = __FILE__;
  8. #endif
  9.  
  10.  
  11. IMPLEMENT_DYNAMIC( CLog, CObject );
  12.  
  13. CLog::CLog()
  14. {
  15. }
  16.  
  17. CLog::~CLog()
  18. {
  19. }
  20.  
  21. CLog& CLog::operator<<( COleVariant& var )
  22. {
  23.    COleVariant varString;
  24.    CString str;
  25.  
  26.    try
  27.    {
  28.       varString.ChangeType( VT_BSTR, var );
  29.       str = varString.bstrVal;
  30.    }
  31.    catch( CException* pException )
  32.    {
  33.       str = _T( "??????" );
  34.  
  35.      pException->Delete();
  36.    }
  37.  
  38.    (*this)<<str;
  39.  
  40.    return( *this );
  41. }
  42.  
  43. CLog& CLog::operator<<( int n )
  44. {
  45.    CString str;
  46.  
  47.    str.Format( _T( "%d" ), n );
  48.  
  49.    (*this)<<str;
  50.  
  51.    return( *this );
  52. }
  53.  
  54.  
  55. IMPLEMENT_DYNAMIC( CNullLog, CLog );
  56.  
  57. CNullLog::CNullLog()
  58. {
  59. }
  60.  
  61. CNullLog::~CNullLog()
  62. {
  63. }
  64.  
  65. CLog& CNullLog::operator<<( LPCTSTR /* pszString */ )
  66. {
  67.    return( *this );
  68. }
  69.  
  70.  
  71. IMPLEMENT_DYNAMIC( CDebugLog, CLog );
  72.  
  73. CDebugLog::CDebugLog() :
  74.    m_tStartOfLine( TRUE )
  75. {
  76. }
  77.  
  78. CDebugLog::~CDebugLog()
  79. {
  80. }
  81.  
  82. CLog& CDebugLog::operator<<( LPCTSTR pszString )
  83. {
  84.    CString strSpan;
  85.    CString strString( pszString );
  86.  
  87.    while( strString.GetLength() > 0 )
  88.    {
  89.       if( m_tStartOfLine )
  90.       {
  91.          OutputDebugString( _T( "TstCon Log: " ) );
  92.          m_tStartOfLine = FALSE;
  93.       }
  94.       strSpan = strString.SpanExcluding( _T( "\n" ) );
  95.       OutputDebugString( strSpan );
  96.       if( strString.GetLength() == strSpan.GetLength() )
  97.       {
  98.          strString.Empty();
  99.       }
  100.       else
  101.       {
  102.          ASSERT( strString[strSpan.GetLength()] == _T( '\n' ) );
  103.          OutputDebugString( _T( "\n" ) );
  104.          m_tStartOfLine = TRUE;
  105.          strString = strString.Mid( strSpan.GetLength()+1 );
  106.       }
  107.    }
  108.  
  109.    return( *this );
  110. }
  111.  
  112.  
  113. IMPLEMENT_DYNAMIC( CFileLog, CLog );
  114.  
  115. CFileLog::CFileLog()
  116. {
  117. }
  118.  
  119. CFileLog::~CFileLog()
  120. {
  121. }
  122.  
  123. CLog& CFileLog::operator<<( LPCTSTR pszString )
  124. {
  125.    m_file.WriteString( pszString );
  126.  
  127.    return( *this );
  128. }
  129.  
  130. BOOL CFileLog::Create( LPCTSTR pszFileName )
  131. {
  132.    BOOL tSuccess;
  133.  
  134.    tSuccess = m_file.Open( pszFileName, CFile::modeCreate|CFile::modeWrite|
  135.       CFile::shareExclusive|CFile::typeText );
  136.    if( !tSuccess )
  137.    {
  138.       return( FALSE );
  139.    }
  140.  
  141.    return( TRUE );
  142. }
  143.  
  144. CString CFileLog::GetFileName() const
  145. {
  146.    return( m_file.GetFileName() );
  147. }
  148.  
  149.  
  150. IMPLEMENT_DYNAMIC( COutputWindowLog, CLog );
  151.  
  152. COutputWindowLog::COutputWindowLog( CEdit* pEditBox ) :
  153.    m_pEditBox( pEditBox )
  154. {
  155.    ASSERT( m_pEditBox != NULL );
  156. }
  157.  
  158. COutputWindowLog::~COutputWindowLog()
  159. {
  160. }
  161.  
  162. CLog& COutputWindowLog::operator<<( LPCTSTR pszString )
  163. {
  164.    int iLastChar;
  165.    CString strSpan;
  166.    CString strString( pszString );
  167.  
  168.    while( strString.GetLength() > 0 )
  169.    {
  170.       strSpan = strString.SpanExcluding( _T( "\n" ) );
  171.       iLastChar = m_pEditBox->GetWindowTextLength();
  172.       m_pEditBox->SetSel( iLastChar, iLastChar );
  173.       m_pEditBox->ReplaceSel( strSpan );
  174.       if( strString.GetLength() == strSpan.GetLength() )
  175.       {
  176.          strString.Empty();
  177.       }
  178.       else
  179.       {
  180.          ASSERT( strString[strSpan.GetLength()] == _T( '\n' ) );
  181.          iLastChar = m_pEditBox->GetWindowTextLength();
  182.          m_pEditBox->SetSel( iLastChar, iLastChar );
  183.          m_pEditBox->ReplaceSel( _T( "\r\n" ) );
  184.          strString = strString.Mid( strSpan.GetLength()+1 );
  185.       }
  186.    }
  187.  
  188.    return( *this );
  189. }
  190.