// Log.cpp: implementation of the Log class.
//
//////////////////////////////////////////////////////////////////////

#include "Common/Support/Log.h"
#include <string.h>
#include <stdarg.h>
#ifdef WIN32
#include <direct.h>
#else
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <limits.h>
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

#ifndef _MAX_PATH
#define _MAX_PATH _POSIX_PATH_MAX
#endif

char *Log::m_logDir = NULL;
int Log::nInstances = 0;
Log::Log()
{
#ifdef _DEBUG
	nInstances++;
	char *str="log";
	m_szFileName = new char[sizeof(str)];
	strcpy(m_szFileName, str);
	m_file = NULL;
#endif
}

Log::~Log()
{
#ifdef _DEBUG
	nInstances--;
	if (nInstances == 0  && m_logDir != NULL)
		delete []m_logDir;
	if (m_file!=NULL)
		fclose(m_file);
	if (m_szFileName!=NULL)
		delete [] m_szFileName;
#endif
}

Log::Log(char *szfileName)
{
#ifdef _DEBUG
	nInstances++;
	m_szFileName = new char[strlen(szfileName)+1];
	strcpy(m_szFileName, szfileName);
	m_file = NULL;
#endif
}

bool Log::Create(int uniform)
{
#ifdef _DEBUG
	char szFN[_MAX_PATH];
	sprintf(szFN, 
#ifdef WIN32
			"%s\\%s_%d"
#else
			"%s/%s_%d"
#endif
			,m_logDir, m_szFileName, uniform);

	m_file = fopen(szFN, "a+");
	if (m_file!=NULL)
		return true;
	return false;
#endif
	return true;
}

void Log::printf(const char *format, ... )
{
#ifdef _DEBUG
	if (m_file!=NULL){
		va_list args;
		va_start(args, format);
		vfprintf(m_file, format, args);
		va_end(args);
	}
#endif
}

void Log::Init()
{
#ifdef _DEBUG
	char buffer[_MAX_PATH];
#ifdef WIN32
	if(_getcwd( buffer, _MAX_PATH ) == NULL )
      		return;
	_mkdir("Log");
#else
	if(getcwd( buffer, _MAX_PATH ) == NULL )
      		return;
	mkdir("Log", 0755);
#endif
	strcat(buffer, 
#ifdef WIN32
			"\\Log"
#else
			"/Log"
#endif
			);
	
	if (m_logDir!=NULL)
		delete[] m_logDir;
	m_logDir = new char[strlen(buffer)+1];
	strcpy(m_logDir, buffer);
#endif
}


/// \brief Ulozi do logu vektor v tvare 
///					|absolutna hodnota| uhol' (X-ova zlozka, Y-ova zlozka)
/// \param v Vektor ktory sa ma logovat
/// \param format formatovanie v tvare pre printf, vypise sa pred samotny vektor
void Log::PrintVector(Vector v, const char *format, ...)
{
#ifdef _DEBUG
	if (m_file!=NULL){
		va_list args;
		va_start(args, format);
		vfprintf(m_file, format, args);
		va_end(args);
		fprintf(m_file, ": |%3.2f| %3.2f\xf8\t(%3.2f, %3.2f)", v.GetLength(), v.GetAngle().GetDeg(), v.GetX(), v.GetY());
	}
#endif
}

/// \brief Ulozi do logu vektor v tvare 
///					|absolutna hodnota| uhol' (X-ova zlozka, Y-ova zlozka)
/// \param p Point ktory sa ma logovat
/// \param formatBefore formatovanie v tvare pre printf, vypise sa pred samotny Point
void Log::PrintPoint(Point p, const char *formatBefore, ... )
{
#ifdef _DEBUG
	if (m_file!=NULL){
		va_list args;
		va_start(args, formatBefore);
		vfprintf(m_file, formatBefore, args);
		va_end(args);
		fprintf(m_file, ": %3.2f, %3.2f", p.GetX(), p.GetY());
	}
#endif
}

void Log::PrintAngle(Angle a, const char *formatBefore, ... )
{
#ifdef _DEBUG
	if (m_file!=NULL){
		va_list args;
		va_start(args, formatBefore);
		vfprintf(m_file, formatBefore, args);
		va_end(args);
		fprintf(m_file, ": %3.2f\xf8", a.GetDeg());
	}
#endif
}
