// C++ code Copyright (C) David R. Evans G4AMJ/NQ0I

#include <DREstring.h>

// define a handler for a fatal error
void _FE(const char* cp, const char* file, const int line)
{ char temp1[100], temp2[10];
  strcpy(temp1, cp);
  if (*cp) then
    strcat(temp1, " -- ");
  strcat(temp1, file);
  strcat(temp1, " : ");
  strcat(temp1, itoa(line, temp2, 10));
  cerr << "Fatal error: " << temp1 << endl;
  exit(-2);
}

// define a handler for nonfatal error
void _NFE(const char* cp, const char* file, const int line)
{ // trace(cp);
  char temp1[100], temp2[10];
  strcpy(temp1, cp);
  if (*cp) then
    strcat(temp1, " -- ");
  strcat(temp1, file);
  strcat(temp1, " : ");
  strcat(temp1, itoa(line, temp2, 10));
  cerr << "Nonfatal error: " << temp1 << endl;
}

// Merely setting the new_handler does not tell us where the error
// occurred. This is poor man's exception handling. I long for the
// real thing.

// a generic logging facility
void _TRACELOG(const char* msg, const char* file, const int line)
{ char temp[100];
  itoa(line, temp, 10);
  trace_log << msg << " (" << file << ":" << temp << ")" << endl << flush;
}

// the Sun does not provide itoa
char* itoa(int value, char* str, int radix)
{ if (radix == 10) then sprintf(str, "%d", value); 
  if (radix == 8) then sprintf(str, "%o", value);
  return str;
}

// the Sun does not provide ltoa
char* ltoa(long value, char* str, int radix)
{ if (radix == 10) then sprintf(str, "%d", value); 
  if (radix == 8) then sprintf(str, "%o", value);
  return str;
}

