/*

  Copyright (C) 2008  INRIA, Plančte Team

  Authors: 
  ------------------------------------------------------------
  Amir Krifa			:  Amir.Krifa@sophia.inria.fr
  Mohamed Karim Sbai	:  Mohamed_Karim.Sbai@sophia.inria.fr
  Chadi Barakat			:  Chadi.Barakat@sophia.inria.fr
  Thierry Turletti		:  Thierry.Turletty@sophia.inria.fr
  ------------------------------------------------------------

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License version 3
as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/



/*!
* \file LogFile.cpp
* \brief Implementation of the class LogFile used to write and manage log files generated by the WM-BITTORRENT.  
* \author Amir Krifa, krifa_amir@hotmail.com
* \version 1.0 
*/

#include "StdAfx.h"
#include "LogFile.h"
#include <windows.h>


extern LPWSTR ConvertLPCSTRToLPWSTR (char* pCstring);
bool ACTIVATE_LOG = false;

LogFile::LogFile(std::string file)
{
	
	InitializeCriticalSection(&log_sync);
	EnterCriticalSection(&log_sync);
	file_name.append(file);
	log_file=fopen(file_name.c_str(),"w");
	LeaveCriticalSection(&log_sync);

}

LogFile::~LogFile(void)
{
	EnterCriticalSection(&log_sync);
	fclose(log_file);
	LeaveCriticalSection(&log_sync);

	DeleteCriticalSection(&log_sync);
}

void LogFile::add_line(std::string  line, bool SetTime)
{
	EnterCriticalSection(&log_sync);
	char szCurrentTime[100];
	if(SetTime)
	{
		// Getting the local current time
		SYSTEMTIME systime={0};
		GetLocalTime(&systime);
		sprintf(szCurrentTime,"%u:%u:%u",systime.wHour,systime.wMinute,systime.wSecond);
	}	
	string line_to_add;
	if(SetTime)
	{	
		fprintf(log_file,"%s\n","----------------------------------");
		fflush(log_file);
		line_to_add.append(szCurrentTime+string("--"));
	}
	line_to_add.append(line);
	
	
	fprintf(log_file,"%s\n",line_to_add.c_str());
	fflush(log_file);
	LeaveCriticalSection(&log_sync);
}

void LogFile::close()
{
}

bool LogFile::file_exists(string file_name)
{
	 return true;

}