//
//  Copyright (c) 2001, Reson, Inc. All Rights Reserved.
//
//  Filename:   Console.cpp
//
//  Project:    6046.
//
//  Author(s):  W. Arcus
//
//  Purpose:    General helpers to allow the service to be run in console
//              mode and output to the screen otherwise limits output
//              to the debugger.
//
//  Notes:

#include "StdAfx.h"
#include "Console.h"

#include <stdio.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//////////////////////////////////////////////////////////////////////
// CConsole output class implementation.

CConsole::CConsole( bool bIsConsole )
{
    m_szMessage[ 0 ]  = '\0';
    m_bConsole = bIsConsole;
}

CConsole::~CConsole( void )
{
}

int CConsole::Print( LPSTR lpszFormat, ... )
{
    int iNumChars = 0;

    try
    {
        va_list pArg;

        va_start( pArg, lpszFormat );

        m_Critical.Enter();

        iNumChars = vsprintf( m_szMessage, lpszFormat, pArg );

        if ( iNumChars > 0 )
        {
            // Send output message to the console if appropriate and the 
            // debugger (provided, of course, we're in debug mode).

            if ( m_bConsole )
            {
                printf( "%s", m_szMessage );
            }

            TRACE( "%s", m_szMessage );
        }

        m_Critical.Leave();

        va_end( pArg );
    }
    catch ( ... )
    {
        iNumChars = 0;
    }

    return iNumChars;
}

void CConsole::ConsoleProgram( bool bConsole )
{
    m_bConsole = bConsole;
}

bool CConsole::IsConsoleProgram( void ) const
{
    return m_bConsole;
}

