//-----------------------------------------------------------------------------
// Copyright © 2003 - Philip Howard - All rights reserved
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//-----------------------------------------------------------------------------
// package	libh/debug
// homepage	http://libh.slashusr.org/
//-----------------------------------------------------------------------------
// author	Philip Howard
// email	libh at ipal dot org
// homepage	http://phil.ipal.org/
//-----------------------------------------------------------------------------
// This file is best viewed using a fixed spaced font such as Courier
// and in a display at least 120 columns wide.
//-----------------------------------------------------------------------------

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>

//-----------------------------------------------------------------------------
// Declare and initialize static variables.
//-----------------------------------------------------------------------------
static FILE *	debug_file	= NULL	;
static int	debug_level	= 0	;
static int	debug_ready	= 0	;


//-----------------------------------------------------------------------------
// function	debug_set_level
//
// purpose	Set the debug level to be used to determine which debug
//		messages are output and which are not.  The higher the
//		level, the more messages are output.
//
// arguments	1 (int) new debug level to use
//
// returns	(void)
//-----------------------------------------------------------------------------
void
(debug_set_level) (
    int		new_level
    )
{
    debug_level = new_level;
    debug_ready = 1;
    return;
}

//-----------------------------------------------------------------------------
// function	debug_set_file
//
// purpose	Set a different (already open) standard C file to use to
//		output debug messages.
//
// arguments	1 (FILE *) pointer to open file to use
//
// returns	(void)
//-----------------------------------------------------------------------------
void
(debug_set_file) (
    FILE *	new_file
    )
{
    debug_file = new_file;
    return;
}

//-----------------------------------------------------------------------------
// function	debug
//
// purpose	Output a programmed debug message if the level specified with
//		the message is equal to or higher than the current debug level
//		setting.  Higher levels on messages are for more detailed
//		output that is less frequently requested.
//
// arguments	1 (int) minimum level setting required to output this message
//		2 (const char *) format string that defines this message
//		3..N+2 various arguments used in the message format
//
// returns	(int) 0 debug message not output
//		(int) 1 debug message was output
//-----------------------------------------------------------------------------
void
(debug) (
    int			arg_msg_level
    ,
    const char *	arg_msg_format
    ,
    ...
    )
{
    va_list		arg_list	;

    if ( ! debug_ready ) {
	char *	env_level	;
	env_level = getenv( "DEBUG_LEVEL" );
	debug_level = ( env_level && env_level[0] ) ? strtol( env_level, NULL, 0 ) : 10;
	debug_ready = 1;
    }

    if ( ! debug_file ) {
	debug_file = stderr;
    }

    if ( arg_msg_level < debug_level && debug_file ) {
	va_start( arg_list, arg_msg_format );
	vfprintf( debug_file, arg_msg_format, arg_list );
	fputc( '\n', debug_file );
	if ( arg_msg_level < 100 ) fflush( debug_file );
	va_end( arg_list );
    }

    return;
}


