/*
    ####             #    #     # #
    #   #            #    #       #          The FreeWare C library for
    #   #  ##   ###  #  # #     # ###             RISC OS machines
    #   # #  # #     # #  #     # #  #   ___________________________________
    #   # ####  ###  ##   #     # #  #
    #   # #        # # #  #     # #  #    Please refer to the accompanying
    ####   ### ####  #  # ##### # ###    documentation for conditions of use
    ________________________________________________________________________

    File:    Debug1.c.uniquepipe
    Author:  Julian Smith, with clever taskwindow code from Paul Field and Cy Booker.
    Version: 0.00 (04 Jun 1995)
    Purpose: Provides a set of Debug_ function which send stuff to a unique
             file in a given directory.
*/


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

#include "Desk.Error.h"
#include "Desk.WimpSWIs.h"

#include "Desk.Debug.h"
#include "Desk.Str.h"

#include "Defs.h"


static char	Desk_debug_filename[ 256] = "";



#define Desk_Debug__EnsureInitialised()	if ( Desk_debug_filename[0]==0)	Desk_Debug_Initialise()

void	Desk_Debug_Initialise( void)
/* Simply redirect stderr to a unique file in pipe:, and	*/
/* open a taskwindow which will *Type out stderr.		*/
/* Aren't taskwindows wonderful?				*/
{
char	command[ 320];
sprintf( Desk_debug_filename, "Pipe:$.DeskLib.%s", Desk_LeafName( tmpnam( NULL)));
freopen( Desk_debug_filename, "w", stderr);
setbuf( stderr, NULL);
sprintf( command, 
	"taskwindow \"type %s\" -wimpslot 16k -name \"Debug output\" -quit", 
	Desk_debug_filename
	);

Desk_Wimp_StartTask( command);
}


int	Desk_Debug_VPrintf( const char *format, va_list va)
	{
	Desk_Debug__EnsureInitialised();
	Desk_Debug__LineNestingPrefix( stderr);
	return vfprintf( stderr, format, va);
	}




int	Desk_Debug_Printf( const char *format, ...)
{
va_list	va;
int	i;
va_start( va, format);
i = Desk_Debug_VPrintf( format, va);
va_end(va);

return i;
}



void	Desk_Debug_Print( const char *text)
{
Desk_Debug__EnsureInitialised();
Desk_Debug__LineNestingPrefix( stderr);
fputs( text, stderr);
}


