
#include "LogFiles.h"
#include <string.h>

const string CLogFiles::m_strFileNameLoglaserBinary1 = "laser1.data";
const string CLogFiles::m_strFileNameLoglaserValue1 = "laser1.txt";
const string CLogFiles::m_strFileNameLogPoints1 = "points1.txt";

const string CLogFiles::m_strFileNameLoglaserBinary2 = "laser2.data";
const string CLogFiles::m_strFileNameLoglaserValue2 = "laser2.txt";
const string CLogFiles::m_strFileNameLogPoints2 = "points2.txt";



CLogFile::CLogFile()
{

}


CLogFile::~CLogFile()
{
	if (m_file)
	{
        m_lock.lock();
		m_file.close();
        m_lock.unlock();
	}
}

void CLogFile::open(const char *_Filename, bool binary /* = false */)
{
	if (!m_file.is_open())
	{
        m_lock.lock();
		if (binary)
			m_file.open(_Filename, ios_base::out | ios_base::binary);
		else
			m_file.open(_Filename, ios_base::out);
        m_lock.unlock();
	}
}

void CLogFile::write(const char *_Str,	streamsize _Count)
{
	if (m_file.is_open())
	{
        m_lock.lock();
		m_file.write(_Str, _Count);
        m_lock.unlock();
	}
}

void CLogFile::write_double(double val)
{
	char buffer[512] = { 0 };
	if (m_file.is_open())
	{
        m_lock.lock();
        sprintf(buffer, "%f ", val);
		m_file.write(buffer, strlen(buffer));
        m_lock.unlock();
	}
}

void CLogFile::write_int(int val)
{
	char buffer[512] = { 0 };
	if (m_file.is_open())
	{
        m_lock.lock();
        sprintf(buffer, "%d ", val);
		m_file.write(buffer, strlen(buffer));
        m_lock.unlock();
	}
}

void CLogFile::close()
{
	if (m_file.is_open())
	{
        m_lock.lock();
		m_file.close();
        m_lock.unlock();
	}
}

/****************************************************************************/

CLogFiles::CLogFiles()
	: m_bBinary(true)
	, m_bValue(true)
	, m_bPoints(true)
{

}

CLogFiles::~CLogFiles()
{
	close_project();
}

void CLogFiles::close_project()
{
	m_logLaserBinary1.close();
	m_logLaserValue1.close();
	m_logPoints1.close();

	m_logLaserBinary2.close();
	m_logLaserValue2.close();
	m_logPoints2.close();
}

void CLogFiles::new_project(const char * path)
{
	close_project();

	if (m_bBinary) 
	{
		string filepath1 = string(path) + m_strFileNameLoglaserBinary1;
		m_logLaserBinary1.open(filepath1.c_str(), true);

		string filepath3 = string(path) + m_strFileNameLoglaserBinary2;
		m_logLaserBinary2.open(filepath3.c_str(), true);
	}

	if (m_bValue)
	{
		string filepath1 = string(path) + m_strFileNameLoglaserValue1;
		m_logLaserValue1.open(filepath1.c_str(), true);

		string filepath3 = string(path) + m_strFileNameLoglaserValue2;
		m_logLaserValue2.open(filepath3.c_str(), true);
	}

	if (m_bPoints)
	{
		string filepath2 = string(path) + m_strFileNameLogPoints1;
		m_logPoints1.open(filepath2.c_str(), true);

		string filepath4 = string(path) + m_strFileNameLogPoints2;
		m_logPoints2.open(filepath4.c_str(), true);
	}
	
}
