#define ERROR
#include <Threading.h>
#include <time.h>
extern pthread_mutex_t error_mutex;

extern int	VerboseOn;

/*
   The error manager handles all the messages the program generates,
   this means that only one thread is outputting to the stdout.  It also means
   that it would be very easy to pipe this to a log file, or syslog or something
   because you only have to change the code in here.
*/

char    ltoaret[1024];
char    *ltoa( long no );


int Error(int type, char *message, ...)
{
va_list	ap;
char	buffer[8000];
time_t	gtime;
struct  tm *ltime;
int	i=0;
char	*p;
long	l;
int	pos=0;
int	c,d;
char	Out[8000];
int	len=0;
int	k=0;

if (( type == 10 ) && ( VerboseOn == 0 ))
   return 0;

/* Parse the arguments list */
len = strlen(message);

strcpy(Out,"");

va_start(ap, message);

while( pos <= len )
   {
   if ( message[pos] == '%' )
      {
      pos++;
      switch (message[pos])
         {
         case 's':
             p = va_arg(ap, char *);
             strcat(Out,p);
             //free(p);
             break;
         case 'i': 
             d = va_arg(ap, int);
             strcat(Out,ltoa((long)d));
             break;
         case 'l':
             l = va_arg(ap, long);
             strcat(Out,ltoa(l));
             if ( message[pos+1] == 'd' )
                pos++;  /* To skip the %ld */
             break;
         case 'c':
             c = va_arg(ap, int);
             k = strlen(Out);
             Out[k] = c;
             k++;
             Out[k] = 0;
             break;
         case '%':
             k = strlen(Out);
             Out[k] = message[pos];
             k++;
             Out[k] = 0;
             break;
         default:
             Error(1,"Error was called with an incomplete format string.");
             break;
         } 
      }
   else      
      {
      k = strlen(Out);
      Out[k] = message[pos];
      k++;
      Out[k] = 0;
      }
   pos++;
   }
va_end(ap);

/* Add timestamp */
time(&gtime);
ltime = localtime(&gtime);
if ( type == 0 )
   sprintf(buffer,"%02i/%02i/%02i %02i:%02i:%02i msg %s\r\n",ltime->tm_mon+1,ltime->tm_mday,ltime->tm_year,ltime->tm_hour,ltime->tm_min,ltime->tm_sec,Out);
if ( type == 1 )
   sprintf(buffer,"%02i/%02i/%02i %02i:%02i:%02i Wrn %s\r\n",ltime->tm_mon+1,ltime->tm_mday,ltime->tm_year,ltime->tm_hour,ltime->tm_min,ltime->tm_sec,Out);
if ( type == 2 )
   sprintf(buffer,"%02i/%02i/%02i %02i:%02i:%02i ERR %s\r\n",ltime->tm_mon+1,ltime->tm_mday,ltime->tm_year,ltime->tm_hour,ltime->tm_min,ltime->tm_sec,Out);
if ( type == 3 )
   sprintf(buffer,"%02i/%02i/%02i %02i:%02i:%02i CRT %s\r\n",ltime->tm_mon+1,ltime->tm_mday,ltime->tm_year,ltime->tm_hour,ltime->tm_min,ltime->tm_sec,Out);
if (( type > 4 ) && ( type < 9 ))
   sprintf(buffer,"%02i/%02i/%02i %02i:%02i:%02i BAD %s\r\n",ltime->tm_mon+1,ltime->tm_mday,ltime->tm_year,ltime->tm_hour,ltime->tm_min,ltime->tm_sec,Out);
if ( type == 9 )
   sprintf(buffer,"%s\r\n",Out);
if ( type == 10 )
   sprintf(buffer,"%02i:%02i:%02i %s\r\n",ltime->tm_hour,ltime->tm_min,ltime->tm_sec,Out);

/* Write to buffer */
pthread_mutex_lock(&error_mutex);
while ( i < ErrorPathCount )
   {
   fputs(buffer,ErrorPath[i]);
   i++;
   }
pthread_mutex_unlock(&error_mutex);

return 0;
}

char *ltoa( long no )
{
sprintf(ltoaret,"%ld",no);
return ltoaret;
}
