/************************************************************************/
/* Copyright 2010 MBARI													*/
/************************************************************************/
/* Summary	: System logging functions for BEDS2 on PIC32MX470			*/
/* Filename : syslog.c													*/
/* Author	: Robert Herlien (rah)										*/
/* Project	: Respirometers												*/
/* Revision : 2.0														*/
/* Created	: 02/15/2018 from OASIS5 syslog.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								*/
/* 15feb2018 rah - created from BEDS syslog.c and Oasis5 syslog.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 <fileUtils.h>					/* BEDS file utilities definitions	*/
#include <file.h>						/* BEDS File I/O Routines			*/
#include <fatFs/ff.h>					/* FatFs definitions				*/

#include <stdio.h>						/* Standard I/O library				*/
#include <time.h>
#include <string.h>						/* String library functions			*/
#include <stdarg.h>
#include <errno.h>

#define LOGBUF_SIZE		512


/********************************/
/*		External Data			*/
/********************************/

Extern Int32	syslogToConsole;		/* 0=No echo, 1=echo to console, 2=echo to TPU*/
Extern MBool	sdOK;


/********************************/
/*		Module Local Data		*/
/********************************/

MLocal const char *logNames[] =
	{"SYSLOG.TXT", "IMULOG.TXT", "ZMODEM.TXT"};

MLocal FIL			logfp[NumberOf(logNames)];
MLocal char			logBuf[LOGBUF_SIZE];


/************************************************************************/
/* 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],	"IMULOG.%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)
{
	time_t		now;
	FRESULT		fres;
	FIL			*fp;
	Int32		rtn;
	struct tm	*tmp;
	SerPort		consoleSave;
	Nat16		ms;

	if (!sdOK)
		return(0);

	if ((ltype >= NumberOf(logNames)) ||
		((fres = f_open(&logfp[ltype], logNames[ltype],
						(BYTE)(FA_WRITE | FA_OPEN_APPEND))) != FR_OK))
	{
		fperr(fres, "Error opening syslog");
		return(0);
	}
		
	clkGetTime(&now, &ms);
	tmp = gmtime(&now);
	fp = &logfp[ltype];

	if ((rtn = f_printf(fp, "%10d.%03d, %02d/%02d/%04d %02d:%02d:%02d, ",
						now, ms, tmp->tm_mon+1, tmp->tm_mday, tmp->tm_year+1900,
						tmp->tm_hour, tmp->tm_min, tmp->tm_sec)) > 0)
	{
//		vsnprintf(logBuf, LOGBUF_SIZE-2, fmt, vlist);
		vsprintf(logBuf, fmt, vlist);
		strcat(logBuf, "\r\n");
		rtn += f_puts(logBuf, fp);
	}

	f_close(fp);

	if (syslogToConsole && (ltype == LOG_BEDS))
	{
		consoleSave = getConsole();
		if (setConsole(syslogToConsole) == OK)
		{
			printf("%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);

			vprintf(fmt, vlist);
			newline();
		}

		setConsole(consoleSave);
	}

	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() */
