/************************************************************************/
/* 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 <oasis.h>						/* OASIS controller definitions		*/
#include <syslog.h>						/* Oasis System logger				*/
#include <file.h>						/* OASIS File I/O Routines			*/
#include <timer.h>						/* OASIS clock routines				*/
#include <debug.h>
#include <fatfs/ff.h>

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

#define LOGBUF_SIZE		512


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

MLocal const char	*sysLogName = "SYSLOG.TXT";
MLocal const char	*logBakName = "SYSLOG.%03d";
MLocal FIL			logfp;				/* Can be global, protected by fileSem*/
MLocal char			logBuf[LOGBUF_SIZE];/* Ditto						*/

#define oldName		logBuf
#define newName		(logBuf + (LOGBUF_SIZE/2))


/************************************************************************/
/* Function	   : sysLogInit												*/
/* Purpose	   : Initialize system logger								*/
/* Inputs	   : None													*/
/* Outputs	   : OK or ERROR											*/
/************************************************************************/
Errno sysLogInit(Void)
{
#if 0
	Int16		i;
	FILE		*logfp;

	for (i = BACKUP_SYSLOG_FILES-1; i; i--)
	{
		sprintf(newName, logBakName, i+1);
		sprintf(oldName, logBakName, i);
		f_unlink(newName);
		f_rename(oldName, newName);
	}

	f_rename(sysLogName, oldName);
#endif
	sysLogPrintf("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(char *fmt, va_list vlist)
{
	time_t		now;
	FRESULT		fres;
	Int32		rtn;
	struct tm	*tmp;
	Nat16		ms;
 
	takeFileSem();

	if ((fres = f_open(&logfp, sysLogName,
					   (BYTE)(FA_WRITE | FA_OPEN_APPEND))) != FR_OK)
	{
		giveFileSem();
		return(0);
	}
	
	clkGetTime(&now, &ms);
	tmp = gmtime(&now);

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

	f_close(&logfp);
	giveFileSem();
	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(char *fmt, ...)
{
	va_list		ap;
 
	va_start(ap, fmt);
	return(vSysLogPrintf(fmt, ap));

} /* sysLogPrintf() */
