//-*-C++-*-

/*  src/Debug.ipp  */


/*
 * Author: Philogelos A. <Philogelos@yahoo.com>
 * Maintainer: Philogelos A.
 * Keywords: C++, library, containers
 *
 * Copyright (C) 1998, 1999 Philogelos A.
 *
 * This file is part of Quercus Robusta.
 *
 * Quercus Robusta is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Library General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this software; see the file COPYING.LIB.  If not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 */



#ifndef __DEBUG_IPP__
#define __DEBUG_IPP__

/* $Id: Debug.ipp,v 1.5 1999/05/22 13:00:28 philogelos Exp $ */

#include "Debug.hpp"
#include "String.hpp"
#include "platform/Platform.hpp"
#include "exceptions/InternalError.hpp"

extern "C"
{
  /* defined in Debug.cpp */
  void ( *oldTerminateHandler )( void );
  void ( *oldUnexpectedHandler )( void );
  void terminateHandler( void );
  void unexpectedHandler( void );
}

#if !defined( INLINE_GETLOGGER )
INLINE Debug *Debug::getLogger( const Top * const )
{
  if( logger == nil )
	{
	  logger = new Debug();
	}
  return logger;
}
#endif

INLINE Debug::Debug()
{
  oldTerminateHandler = set_terminate( terminateHandler );
  oldUnexpectedHandler = set_unexpected( unexpectedHandler );
  totalMessagesLogged = 0;
}

INLINE void Debug::log( const char *const aMessage, ... )
{
  va_list args;

  va_start( args, aMessage );
  Platform::getInstance() -> rawErrorPrintf
	( "\n[%s] ", Platform::getInstance() -> getTimeStampString() );
  Platform::getInstance() -> rawErrorVPrintf( aMessage, args );
  va_end(args);
}


INLINE void Debug::logObject( const Top *anObject,
					   String ( Top::*aMethod )() const )
{
  char *formatted;
  
  formatted = ( anObject ->* aMethod )().getChars();
  test_( formatted != NULL );
  
  Platform::getInstance() -> rawErrorPrintf( "\n%s", formatted );
  delete []formatted;
}

INLINE void Debug::logObject( const char *const aMessage,
					   const Top *anObject,
					   String ( Top::*aMethod )() const )
{
  char *formatted;
  
  formatted = ( anObject ->* aMethod )().getChars();
  test_( formatted != NULL );
  
  Platform::getInstance() -> rawErrorVPrintf( "\n", NIL );
  Platform::getInstance() -> rawErrorPrintf( aMessage, formatted );
  delete []formatted;
}

INLINE void Debug::logString( const char *const aMessage, String aString )
{
  char *formatted;
  
  formatted = aString.getChars();
  test_( formatted != NULL );
  
  Platform::getInstance() -> rawErrorVPrintf( "\n", NIL );
  Platform::getInstance() -> rawErrorPrintf( aMessage, formatted );
  delete []formatted;
}

INLINE void Debug::panic( const char *const aMessage )
{
  Platform::getInstance() -> 
	rawErrorPrintf( "A serious error `%s' occurred. Choosing suicide...",
					aMessage );

  if( shouldDropCore )
	{
	  Platform::getInstance() -> suicide();
	}
  else if( attachDebuggerOnError )
	{
	  Platform::getInstance() -> attachDebugger();
	}
  else
	{
	  Platform::getInstance() -> exitImmediate( -1 );
	}
}

INLINE void Debug::dumpCore()
{
  Platform::getInstance() -> suicide();
}

INLINE void Debug::assertion( boolean aCondition,
							  const char *const aMessage,
							  const char *const aConditionAsString,
							  const char *const aFunction,
							  const char *const aFileName,
							  const Index   aLineNo )
{
  if( !aCondition )
    {
      log( "Assertion `%s' (%s) failed in %s (%s:%li)",
		   aMessage, aConditionAsString, aFunction, aFileName, aLineNo );
      if( assertionFailsAreFatal )
		{
		  panic( "Assertion failed" );
		}
	  else if( attachDebuggerOnError )
		{
		  Platform::getInstance() -> attachDebugger();
		}
    }
}

INLINE void Debug::testCondition( boolean aCondition,
								  const char *const aMessage,
								  const char *const aConditionAsString,
								  const char *const aFunction,
								  const char *const aFileName,
								  const Index   aLineNo )
{
  if( !aCondition )
	{
      log( "Condition `%s' (%s) not met in %s (%s:%li)",
		   aMessage, aConditionAsString, aFunction, aFileName, aLineNo );

	  if( attachDebuggerOnError )
		{
		  Platform::getInstance() -> attachDebugger();
		}
	  else
		{
		  throw ( new InternalError( aMessage, null ) );
		}
	}
}

INLINE void Debug::preCondition( boolean aCondition,
								 const char *const aMessage,
								 const char *const aConditionAsString,
								 const char *const aFunction,
								 const char *const aFileName,
								 const Index   aLineNo )
{
  testCondition( aCondition, aMessage, aConditionAsString, aFunction, aFileName, aLineNo );
}

INLINE void Debug::postCondition( boolean aCondition,
								  const char *const aMessage,
								  const char *const aConditionAsString,
								  const char *const aFunction,
								  const char *const aFileName,
								  const Index   aLineNo )
{
  testCondition( aCondition, aMessage, aConditionAsString, aFunction, aFileName, aLineNo );
}

INLINE void Debug::exception( Exception *anEx )
{
  logString( "Exception description: %s", anEx -> getFullDescription() );
}


#if defined(_INLINE)
#include "../src/String.ipp"
#endif

/* $Log: Debug.ipp,v $
 * Revision 1.5  1999/05/22 13:00:28  philogelos
 * Merging sources back from SPARC
 *
 * Revision 1.4  1999/03/03 20:43:27  philogelos
 * Switch to vararg-style argument passing
 *
 * Revision 1.3  1999/03/03 19:09:26  philogelos
 * Put sources under GNU Library License
 *
 * Revision 1.2  1999/02/28 15:46:48  philogelos
 * Tuned for inlines. Copyright updated.
 *
 * Revision 1.1.1.1  1998/11/25 20:11:01  philogelos
 * Quercus Robusta
 *
 * Revision 1.1  1998/07/09 09:30:36  philogelos
 * new files added to the repository
 * */

/* __DEBUG_IPP__ */
#endif