/************************************************************************/
/* Copyright 2010 MBARI							*/
/************************************************************************/
/* Summary  : System logging functions for Respirometer Controller (O4)	*/
/* Filename : syslog.c							*/
/* Author   : Robert Herlien (rah)					*/
/* Project  : Respirometers						*/
/* Revision : 1.0							*/
/* Created  : 10/01/2010 from OASIS log.c				*/
/*									    */
/* MBARI provides this documentation and code "as is", with no warranty,    */
/* express or implied, of its quality or consistency. It is provided without*/
/* support and without obligation on the part of the Monterey Bay Aquarium  */
/* Research Institute to assist in its use, correction, modification, or    */
/* enhancement. This information should not be published or distributed to  */
/* third parties without specific written permission from MBARI.            */
/*									    */
/************************************************************************/
/* Modification History:						*/
/* 01oct2010 rah - created from OASIS log.c
*/
/************************************************************************/

#include <mbariTypes.h>			/* MBARI type definitions	    */
#include <mbariConst.h>			/* MBARI constants		    */
#include <beds.h>			/* BEDS controller definitions	    */
#include <syslog.h>			/* BEDS System logger		    */
#include <clock.h>			/* BEDS Clock module		    */
#include <serial.h>			/* BEDS serial I/O definitions	    */
#include <io.h>				/* BEDS I/O definitions		    */
#include <fileUtils.h>		/* BEDS file utilities definitions	*/

#include <cfxpico.h>			/* Persistor PicoDOS definitions    */
#include <stdio.h>			/* Standard I/O library		    */
#include <time.h>
#include <string.h>			/* String library functions	    */
#include <stdarg.h>
#include <errno.h>


/********************************/
/*	External Data		*/
/********************************/

Extern Int32	syslogToConsole;	/* 0=No echo, 1=echo to console, 2=echo to TPU*/


/********************************/
/*	Module Local Data	*/
/********************************/

MLocal const char *logNames[] =
    {"SYSLOG.TXT", "MPULOG.TXT", "ZMODEM.TXT"};


/************************************************************************/
/* Function    : sysLogInit						*/
/* Purpose     : Initialize system logger				*/
/* Inputs      : None							*/
/* Outputs     : OK or ERROR						*/
/************************************************************************/
Errno sysLogInit(Void)
{
    backupFile(logNames[LOG_BEDS],  "SYSLOG.%03d");
    backupFile(logNames[LOG_MPU],   "MPULOG.%03d");
    backupFile(logNames[LOG_ZMODEM], "ZMODEM.%03d");

    sysLog("SysLog started");
    return(OK);

} /* sysLogInit() */


/************************************************************************/
/* Function    : vSysLogPrintf						*/
/* Purpose     : Write an entry into the system log file, vprintf style	*/
/* Inputs      : Format, va_list					*/
/* Outputs     : Number of chars printed				*/
/************************************************************************/
Int32 vSysLogPrintf(SysLogType ltype, const char *fmt, va_list vlist)
{
    FILE	*logfp;
    time_t	now;		/* Current time_t			*/
    Nat16	ms;		/* Current milliseconds			*/
    Int32	rtn;
    struct tm	*tmp;

    if ((ltype >= NumberOf(logNames)) ||
	((logfp = fopen(logNames[ltype], "a+")) == NULL))
	return(0);

    clkGetTime(&now, &ms);
    tmp = gmtime(&now);

    rtn = fprintf(logfp, "%04d/%02d/%02d %02d:%02d:%02d.%03hd, ",
		  tmp->tm_year+1900, tmp->tm_mon+1, tmp->tm_mday,
		  tmp->tm_hour, tmp->tm_min, tmp->tm_sec, ms);

    rtn += vfprintf(logfp, fmt, vlist);
    if (fmt[strlen(fmt)-1] != '\n')
	rtn += fprintf(logfp, "\r\n");
    fclose(logfp);


    if (syslogToConsole && (ltype == LOG_BEDS))
    {
	char	buf[512];

	sprintf(buf, "%04d/%02d/%02d %02d:%02d:%02d.%03hd, ",
		tmp->tm_year+1900, tmp->tm_mon+1, tmp->tm_mday,
		tmp->tm_hour, tmp->tm_min, tmp->tm_sec, ms);

	vsprintf(buf + strlen(buf), fmt, vlist);

	if (buf[strlen(buf)-1] != '\n')
	    sprintf(buf + strlen(buf), "\n");

	if (syslogToConsole == 1)	/* Echo syslog to console port		*/
	{
	    EIAForceOff(FALSE);
	    consolePrintf(buf);
	    CIOdrain();
	}
	else
	{
	    TUPort *tp = getTPUPort();
	    altSerEnable(TRUE);
	    TUTxPrintf(tp, buf);
	    tdrain(tp);
	}
    }

    return(rtn);

} /* vSysLogPrintf() */


/************************************************************************/
/* Function    : sysLogPrintf						*/
/* Purpose     : Write an entry into the system log file, printf style	*/
/* Inputs      : String to write, arguments				*/
/* Outputs     : Number of chars printed				*/
/************************************************************************/
Int32 sysLogPrintf(const char *fmt, ...)
{
    va_list	ap;
 
    va_start(ap, fmt);
    return(vSysLogPrintf(LOG_BEDS, fmt, ap));

} /* sysLogPrintf() */
