//	Darkstar Engine  (c) UnderWorld Interactive Studio (all coding by Svarog    2007)
//
//      Данный файл является частью Darkstar Engine. Любое использование кода файла
//      без согласия компании, является нарушением закона об авторском праве (в слу-
//      чае если не оговорено обратное).
//
//      Имя файла: log.inl
//      Краткое описание: лог приложения
//

#include <Darkstar/Kernel/Misc/memory_management.hpp>

namespace ker
{
	inline Log::Log()
	{
		warnings = 0;
		errors = 0;

		to_console = false;
	};

	inline Log::~Log()
	{
		close();
	};

	inline Log& Log::writeError(const std::string& file, const unsigned int line, const std::string& func)
	{
		log << "\n<BR>";
		log << "<FONT COLOR=\"#000080\" size=3> [" << getFileName(file);
		log << "<FONT COLOR=\"#000000\">:" << line;
		log << "]<FONT COLOR=\"#A52A2A\"> " << func.c_str() << "()";
		log << "<FONT COLOR=\"#FF0000\" > Error: ";
		log << "<FONT COLOR=\"#000000\">";
		errors++;
		if(to_console)
			std::cout << "Error in File: " << file << "  Line: " << line << "  Function: " << func << std::endl;
		log.flush();
		return *this;
	};

	inline Log& Log::writeWarning(const std::string& file, const unsigned int line, const std::string& func)
	{
		log << "\n<BR>";
		log << "<FONT COLOR=\"#000080\" size=3> [" << getFileName(file);
		log << "<FONT COLOR=\"#000000\" >:" << line;
		log << "]<FONT COLOR=\"#A52A2A\" > " << func.c_str() << "()";
		log << "<FONT COLOR=\"#008000\" > Warning: ";
		log << "<FONT COLOR=\"#000000\">";
		warnings++;
		if(to_console)
			std::cout << "Warning in File: " << file << "  Line: " << line << "  Function: " << func << std::endl;
		log.flush();
		return *this;
	};

	inline Log& Log::writeMessage(const std::string& file, const unsigned int line, const std::string& func)
	{
		log << "\n<BR>";
		log << "<FONT COLOR=\"#000080\" size=3> [" << getFileName(file);
		log << "<FONT COLOR=\"#000000\" >:" << line;
		log << "]<FONT COLOR=\"#A52A2A\" > " << func << "()";
		log << "<FONT COLOR=\"#660066\" > Message: ";
		log << "<FONT COLOR=\"#000000\">";
		if(to_console)
			std::cout << "Message in File: " << file << "  Line: " << line << "  Function: " << func << std::endl;
		log.flush();
		return *this;
	};

	inline const char* Log::getFileName(const std::string& file)
	{
		const char* f = strrchr(file.c_str(), '\\');
		if(f == 0)
			f = strrchr(file.c_str(), '/');
		if(f == 0)
			return file.c_str();
		else
			return ++f;
	};

	inline void Log::open(const std::string& filename)
	{
		log.open(filename.c_str(), std::ios::out | std::ios::trunc);
		log << "<HTML> \n";
		log << "<HEAD> \n";
		log << "<TITLE>Лог файл</TITLE>\n";
		log << "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=windows-1251\">\n";
		log << "<BODY BGCOLOR=#FFFFFF>\n</BODY>";
		log << "<FONT FACE=\"Courier\"\"Times  Roman\",\"Arial\",\"Tahoma\">\n";
		log << "<B><FONT size=3 FACE=\"Times New Roman\"> " << filename.c_str() << "  log file</B>\n</FONT>";
		log << "<BR><BR><BR>\n";
		log.flush();
	};

	inline void Log::close()
	{
		log << "<P>\n\n<FONT COLOR=\"green\">Warnigs: " << warnings << std::endl;
		log << "<P>\n\n<FONT COLOR=\"red\">Errors: " << errors << std::endl;
		log.close();
	};

	template <typename T>
	inline Log& Log::operator<<(const T& obj)
	{
		log << obj;
		if(to_console)
			std::cout << obj;
		log.flush();
		return *this;
	};

	inline bool Log::getIsToConsole()
	{
		return to_console;
	};

	inline void Log::setIsToConsole(bool is_true)
	{
		to_console = is_true;
	};
};