#include "stdafx.h"
#include "log_module.h"
#include "Functions/Functions.h"

std::vector<std::shared_ptr<MyLog>> vtLog;
std::map<std::string,std::shared_ptr<MyLog>> mpLog;

int Log::init_log_module()
{
	clear();

	for (int i = 0; i < FILE_TYPE_TOTLA; i++)
	{
		std::shared_ptr<MyLog> log = std::make_shared<MyLog>((FILE_TYPE)i);
		vtLog.push_back(log);

		//mpLog.insert(std::make_pair(my_log_file_name[i],log));
	}

	return 0;
}

int Log::write_log(FILE_TYPE fileType,const std::string& content)
{
	if (fileType > FILE_TYPE::FILE_TYPE_TOTLA)
	{
		return 1;
	}

	if (vtLog.size() < fileType)
	{
		return 1;
	}
	
	std::vector<std::shared_ptr<MyLog>>::iterator it = vtLog.begin();
	for (;it != vtLog.end();++it)
	{
		if ((*it)->GetFileType() == fileType)
		{
			break;
		}
	}
	if (it != vtLog.end())
	{
		(*it)->WriteLog(content);
	}
	//vtLog[fileType]->WriteLog(content);

	return 0;
}

int Log::write_log(FILE_TYPE fileType,const char* pValue,bool status)
{
	if (fileType > FILE_TYPE::FILE_TYPE_TOTLA)
	{
		return 1;
	}

	if (vtLog.size() < fileType)
	{
		return 1;
	}

	if (status != vtLog[fileType]->GetStatus())
	{
		vtLog[fileType]->SetLayout(status);
	}

	write_log(fileType,pValue);

	return 0;
}

int Log::write_log(FILE_TYPE fileType,const char* pValue)
{
	std::string val = "";
	val = pValue;

	write_log(fileType,val);

	return 0;
}

//int Log::write_log(FILE_TYPE fileType,const char* pValue,...)
//{
//	std::string val = "";
//	val = pValue;
//
//	write_log(fileType,val);
//
//	return 0;
//}

int Log::write_log(FILE_TYPE fileType,const std::string& content,bool status)
{
	if (fileType > FILE_TYPE::FILE_TYPE_TOTLA)
	{
		return 1;
	}

	if (vtLog.size() < fileType)
	{
		return 1;
	}

	if (status != vtLog[fileType]->GetStatus())
	{
		vtLog[fileType]->SetLayout(status);
	}
	
	write_log(fileType,content);

	return 0;
}

int Log::clear()
{
	if (vtLog.size() > 0)
	{
		vtLog.erase(vtLog.begin(),vtLog.end());
		vtLog.resize(0);
	}

	if (mpLog.size() > 0)
	{
		mpLog.erase(mpLog.begin(),mpLog.end());
	}

	return 0;
}