/**************************************************************
$Workfile:   eventlog.c  $
***************************************************************
**
** FIRMWARE, Copyright (C) 2000-2002
** Ikanos Communication, Fremont, CA
** All rights reserved.
**
** The information and contents of this file are the
** proprietary information of Ikanos Communication and may not
** be disclosed or used without the formal written approval of
** Ikanos Communication
**
***************************************************************/
#ifdef USE_VXWORKS /* IXDP425 */
#include <vxWorks.h>
#endif
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#ifdef USE_VXWORSK /* IXDP425 */
#include "drv/timer/timestampDev.h"
#endif
#include "apiosmacro.h"
#include "eventlog.h"
#ifndef USE_VXWORKS /* IXDP425 */
#include <v2pthread.h>
#endif
int EventLogEnable=TRUE;

// Global variables
tEvent EventBuffer[MAX_EVENT_LOGS];

int FirstEvent;
int LastEvent;

tEvent UserLogBuffer[MAX_EVENT_LOGS];

int FirstUserEvent;
int LastUserEvent;
#ifdef ALASKA
extern int webflag;
#endif


void EventLog(const char *output, ...)
	{
	int count;
	va_list ap;
	int key;

	/* Disable interrupts here */
	key = iposIntLock();			/* TT ID 1159 */

	if (EventLogEnable==TRUE)
	{
	LastEvent++;
	if (LastEvent==MAX_EVENT_LOGS)
		LastEvent=0;

	if (LastEvent<=FirstEvent)
		{
		FirstEvent++;
		if (FirstEvent==MAX_EVENT_LOGS)
			FirstEvent=0;

		}

	EventBuffer[LastEvent].EventString=(char*)output;

	
	/*time(&present_time);

	localtime_r(&present_time,&EventBuffer[LastEvent].TimeStamp);
	*/
	EventBuffer[LastEvent].time=0/*sysTimestamp()*/;

    va_start(ap, output);
    for (count = 0; count < MAX_EVENT_PARMS; count++)
    {
        EventBuffer[LastEvent].VarList[count] = va_arg(ap, S32);
    }
    va_end(ap);

	} /* if (EventLogEnable==TRUE) */


	/* Enable interrupts again */
	iposIntUnlock(key);			/* TT ID 1159 */


	} /* eventlog */


void InitEventLogs()
	{	
	int count;

	for (count=0;count<MAX_EVENT_LOGS;count++)
		EventBuffer[count].EventString=NULL;

	FirstEvent=0;
	LastEvent=0;

	}


void PrintEventLogs()
	{
	
	int count;
	int LastEventFlag=FALSE;
#ifdef ALASKA
        FILE *vdslout;
	if(webflag)
        {
	  vdslout = fopen("/tmp/vdslcmd.out","w");
	  if(vdslout == NULL)
	  {
	    perror("PrintEventLogs(): cannot open /tmp/vdslcmd.out in write mode");
	    return;
	  }
	  fprintf (vdslout,"\n Host Software Event Logs ");
	  fprintf (vdslout,"\n===================================");
	}
	else
	{
#endif
	  printf ("\n Host Software Event Logs ");
	  printf ("\n===================================");
#ifdef ALASKA
	}
#endif

	count=FirstEvent-1;

	do
	{
		count++;
		if (count==MAX_EVENT_LOGS)
			count=0;

		if(EventBuffer[count].EventString!=NULL)
			{
#ifdef ALASKA
                        if(!webflag)
                        {
#endif
			printf ("\n%.2i:",(int)EventBuffer[count].time);

			printf (EventBuffer[count].EventString,
						EventBuffer[count].VarList[0],
						EventBuffer[count].VarList[1],
						EventBuffer[count].VarList[2],
						EventBuffer[count].VarList[3],
						EventBuffer[count].VarList[4]);
#ifdef ALASKA
                        }
                        else
                        {
			fprintf (vdslout,"\n%.2i:",(int)EventBuffer[count].time);

			fprintf (vdslout,EventBuffer[count].EventString,
						EventBuffer[count].VarList[0],
						EventBuffer[count].VarList[1],
						EventBuffer[count].VarList[2],
						EventBuffer[count].VarList[3],
						EventBuffer[count].VarList[4]);
			}
#endif
			} 

		
		if (count==LastEvent) LastEventFlag=TRUE;

	} while (!LastEventFlag); /* while */
#ifdef ALASKA
        if(webflag)
          fclose(vdslout);
        printf("\n");
#endif
	} /* PrintEventLogs */


void UserLog(const char *output, ...)
	{
	int count;
	va_list ap;
	int key;
	time_t present_time;

	/* Disable interrupts here */
	key = iposIntLock();			/* TT ID 1159 */


	LastUserEvent++;
	if (LastUserEvent==MAX_USER_LOGS)
		LastUserEvent=0;

	if (LastUserEvent<=FirstUserEvent)
		{
		FirstUserEvent++;
		if (FirstUserEvent==MAX_USER_LOGS)
			FirstUserEvent=0;

		}

	UserLogBuffer[LastUserEvent].EventString=(char*)output;

	
	time(&present_time);

	localtime_r(&present_time,&UserLogBuffer[LastUserEvent].TimeStamp);
	

	//UserLogBuffer[LastUserEvent].time=sysTimestamp();

    va_start(ap, output);
    for (count = 0; count < MAX_USER_PARMS; count++)
    {
        UserLogBuffer[LastUserEvent].VarList[count] = va_arg(ap, S32);
    }
    va_end(ap);




	/* Enable interrupts again */
	iposIntUnlock(key);			/* TT ID 1159 */


	} /* eventlog */


void InitUserLogs()
	{	
	int count;

	for (count=0;count<MAX_USER_LOGS;count++)
		UserLogBuffer[count].EventString=NULL;

	FirstUserEvent=0;
	LastUserEvent=0;

	}


void PrintUserLogs()
	{
	
	int count;
	int LastEventFlag=FALSE;
#ifdef ALASKA
        FILE *vdslout;

        if(webflag)
        {
          vdslout = fopen("/tmp/vdslcmd.out","w");
	  if(vdslout == NULL)
	  {
	    perror("PrintUserLogs(): cannot open /tmp/vdslcmd.out in write mode");
	    return;
	  }
	  fprintf (vdslout,"\n Host Software Event Logs ");
	  fprintf (vdslout,"\n===================================");
        }
        else
        {
#endif
	  printf ("\n Host Software Event Logs ");
	  printf ("\n===================================");
#ifdef ALASKA
        }
#endif

	count=FirstUserEvent-1;

	do
	{
		count++;
		if (count==MAX_USER_LOGS)
			count=0;

		if(UserLogBuffer[count].EventString!=NULL)
			{
#ifdef ALASKA
                if(!webflag)
                {
#endif
			printf ("\n%.2i:%.2i:%.2i:day %i: "
				,(int)UserLogBuffer[count].TimeStamp.tm_hour
				,(int)UserLogBuffer[count].TimeStamp.tm_min
				,(int)UserLogBuffer[count].TimeStamp.tm_sec
				,(int)UserLogBuffer[count].TimeStamp.tm_mday);

			printf (UserLogBuffer[count].EventString,
						UserLogBuffer[count].VarList[0],
						UserLogBuffer[count].VarList[1],
						UserLogBuffer[count].VarList[2],
						UserLogBuffer[count].VarList[3],
						UserLogBuffer[count].VarList[4]);
#ifdef ALASKA
                }
                else
                {
			fprintf (vdslout,"\n%.2i:%.2i:%.2i:day %i: "
				,(int)UserLogBuffer[count].TimeStamp.tm_hour
				,(int)UserLogBuffer[count].TimeStamp.tm_min
				,(int)UserLogBuffer[count].TimeStamp.tm_sec
				,(int)UserLogBuffer[count].TimeStamp.tm_mday);

			fprintf (vdslout,UserLogBuffer[count].EventString,
						UserLogBuffer[count].VarList[0],
						UserLogBuffer[count].VarList[1],
						UserLogBuffer[count].VarList[2],
						UserLogBuffer[count].VarList[3],
						UserLogBuffer[count].VarList[4]);
                }
#endif
			} 

		
		if (count==LastUserEvent) LastEventFlag=TRUE;

	} while (!LastEventFlag); /* while */
#ifdef ALASKA
        if(webflag)
          fclose(vdslout);
	printf("\n");
#endif

	} /* PrintEventLogs */
