/************************************************************
**
** Application: CJLib
**
** Title:       c.debug
**
*************************************************************/

/* Include files */
/* from standard clib */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>

/* from oslib */
#include "oslib/os.h"
#include "oslib/osfile.h"
#include "OSLib:osword.h"

/* from CJLib */
#include "CJLib:etc.h"

/* from application */
#include "file.h"
#include "debug.h"



/************************************************************/

/* exported variables */

/* global variables */

/* forward function declarations */


/************************************************************/


static osbool dbug_on;
static char *app_name;
static char *app_dir;
static char dbug_fname[256];
static FILE * dbfp;



/******************************************************************************/

void CJL_db_printf (osbool close, char * format, ...)

{
  va_list   args;
  char      v[512];


  if (!dbug_on) return;

  va_start (args, format);
  vsprintf (v, format, args);

  if (!dbfp) dbfp = fopen (dbug_fname, "ab+");
  if (dbfp)
  {
    fprintf (dbfp,"%s\n",v);
    if (close)
    {
      fclose(dbfp);
      dbfp=NULL;
    }
  }
  va_end (args);
}




/******************************************************************************/

static int convert_bcd_to_dec (int bcd_val)

{
  return ( (bcd_val & 0xf) + ((bcd_val >> 4) & 0xf) * 10 );
}


/******************************************************************************/

void CJL_db_ts_printf (osbool close, char * format, ...)

{
  va_list   args;
  char      v[512];
  oswordreadclock_local_bcd_block bcd;
  int h, m, s;

  if (!dbug_on) return;

  va_start (args, format);
  vsprintf (v, format, args);

  if (!dbfp) dbfp = fopen (dbug_fname, "ab+");
  if (dbfp)
  {
    /* Read the clock in bcd format and convert to decimal */
    bcd.op = oswordreadclock_OP_LOCAL_BCD;
    oswordreadclock_local_bcd (&bcd);

    h = convert_bcd_to_dec (bcd.date_and_time.hour);
    m = convert_bcd_to_dec (bcd.date_and_time.minute);
    s = convert_bcd_to_dec (bcd.date_and_time.second);

    fprintf (dbfp, "%.2d:%.2d:%.2d %s\n", h, m, s, v);

    if (close)
    {
      fclose (dbfp);
      dbfp = NULL;
    }
  }
  va_end (args);
}





/******************************************************************************/

void CJL_db_force_enable (osbool state)
{
  dbug_on = TRUE;
  if (state)
  {
    CJL_db_ts_printf (TRUE, "Debug logging set to enabled");
  }
  else
  {
    CJL_db_ts_printf (TRUE, "Debug logging set to disabled");
  }
  dbug_on = state;
  return;
}





/******************************************************************************/

void CJL_db_clear_file (void)
{

  return;
}





/******************************************************************************/

void CJL_db_close_file (void)
{
  if (dbfp)
  {
    fclose(dbfp);
    dbfp=NULL;
  }

  return;
}





/******************************************************************************/

void  CJL_db_init ( osbool clear, char *name, char *dir )

{
  char  string[256];


  app_name = name;
  app_dir = dir;

  /* make dbug file name */
  dbug_on = TRUE;
  sprintf (dbug_fname, "%s.dbugout", app_dir );
  /* Do we start with an empty file */
  if (clear)
  {
    dbfp = fopen (dbug_fname, "wb");
  }
  else
  {
    dbfp = fopen (dbug_fname, "ab+");
  }
  CJL_db_ts_printf (TRUE, "%s debug logging initialised", app_name);
  /* Set type to text */
  osfile_set_type (dbug_fname, osfile_TYPE_TEXT);
  /* Check if debugging is enabled by !Run file */
  /* The default is not */
  sprintf (string, "%s$Debug", app_name );
  if ( getenv (string) )
  {
    dbug_on = TRUE;
    CJL_db_ts_printf (TRUE, "Debug logging is enabled in !Run file");
  }
  else
  {
    dbug_on = FALSE;
    CJL_db_ts_printf (TRUE, "Debug logging is disabled in !Run file");
  }
  return;
}





/******************************************************************************/

