/*
 * Desktop Chat Server - debug.c
 * (C) Joseph Heenan, 1997
 *
 * $Log: debug,v $
 * Revision 1.4  1998/08/16 13:52:37  joseph
 * Added debug_enable and debug_disable functions
 *
 * Revision 1.3  1998/06/08 16:40:26  joseph
 * Improved debug routines, removed/inserted some debugging
 *
 * Revision 1.2  1998/05/28 17:14:31  joseph
 * Replaced with latest version (from NewsHound)
 *
 *
 */

#include "debug.h"

static int debug_enabled = 0;

void debug_disable( void )
{
  debug_enabled = 0;
}

#ifdef DEBUG

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

#include "kernel.h"
#include "wimplib.h"
#include "macros.h"
#include "defines.h"

static char	debug_filename[ 256] = "";

#define debug_checkinit() { if (debug_filename[0]==0) debug_initialise(); }

void debug_enable( void )
{
  debug_enabled = 1;
}


static void debug_initialise(void)
{
  int handle;
  char command[320];
  sprintf( debug_filename, "Pipe:$.WebGet.debug" );
  freopen( debug_filename, "w", stderr);
  setbuf( stderr, NULL);
  sprintf( command, "taskwindow \"type %s\" -wimpslot 16k -name \"Debug output\" -quit", debug_filename);

  E_CHECK( wimp_start_task( command, &handle ) );
}


int debug_printf_int(const char *format, ...)
{
  va_list  va;
  int      i;

  if ( !debug_enabled ) return 0;

  debug_checkinit();
  va_start(va, format);
  i = vfprintf( stderr, format, va);
  va_end(va);

  return i;
}



void debug_print(const char *text)
{
  if ( !debug_enabled ) return;

  debug_checkinit();
  fputs( text, stderr);
}

#endif

