/******************************************************************************
 * $Id: gfx.cc,v 1.20 2006/11/04 00:02:56 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 gfx library (not the gfxsys).
 * @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 );
    atexit( GFX_CLEANUP );
#if GFXSYS_OPENGL
  //GFX_OPENGL_CHECK();  // such a function won't work, see gfx_opengl.cc
#endif
	return true;
}

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

	    gExitingGfx = true;  // this disables many things
	}
}

/*****************************************************************************
 * 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 = &cerr;

	// 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
