/////////////////////////////////////////////////////////////////////
// This open-source software sample is offered free of charge and without guarantee.
// Polhemus accepts no responsibility or liability for any use of this code.
/////////////////////////////////////////////////////////////////////
// Adapted from "A Simple LogFile" original version, written by Neil Yao, 19 Mar 2003
// http://www.codeproject.com/Articles/2486/A-Simple-LogFile
/////////////////////////////////////////////////////////////////////
//  VSS $Header: /Viper/VPCmdIF/VPCmdIF/LogFile.h 1     8/18/17 5:12p Suzanne $  
///@cond
#ifndef _ATA_LOGFILE_
#define _ATA_LOGFILE_
#include <cstdio>
#include "PTTypeDefs.h"

/////////////////////////////////////////////////////////////////
//features:	not depends on MFC ore ATL.
//			file name could use absolute path or just the name, in which case the 
//			file will be created at the same place with the binary module, no concern 
//			with curret directory, which always bring me truble.
//			every log line has a time stamp attached, with million seconds.
//			uses printf like format to write log lines
//			uses a preprocessor definition _DEBUG_LOG to switch if write log file
//			multi thread safe, finally added:)

////////////////////////////////////////////////////
//	CLogFile, a debug log file wrapper

#define SZLOGLINE_LEN 2048
#define SZLOGTIME_LEN 100
class PDEBUG_API CLogFile
{
public:

	//////////////////////////////
	//	Constructor, open the logfile
	CLogFile(LPCTSTR strFile=_T("POLHEMUS.LOG"), bool bTS=true, bool bAppend = false, int lTruncate = 0x100000, int nFlushCount=0)
	{
		m_lTruncate = lTruncate;
		m_bTS = bTS;
		m_pLogFile = 0;
		m_nFlushCount = nFlushCount;
		m_nWriteCount = 0;
		_tcscpy_s(m_szFile, _countof(m_szFile), strFile);
	}
	/////////////////////////////////
	//	Destructor, close if logfile if opened
	~CLogFile()
	{
		Close();
	}
	void Open( int nFlushCount=0, bool bAppend=false, bool bTS=true )
	{
		if (m_pLogFile)
		{
			Close();
		}
		m_bTS = bTS;
		m_pLogFile = _tfsopen(m_szFile, bAppend ? _T("a") : _T("w"), _SH_DENYNO );
		m_nFlushCount = nFlushCount;
		m_nWriteCount = 0;
	}
	void Close()
	{
		if (m_pLogFile)
		{
			fputs("\n===============Finished Logging================\n\n", m_pLogFile);
			Flush();
			fclose(m_pLogFile);
			m_pLogFile = 0;
		}
	}
	/////////////////////////////////
	//	Write log info into the logfile, with printf like parameters support

	void Write(LPCSTR pszFormat, ...);
	
	void WriteNoFlush(LPCSTR pszFormat, ...);
	
	void VWrite(LPCSTR pszFormat, va_list argptr);

	void VWriteNoFlush(LPCSTR pszFormat, va_list argptr);

	void Flush()
	{
		if (!m_pLogFile)
			return;

		fflush(m_pLogFile);
	}

	void SetFilename(TCHAR * fp)
	{
		if (fp)
		{
			_tcsncpy_s(m_szFile, fp, MAX_PATH);
		}
	}
	LPCTSTR Filename()
	{ return m_szFile; }

	bool IsOpen(){ return (m_pLogFile != 0); }

private:

	FILE*	m_pLogFile;
	int		m_nFlushCount;
	int		m_nWriteCount;
	int		m_lTruncate;
	bool	m_bTS;
	TCHAR	m_szFile[MAX_PATH+1];

};

#endif //_ATA_LOGFILE_
///@endcond
