/******************************************************************************
 * $Id: gfx.cc,v 1.16 2006/07/23 02:04:39 devel Exp $
 * @file
 * @author	Jim E. Brooks  http://www.jimbrooks.org
 * @brief	Initialization.
 *//*
 * LEGAL:	COPYRIGHT (C) 2004 JIM E. BROOKS
 *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
 *          OF THE GNU GENERAL PUBLIC LICENSE (GPL).
 ******************************************************************************/

/// @namespace gfx
/// @brief Graphics library namespace (including non-graphics utility code).

#define GFX_CC 1
#include "gfx.hh"
#include "gfx_misc.hh"
#include "gfx_file.hh"
#include "gfx_stream.hh"
namespace gfx {

// True when program is exiting.
bool gExitingGfx = false;

// Streams for printing to console.
ostream* gStreamOut;	// don't delete these which may be assigned cout/cerr
ostream* gStreamError;
ostream* gStreamDebug;

static string sErrorLogFname = "ERROR_LOG.txt";
static FILE* sErrorLogFile;

/*****************************************************************************
 * Initialize library.
 * @param	appname
 *			Set the program's name to let the user know which program had errors.
 *****************************************************************************/
bool
GFX_INIT( const string& appname )
{
	sErrorLogFname = appname + "_ERROR_LOG.txt";
	GfxOpenStreams();
	GFX_SET_APPNAME( appname );
	extern bool GFX_SET_READY( bool ready );
	GFX_SET_READY( false );		// underlying gfxsys isn't ready however
    atexit( GFX_CLEANUP );
	return true;
}

/*****************************************************************************
 * Cleanup library.
 *****************************************************************************/
void
GFX_CLEANUP( void )
{
	// Was called?
    static int ran = 0;
    if ( ran++ == 0 )
	{
	    gExitingGfx = true;
		GfxCloseStreams();
	}
}

/*****************************************************************************
 * Set the program's name to let the user know which program had errors.
 *****************************************************************************/
static string sAppname;
void
GFX_SET_APPNAME( const string& appname )
{
	sAppname = appname;
}
string
GFX_GET_APPNAME( void )
{
	return sAppname;
}

/*****************************************************************************
 * Open/close global stream objects.
 *****************************************************************************/
static void
GfxOpenStreams( void )
{
	gStreamOut   = &cout;
	gStreamDebug = &cout;

	// gStreamError prints to both stderr and a log file.
	sErrorLogFile = fopen( sErrorLogFname.c_str(), "w" );
	if ( sErrorLogFile )
		gStreamError = new ostream( new StreambufFile2( stderr, sErrorLogFile, false, false ) );
	else
		gStreamError = &cerr; // couldn't open log file
}
static void
GfxCloseStreams( void )
{
	// -- Don't delete the stream objects in case they're actually cout/cerr --

	// Close error file, remove if empty.
	if ( sErrorLogFile ) // if it was opened ok
	{
		bool errorLogEmpty = (ftell( sErrorLogFile ) == 0);
		fclose( sErrorLogFile );
		sErrorLogFile = NULL;
		if ( errorLogEmpty ) RemoveFile( sErrorLogFname );
	}
}

} // namespace gfx
